1、修改字段類型、字段名、字段注釋、類型長度、字段默認值
mysql修改字段類型: --能修改字段類型、類型長度、默認值、注釋 --對某字段進行修改 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 '注釋'; -- 正常,能修改字段類型、類型長度、默認值、注釋 mysql修改字段名: ALTER TABLE 表名 CHANGE [column] 舊字段名 新字段名 新數據類型; alter table table1 change column1 column1 varchar(100) DEFAULT 1.2 COMMENT '注釋'; -- 正常,此時字段名稱沒有改變,能修改字段類型、類型長度、默認值、注釋 alter table table1 change column1 column2 decimal(10,1) DEFAULT NULL COMMENT '注釋' -- 正常,能修改字段名、字段類型、類型長度、默認值、注釋 alter table table1 change column2 column1 decimal(10,1) DEFAULT NULL COMMENT '注釋' -- 正常,能修改字段名、字段類型、類型長度、默認值、注釋 alter table table1 change column1 column2; -- 報錯 mysql> alter table white_user change column name nick_name varchar(50) null comment '昵稱'; -- 正確 Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
2、修改表名
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)
3、修改表的注釋
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='新表-白名單表'
4、在指定位置插入新字段
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> 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 '姓名', `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 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關鍵字
5、刪除字段
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 mysql> alter table white_user_new drop erp ; Query OK, 0 rows affected (0.02 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='新表-白名單表'
------------------------------------------------------------------------------------------分割線-------------------------------------------------------------------------------------------------
跟上面一樣的
增加字段:
alter table 表名 ADD 字段 類型 約束 [默認值 注釋]
ALTER TABLE video ADD category_id int(11) unsigned not null DEFAULT '0' COMMENT '視頻分類id';
修改字段名:
alter table 表名 rename column A to B
ALTER TABLE video RENAME COLUMN category_id TO cid;
修改字段類型:
alter table 表名 modify column 字段名 類型 約束 [默認值, 注釋];
ALTER TABLE video MODIFY COLUMN category_id smallint(5) unsigned not null DEFAULT '0' COMMENT '視頻分類id';
修改字段默認值
alter table 表名 alter column 字段名 drop default; --(若本身存在默認值,則先刪除)
alter table 表名 alter column 字段名 set default 默認值; --(若本身不存在則可以直接設定)
ALTER TABLE video ALTER COLUMN sort SET DEFAULT '50';
原文鏈接:https://blog.csdn.net/kimgoo/article/details/54630257