分类: 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. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  2. Poj OpenJudge 百练 2602 Superlong sums

    1.Link: http://poj.org/problem?id=2602 http://bailian.openjudge.cn/practice/2602/ 2.Content: Superlo ...

  3. 学习笔记--Quartz定时器

    文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/338.html 1.Quartz定时器执行流程 Quartz需要定义执行任务.触发器,在 ...

  4. c#中的枚举

    1.枚举概念:枚举是用户定义的整型类型,在声明一个枚举时,要指定该枚举的实例可以包含的一组可接受的值,还可以给值指定易于记忆的名称.如果在代码的某个地方,要试图把一个不可接受范围内的值赋予枚举的一个实 ...

  5. PostgreSQL 8.1 中文文档

    PostgreSQL 8.1 中文文档 http://www.php100.com/manual/PostgreSQL8/

  6. 去掉iphone手机滑动默认行为

    /*去掉iphone手机滑动默认行为*/ $('body').on('touchmove', function (event) { event.preventDefault(); });

  7. C# 学习之旅(1)

    第一, 输入输出流都来自控制台. using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  8. WPF Button的背景图片设置

    这个问题很简单,但是对于从winfrom转过来的来讲,在做事的时候就会被绕进去,言归正传,如何设置一个bUtton的背景图片?如何去掉让人烦的默认选中时的灰色背景?请看如下的描述.问题的来源和解决都在 ...

  9. HDFS知识体系 思维导图

  10. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(六)

    使用 HttpApplication 对象 ASP.NET 框架中的许多类都提供了许多很方便的属性可以直接映射到 HttpContext 类中定义的属性.这种交叠有一个很好的例子就是 HttpAppl ...