修改字段名、字段类型、类型长度、默认值、注释
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型; alter table table1 change column1 column2 decimal(10,1) DEFAULT NULL COMMENT \'注释\';-- 正确,能修改字段名、字段类型、类型长度、默认值、注释alter table table1 change column2 column2 varchar(100) DEFAULT 1.2 COMMENT \'注释\'; -- 正确,此时字段名称没有改变,能修改字段类型、类型长度、默认值、注释 alter table table1 change column1 column2; -- 报错
使用change关键字,需要写2个字段
- 修改字段类型、类型长度、默认值、注释
ALTER TABLE 表名 MODIFY [COLUMN] 字段名 新数据类型 新类型长度 新默认值 新注释; -- COLUMN可以省略alter table table1 modify column column1 decimal(10,1) DEFAULT NULL COMMENT \'注释\'; -- 正确,能修改字段类型、类型长度、默认值、注释alter table table1 modify column1 decimal(10,2) DEFAULT NULL COMMENT \'注释\'; -- 正确,能修改字段类型、类型长度、默认值、注释
使用modify关键字,写1个字段即可
- 修改表名
ALTER TABLE 旧表名 RENAME TO 新表名 ;mysql> show tables ;+-------------------+| Tables_in_db_test |+-------------------+| white_user |+-------------------+1 row in set (0.00 sec) mysql> alter table white_user rename to white_user_new ;Query OK, 0 rows affected (0.00 sec) mysql> show tables ;+-------------------+| Tables_in_db_test |+-------------------+| white_user_new |+-------------------+1 row in set (0.00 sec)
- 修改表的注释
ALTER TABLE 表名 COMMENT \'新注释\' mysql> alter table white_user_new comment \'新表-白名单表\' ;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table white_user_new ; CREATE TABLE `white_user_new` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT \'ID\', `name` varchar(50) NOT NULL COMMENT \'姓名\', `created_time` datetime DEFAULT NULL COMMENT \'创建时间\', `updated_time` datetime DEFAULT NULL COMMENT \'更新时间\', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT=\'新表-白名单表\'
- 在指定位置添加新字段(如果不使用after,默认是在最后加字段)
ALTER TABLE 表名 ADD [COLUMN] 字段名 字段类型 是否可为空 COMMENT \'注释\' AFTER 指定某字段 ;--COLUMN关键字可以省略不写mysql> alter table white_user_new add column erp varchar(50) not null comment \'erp账号\' after name ;Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0--在name字段后面添加erp字段mysql> alter table white_user_new add position varchar(50) not null comment \'岗位\' after name ;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0--在name字段后面添加position字段。 mysql> show create table white_user_new ; CREATE TABLE `white_user_new` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT \'ID\', `name` varchar(50) NOT NULL COMMENT \'姓名\', `position` varchar(50) NOT NULL COMMENT \'岗位\', `erp` varchar(50) NOT NULL COMMENT \'erp账号\', `created_time` datetime DEFAULT NULL COMMENT \'创建时间\', `updated_time` datetime DEFAULT NULL COMMENT \'更新时间\', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT=\'新表-白名单表\' mysql> alter table white_user_new add mobile varchar(50) not null comment \'手机号码\' before position ;--报错,在position字段前添加mobile字段,不能使用before关键字
- 删除字段
ALTER TABLE 表名 DROP [COLUMN] 字段名 ;-- COLUMN关键字可以省略不写 mysql> alter table white_user_new drop column position ;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0-- 删除position字段 mysql> alter table white_user_new drop erp ;Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0-- 删除erp字段mysql> show create table white_user_new ; CREATE TABLE `white_user_new` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT \'ID\', `name` varchar(50) NOT NULL COMMENT \'姓名\', `created_time` datetime DEFAULT NULL COMMENT \'创建时间\', `updated_time` datetime DEFAULT NULL COMMENT \'更新时间\', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT=\'新表-白名单表\'