4. 修改字段
ALTER TABLE 表名
MODIFY 字段名 数据类型 [完整性约束条件...];
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件...];
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件...];
eg:
1. 修改存储引擎
- alter table service
- engine=innodb;
2. 添加字段
- alter table student10
- add name char(20) not null,
- add age int(3) not null default 22;
- alter table student10
- add stu_num varchar(10) not null after name; #添加 name 字段之后
- alter table student10
- add sex enum('male','female') default 'male' first; #添加到最前面
3. 删除字段
- alter table student10
- drop sex;
- alter table service
- drop mac;
4. 修改字段类型 modify
- alter table student10
- modify age int(9);
- alter table student10
- change name NAME char(7);
- alter table student10
- modify id int(11) not null primary key auto_increment; #修改为主键
5. 增加约束 (针对已有的主键增加 auto_increment)
- alter table student10 modify id int(11) not null auto_increment;
- alter table student10 modify id int(11) not null primary key auto_increment;(ERROR)
6. 对已经存在的表增加复合主键
- alter table service2
- add primary key(host_ip,port);
7. 增加主键
- alter table student10
- modify name varchar(10) not null primary key;
8. 增加主键和自动增长
- alter table student10
- modify id int(10) not null primary key auto_increment;# 自动增长的主键应该使用整型
9. 删除主键
a. 删除自增约束
alter table student10 modify id int(11) not null;
b. 删除主键
- alter table student10
- drop primary key;
五, 复制表
复制表结构 + 记录 (key 不会复制: 主键, 外键和索引)
mysql> create table new_service select * from service;
来源: http://www.bubuko.com/infodetail-2687030.html