分类: MySQL

MySQL 5.6.14

之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是MySQL后期追加注释比较麻烦
需要使用modify语法。

只要不小心写错一点,就可能导致表结构的变更,而不是注释的变更.

实验表如下:

  1. create table t(
  2. c1 int primary key auto_increment,
  3. c2 char(20) not null default 'c2'  comment 'c2的注释',
  4. c3 date default '2016-01-25' comment 'date类型测试',
  5. c4 varchar(20) not null default '' ,
  6. c5 bigint ,
  7. c6 text comment 'text测试',
  8. c7 timestamp not null default current_timestamp on update current_timestamp,
  9. c8 datetime not null default now()
  10. );

通过如下的SQL,解析元数据信息,可以直接显示modify的内容.
追加或者修改注释之后,执行语句即可.
这样可以避免人为的失误.

  1. SELECT
  2. concat(
  3. 'alter table ',
  4. table_schema, '.', table_name,
  5. ' modify column ', column_name, ' ', column_type, ' ',
  6. if(is_nullable = 'YES', ' ', 'not null '),
  7. if(column_default IS NULL, '',
  8. if(
  9. data_type IN ('char', 'varchar')
  10. OR
  11. data_type IN ('date', 'datetime', 'timestamp') AND column_default != 'CURRENT_TIMESTAMP',
  12. concat(' default ''', column_default,''''),
  13. concat(' default ', column_default)
  14. )
  15. ),
  16. if(extra is null or extra='','',concat(' ',extra)),
  17. ' comment ''', column_comment, ''';'
  18. ) s
  19. FROM information_schema.columns
  20. WHERE table_schema = 'test'
  21. AND table_name = 't'

以实验表为例,生成的modify语句如下.

  1. alter table test.t modify column c1 int(11) not null  auto_increment comment '';
  2. alter table test.t modify column c2 char(20) not null  default 'c2' comment 'c2的注释';
  3. alter table test.t modify column c3 date   default '2016-01-25' comment 'date类型测试';
  4. alter table test.t modify column c4 varchar(20) not null  default '' comment '';
  5. alter table test.t modify column c5 bigint(20)   comment '';
  6. alter table test.t modify column c6 text   comment 'text测试';
  7. alter table test.t modify column c7 timestamp not null  default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '';
  8. alter table test.t modify column c8 datetime not null  default CURRENT_TIMESTAMP comment '';
 
 

MySQL追加注释或者大量修改注释的更多相关文章

  1. svn 强制用户添加注释 和 允许用户修改注释

    当我们用TortoiseSVN提交代码时,有很多人不喜欢写注释,导致以后代码版本多,也不清楚哪个版本到底改了什么东西.所以在提交的时候,我会强制要求添加注释.这是如何实现的?这个话题就涉及到了svn的 ...

  2. mysql查看和修改注释

    MySQL查看注释,MySQL修改注释,mysql查看注释,mysql修改注释 1.给大家做演示,这里随便创建一张学生表,代码如下: CREATE TABLE `student` ( `id` int ...

  3. mysql中注释的添加修改

    1 创建表的时候写注释,在使用comment关键字语法:create table 表名 (字段名 类型 comment '字段的注释' )comment='表的注释'; 查看一下 2 修改表的注释al ...

  4. mysql添加表注释、字段注释、查看与修改注释

    1 创建表的时候写注释create table test1( field_name int comment '字段的注释')comment='表的注释'; 2 修改表的注释alter table te ...

  5. git 修改注释信息

    1. push 之前 先看看自己提交过多少次,然后执行 git rebase -i HEAD~数字(你要修改你的第几次提交) 接下来执行,修改注释 git commit --amend 修改完注释之后 ...

  6. Eclipse设置问题:字体大小、修改注释内容、修改快捷键

    一.设置字体大小,看下图,包括了设计代码字体大小和控制台输出字体大小 二.修改注释内容 选择window---->>preferences 选择Java---->>code s ...

  7. MySql给表添加列和注释

    1.给表添加列 ALTER TABLE supplier_seller ADD COLUMN company_id INT NULL COMMENT '供应主体id'; 默认情况下,添加的列会添加到最 ...

  8. pycharm修改注释颜色

    原来的注释是红色的,看着跟报错似的.. 还有flask中html文件的注释,我修改了Django的注释颜色,flask也就改了 也可以直接点击下面的代码,哪里难看点哪里

  9. git强制修改注释

    在一些公司项目中,常常要求git注释提交的时候加上前缀,比如JIRA号,但是有的时候我们常常会忘了 如果用source tree等一些工具,会推送到本地仓库一半,但远程又上不去. 这个时候我们就需要强 ...

随机推荐

  1. centos mail 不能发邮件

    http://alfred-long.iteye.com/blog/1836488 (参考) 最近centos 6.4突然不能发邮件了, 直接用 mail命令测试也不收不到邮件 以下参考大侠们的经验后 ...

  2. php100 编程小技巧

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中 ...

  3. linux命令之ps命令

    1.管道 linux命令管道通过|表示.一般在linux命令中|(管道)之前的命令会输出大量的结果,|(管道)之后的命令一般就是带有条件的,只将|前满足条件的结果显示出来. 2.grep命令 grep ...

  4. Python OS模块标准库的系统接口及操作方法

    Python OS模块标准库的系统接口及操作方法 os.name 返回当前操作系统名,定义了'posix','nt','mac','os2','ce','java'(我使用win7/python3.1 ...

  5. iframe 刷新

    iframe刷新父页面 parent.location.reload(); iframe 一个子页面操作过后,刷新指定子页面 parent.frames('ifrmname').location.re ...

  6. bootstrap 框架选型过程

    1.看有没有帮助文档,帮助文档的完整程度. 2.是否是我们业务需要的,花哨的功能真的有用吗? 对于偏pc端使用的系统 toggles意义不大 bootstrap和extjs的区别是什么呢? easyU ...

  7. 【Entity Framework 7】 完全不一样的玩法

    http://www.cnblogs.com/n-pei/p/4274907.html

  8. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

  9. 扎克伯格谈Facebook创业过程

    第一课:Facebook的产品研发 (1)不仅注重用户体验,更关注程序本身对社会和产品的是否有益,进而对产品做出调整 (2)以学校为标准作为群组来划分,就是对产品进行了思考后决定的,不管是直觉决定还是 ...

  10. 实时数据处理环境搭建flume+kafka+storm:4.storm安装配置

    1.解压 apache-storm-0.9.3.tar.gz   2.修改配置文件 conf/storm.yaml --zk地址  storm.zookeeper.servers:  - " ...