MySQL追加注释或者大量修改注释
mysql 修改 注释 或者 大量 追加
2023-09-11 14:19:24 时间
MySQL追加注释或者大量修改注释 2016-01-25 20:28:05
分类: MySQL
MySQL 5.6.14
之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是MySQL后期追加注释比较麻烦
需要使用modify语法。
只要不小心写错一点,就可能导致表结构的变更,而不是注释的变更.
实验表如下:
通过如下的SQL,解析元数据信息,可以直接显示modify的内容.
追加或者修改注释之后,执行语句即可.
这样可以避免人为的失误.
以实验表为例,生成的modify语句如下.
之前一个项目比较仓促,开发给的建表语句没有注释.
现在要补全注释信息.
但是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 '';
相关文章
- Linux(Centos)下安装MySQL
- Navicat Premium连接MySQL 1251错误和Mysql初始化root密码和允许远程访问
- Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
- 批量修改mysql数据库引擎
- 【MySQL】一次修改mysql 默认路径的经历
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- MySQL选择数据库use与mysql_select_db使用详解
- MySql查看与修改auto_increment方法(转)
- mysql权限管理(实例)
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
- 【高并发简单解决方案】redis缓存队列+mysql 批量入库+php离线整合
- MYSQL随机抽取查询 MySQL Order By Rand()效率问题
- [转]2006 MySQL server has gone away错误,最大值溢出解决办法 mysql max_allowed_packet 查询和修改
- ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 43. Created with MySQL 5
- mysql 数据不存在则插入,存在则更新
- MYSQL提权之反弹SHELL——数据库提权属于webshell到管理员的纵向提权,本质还是利用udf提权,无非是在mysql自定义函数中使用了反弹shell而已
- mysql修改密码策略
- mysql子查询
- MySQL----MySQL中因为sql模式设置引起的问题解决办法
- MySQL修改表的主键字段
- mysql 添加用户、修改权限,修改登陆权限ip
- MySQL表设计和多表操作
- windows上同时安装多个版本的mysql数据库
- [MySQL] 解决办法: Error: Transaction test error: file /etc/my.cnf from install of mysql-community-server