记录:Mysql 修改字段长度、修改列名、新增列、修改自增主键起始值

以下转自https://www.cnblogs.com/yangjinwang/p/5918906.html

alter table 表名 modify column 字段名 类型;

例如

数据库中user表 name字段是varchar(30)

可以用

复制代码

alter table user modify column name varchar(50) ; --修改字段长度

 

alter table test change  column address address1 varchar(30)--修改表列名

 

alter table test add  column name varchar(10); --添加表列  

复制代码

复制代码

2、MySQL 脚本实现  字段默认系统时间 用例 

--添加CreateTime 设置默认时间 CURRENT_TIMESTAMP 

ALTER TABLE `table_name`
ADD COLUMN  `CreateTime` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ;

 

--修改CreateTime 设置默认时间 CURRENT_TIMESTAMP 
ALTER TABLE `table_name`
MODIFY COLUMN  `CreateTime` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' ;

如果设定不了, 请将 DATETIME改为TIMESTAMP。

复制代码

3.mySql 设置表的自增主键的起始值

alter table    表名  AUTO_INCREMENT = 1000;

以下转自https://www.cnblogs.com/sujulin/p/9021355.html

1.修改字段的长度

语法:

ALTER TABLE 表名 MODIFY COLUMN 字段名  数据类型(修改后的长度)

例子:

将字段的长度由10改为20

ALTER TABLE attence MODIFY COLUMN id INT(20)

2.修改字段的名称

语法:

alter table <表名> change <字段名> <字段新名称> <字段的类型>。

例子:

将字段attence_name改为name

ALTER TABLE attence CHANGE attence_name NAME  VARCHAR(20)

3.新增字段

语法:

新增默认为空的字段
ALTER TABLE 表名 ADD COLUMN 字段名 字段类型 DEFAULT NULL; 
新增不为空的字段
ALTER TABLE 表名ADD COLUMN 字段名 字段类型  NOT NULL;

例子:
ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL; 

ALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;

4.删除字段

语法:

ALTER TABLE 表名 DROP COLUMN 字段名;

例子:

ALTER TABLE attence DROP COLUMN age;

5.批量增加字段

方法一
可以使用事务

语法:

begin;                                           //事务开始
alter table 表名  add 字段名  字段类型(长度);
alter table 表名 add 字段名  字段类型(长度);
alter table 表名 add 字段名  字段类型(长度);
alter table 表名 add 字段名  字段类型(长度);
commit;    

例子: 

begin;                                           //事务开始
alter table em_day_data add f_day_house7 int(11);
alter table em_day_data add f_day_house8 int(11);
alter table em_day_data add f_day_house9 int(11);
alter table em_day_data add f_day_house10 int(11);
commit;     

方法二

alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));

alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));

6.批量修改字段名称

语法:

alter table 表 change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null

例子:

alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
change f_day_house12 f_day_hour12 int(11) not null,
change f_day_house13 f_day_hour13 int(11) not null,
change f_day_house14 f_day_hour14 int(11) not null,
change f_day_house15 f_day_hour15 int(11) not null,
change f_day_house16 f_day_hour16 int(11) not null,
change f_day_house17 f_day_hour17 int(11) not null

猜你喜欢

转载自blog.csdn.net/qq_38085240/article/details/84524944