T8888

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

     https://blog.csdn.net/u010002184/article/details/79354136

分类:

技术点:

相关文章: