创建表:
mysql> create table student( -> id int(4) not null auto_increment, -> name char(8) not null, -> primary key(id), -> key index_name(name) -> ); mysql> show create table studentG *************************** 1. row *************************** Table: student Create Table: CREATE TABLE `student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(8) NOT NULL, PRIMARY KEY (`id`), KEY `index_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 mysql> desc student; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | MUL | NULL | | +-------+---------+------+-----+---------+----------------+ mysql> select * from student; Empty set (0.00 sec)
插入数据insert:
mysql> insert into student (id,name) values(1,'kaka'); mysql> select * from student; +----+------+ | id | name | +----+------+ | 1 | kaka | +----+------+ mysql> insert into student (name) values('bobo'); mysql> select * from student; +----+------+ | id | name | +----+------+ | 2 | bobo | | 1 | kaka | +----+------+ mysql> insert into student values(3,'tom'),(4,'jack'); mysql> select * from student; +----+------+ | id | name | +----+------+ | 2 | bobo | | 4 | jack | | 1 | kaka | | 3 | tom | +----+------+
查询数据select:
mysql> select id,name from student; #指定id,name列查询 +----+------+ | id | name | +----+------+ | 2 | bobo | | 4 | jack | | 1 | kaka | | 3 | tom | +----+------+ mysql> select id,name from student limit 2; #限制2条查询,默认从表头开始,此次是name列加索引的原因 +----+------+ | id | name | +----+------+ | 2 | bobo | | 4 | jack | +----+------+ mysql> select id,name from student limit 0,2; #限制查询0,2条不包括第0条 +----+------+ | id | name | +----+------+ | 2 | bobo | | 4 | jack | +----+------+ mysql> select id,name from student where id=1; +----+------+ | id | name | +----+------+ | 1 | kaka | +----+------+ mysql> select id,name from student where name='kaka'; +----+------+ | id | name | +----+------+ | 1 | kaka | +----+------+ mysql> select id,name from student where id=1 and name='kaka'; +----+------+ | id | name | +----+------+ | 1 | kaka | +----+------+ mysql> select id,name from student where id=2 or name='jack'; +----+------+ | id | name | +----+------+ | 2 | bobo | | 4 | jack | +----+------+ mysql> select id,name from student order by id asc; +----+------+ | id | name | +----+------+ | 1 | kaka | | 2 | bobo | | 3 | tom | | 4 | jack | +----+------+ mysql> select id,name from student order by name desc; +----+------+ | id | name | +----+------+ | 3 | tom | | 1 | kaka | | 4 | jack | | 2 | bobo | +----+------+
explain查看select执行情况:
mysql> desc student; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | MUL | NULL | | +-------+---------+------+-----+---------+----------------+ mysql> explain select id,name from student where name='kaka'G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: student type: ref possible_keys: index_name key: index_name key_len: 24 ref: const rows: 1 name列有索引情况下,只用一条。 Extra: Using where; Using index mysql> alter table student drop key index_name; mysql> desc student; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ mysql> explain select id,name from student where name='kaka'G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: student type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 4 #删除索引后用4条 Extra: Using where
增删表的字段:
mysql> desc student; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | | NULL | | | sex | char(4) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ mysql> alter table student add age tinyint(2) not null after name; mysql> desc student; +-------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | | NULL | | | age | tinyint(2) | NO | | NULL | | | sex | char(4) | NO | | NULL | | +-------+------------+------+-----+---------+----------------+ mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | qq | varchar(18) | NO | | NULL | | | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(8) | NO | | NULL | | | age | tinyint(2) | NO | | NULL | | | sex | char(4) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ mysql> select * from student; +----+----+------+-----+-----+ | qq | id | name | age | sex | +----+----+------+-----+-----+ | | 1 | kaka | 0 | | | | 2 | bobo | 0 | | | | 3 | tom | 0 | | | | 4 | jack | 0 | | +----+----+------+-----+-----+ mysql> alter table student drop qq; mysql> alter table student drop sex; mysql> alter table student drop age; mysql> select * from student; +----+------+ | id | name | +----+------+ | 1 | kaka | | 2 | bobo | | 3 | tom | | 4 | jack | +----+------+ mysql> alter table student add age tinyint(2) not null,add sex char(4) not null; mysql> select * from student; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 1 | kaka | 0 | | | 2 | bobo | 0 | | | 3 | tom | 0 | | | 4 | jack | 0 | | +----+------+-----+-----+
更新表的内容:
mysql> update student set age=18,sex='man' where id=1; mysql> select * from student; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 1 | kaka | 18 | man | | 2 | bobo | 0 | | | 3 | tom | 0 | | | 4 | jack | 0 | | +----+------+-----+-----+
删除表数据:
mysql> delete from student where id=1; mysql> delete from student where id>3; mysql> delete from student where name='tom'; mysql> select * from student; +----+------+-----+-----+ | id | name | age | sex | +----+------+-----+-----+ | 2 | bobo | 0 | | +----+------+-----+-----+
更改表名:
mysql> rename table student to student1; mysql> show tables; +-------------------+ | Tables_in_oldgirl | +-------------------+ | student1 | | test1 | +-------------------+ mysql> alter table student1 rename to student; mysql> show tables; +-------------------+ | Tables_in_oldgirl | +-------------------+ | student | | test1 | +-------------------+
删除表:
drop table test;
清空表
truncate table test; 更快 清空物理文件 delete from test; 逻辑清楚,按行删