### 复制表结构+记录
create table 新表名 select * from 旧表名;
mysql> create table t4 select * from t3;
mysql> desc t4;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
### 复制表结构,没有内容,只有字段名
create table 新表名 select * from 旧表名 where 1=3;
create table 新表名 like 旧表名;
mysql> create table t5 select * from t3 where 1=3;
mysql> desc t5;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
修改
语法:
1. 修改表名
alter table 表名
rename 新表名;
mysql> alter table t5
-> rename t6;
Query OK, 0 rows affected (0.03 sec)
mysql> desc t5;
ERROR 1146 (42S02): Table 'db1.t5' doesn't exist
mysql> desc t6;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
2. 增加字段
alter table 表名
add 字段名 数据类型 [完整性约束条件…],
add 字段名 数据类型 [完整性约束条件…];
alter table 表名
add 字段名 数据类型 [完整性约束条件…] first;
alter table 表名
add 字段名 数据类型 [完整性约束条件…] after 字段名;
mysql> alter table t6
-> add score int;
mysql> desc t6
-> ;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
3. 删除字段
alter table 表名
drop 字段名;
mysql> alter table t6
-> drop score;
mysql> desc t6;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
4. 修改字段
alter table 表名
modify 字段名 数据类型 [完整性约束条件…];
alter table 表名
change 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
alter table 表名
change 旧字段名 新字段名 新数据类型 [完整性约束条件…];
mysql> alter table t6
-> change age Age int;
mysql> desc t6;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| Age | int(11) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+
示例:
1. 修改存储引擎
mysql> alter table service
-> engine=innodb;
2. 添加字段
mysql> alter table student10
-> add name varchar(20) not null,
-> add age int(3) not null default 22;
mysql> alter table student10
-> add stu_num varchar(10) not null after name; //添加name字段之后
mysql> alter table student10
-> add sex enum('male','female') default 'male' first; //添加到最前面
3. 删除字段
mysql> alter table student10
-> drop sex;
mysql> alter table service
-> drop mac;
4. 修改字段类型modify
mysql> alter table student10
-> modify age int(3);
mysql> alter table student10
-> modify id int(11) not null primary key auto_increment; //修改为主键
5. 增加约束(针对已有的主键增加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
6. 对已经存在的表增加复合主键
mysql> alter table service2
-> add primary key(host_ip,port);
7. 增加主键
mysql> alter table student1
-> modify name varchar(10) not null primary key;
8. 增加主键和自动增长
mysql> alter table student1
-> modify id int not null primary key auto_increment;
9. 删除主键
a. 删除自增约束
mysql> alter table student10 modify id int(11) not null;
b. 删除主键
mysql> alter table student10
-> drop primary key;
操作文件内容
插入数据
mysql> insert into t6 values
-> (1,'xiaoming','male',17);
mysql> insert into t6(name) values
-> ('xiaohong') ;
查询数据
mysql> select * from t6;
+------+----------+------+------+
| id | name | sex | Age |
+------+----------+------+------+
| 1 | xiaoming | male | 17 |
+------+----------+------+------+
mysql> insert into t6(name) values
-> ('xiaohong') ;
mysql> select * from t6;
+------+----------+------+------+
| id | name | sex | Age |
+------+----------+------+------+
| 1 | xiaoming | male | 17 |
| NULL | xiaohong | NULL | NULL |
+------+----------+------+------+
mysql> select name from t6;
+----------+
| name |
+----------+
| xiaoming |
| xiaohong |
+----------+
修改数据
mysql> update db1.t6 set name='people';
mysql> select * from t6;
+------+--------+------+------+
| id | name | sex | Age |
+------+--------+------+------+
| 1 | people | male | 17 |
| NULL | people | NULL | NULL |
+------+--------+------+------+
mysql> update db1.t6 set name='xiaoming' where id=1;
mysql> select * from t6;
+------+----------+------+------+
| id | name | sex | Age |
+------+----------+------+------+
| 1 | xiaoming | male | 17 |
| NULL | people | NULL | NULL |
+------+----------+------+------+
删除数据
delete from t6 where id=1;
mysql> select * from t6;
+------+--------+------+------+
| id | name | sex | Age |
+------+--------+------+------+
| NULL | people | NULL | NULL |
+------+--------+------+------+
mysql> insert into t6 values
-> (1,'xiaoming','female',18),
-> (2,'xiaohong','male',17),
-> (3,'xiaoqiang','female',19);
mysql> select * from t6;
+------+-----------+--------+------+
| id | name | sex | Age |
+------+-----------+--------+------+
| NULL | people | NULL | NULL |
| 1 | xiaoming | female | 18 |
| 2 | xiaohong | male | 17 |
| 3 | xiaoqiang | female | 19 |
+------+-----------+--------+------+
mysql> delete from t6;
mysql> select * from t6;
Empty set