MySQL追加注释或者大量修改注释
2016-02-27 11:39
726 查看
MySQL追加注释或者大量修改注释 2016-01-25 20:28:05
分类: MySQL
MySQL 5.6.14
之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是MySQL后期追加注释比较麻烦
需要使用modify语法。
只要不小心写错一点,就可能导致表结构的变更,而不是注释的变更.
实验表如下:
create table t(
c1 int primary key auto_increment,
c2 char(20) not null default 'c2' comment 'c2的注释',
c3 date default '2016-01-25' comment 'date类型测试',
c4 varchar(20) not null default '' ,
c5 bigint ,
c6 text comment 'text测试',
c7 timestamp not null default current_timestamp on update current_timestamp,
c8 datetime not null default now()
);
通过如下的SQL,解析元数据信息,可以直接显示modify的内容.
追加或者修改注释之后,执行语句即可.
这样可以避免人为的失误.
SELECT
concat(
'alter table ',
table_schema, '.', table_name,
' modify column ', column_name, ' ', column_type, ' ',
if(is_nullable = 'YES', ' ', 'not null '),
if(column_default IS NULL, '',
if(
data_type IN ('char', 'varchar')
OR
data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
concat(' default ''', column_default,''''),
concat(' default ', column_default)
)
),
if(extra is null or extra='','',concat(' ',extra)),
' comment ''', column_comment, ''';'
) s
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 't'
以实验表为例,生成的modify语句如下.
alter table test.t modify column c1 int(11) not null auto_increment comment '';
alter table test.t modify column c2 char(20) not null default 'c2' comment 'c2的注释';
alter table test.t modify column c3 date default '2016-01-25' comment 'date类型测试';
alter table test.t modify column c4 varchar(20) not null default '' comment '';
alter table test.t modify column c5 bigint(20) comment '';
alter table test.t modify column c6 text comment 'text测试';
alter table test.t modify column c7 timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '';
alter table test.t modify column c8 datetime not null default CURRENT_TIMESTAMP comment '';
分类: MySQL
MySQL 5.6.14
之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是MySQL后期追加注释比较麻烦
需要使用modify语法。
只要不小心写错一点,就可能导致表结构的变更,而不是注释的变更.
实验表如下:
create table t(
c1 int primary key auto_increment,
c2 char(20) not null default 'c2' comment 'c2的注释',
c3 date default '2016-01-25' comment 'date类型测试',
c4 varchar(20) not null default '' ,
c5 bigint ,
c6 text comment 'text测试',
c7 timestamp not null default current_timestamp on update current_timestamp,
c8 datetime not null default now()
);
通过如下的SQL,解析元数据信息,可以直接显示modify的内容.
追加或者修改注释之后,执行语句即可.
这样可以避免人为的失误.
SELECT
concat(
'alter table ',
table_schema, '.', table_name,
' modify column ', column_name, ' ', column_type, ' ',
if(is_nullable = 'YES', ' ', 'not null '),
if(column_default IS NULL, '',
if(
data_type IN ('char', 'varchar')
OR
data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
concat(' default ''', column_default,''''),
concat(' default ', column_default)
)
),
if(extra is null or extra='','',concat(' ',extra)),
' comment ''', column_comment, ''';'
) s
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 't'
以实验表为例,生成的modify语句如下.
alter table test.t modify column c1 int(11) not null auto_increment comment '';
alter table test.t modify column c2 char(20) not null default 'c2' comment 'c2的注释';
alter table test.t modify column c3 date default '2016-01-25' comment 'date类型测试';
alter table test.t modify column c4 varchar(20) not null default '' comment '';
alter table test.t modify column c5 bigint(20) comment '';
alter table test.t modify column c6 text comment 'text测试';
alter table test.t modify column c7 timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '';
alter table test.t modify column c8 datetime not null default CURRENT_TIMESTAMP comment '';
相关文章推荐
- MySQL知识(十五)——存储过程的调用、查看、修改和删除
- mysql workBeach 简单实用
- MySQL知识(十四)——存储过程的创建
- MySQL详解--锁
- MySQL中EXPLAIN解释命
- mysql开启GTID环境使用xtrabackup备份搭建复制环境
- mysql 线程等待时间,解决sleep进程过多的办法
- mysql学习笔记
- mysql --- 存储过程
- mysql ---自定义函数
- mysql批量删除前缀相同的表
- 数据库---mysql 事务
- mysql开启慢SQL并分析原因
- MySQL修改tmpdir参数
- MySQL批量插入数据脚本
- mysql主从复制跳过错误
- 死锁-1.0.2
- mysql拒绝访问 Error 1044/1045 问题的解决
- mysql字段类型的选择
- mac下修改mysql密码