MySQL追加注釋或者大量修改注釋

2021-07-16 23:05:10 字數 2588 閱讀 1439

**:

之前乙個專案比較倉促,開發給的建表語句沒有注釋.

現在要補全注釋資訊.

但是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

notnull

default

current_timestamp

onupdate

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

notnull

default

current_timestamp

onupdate

current_timestamp comment '';

alter

table test.t modify column c8 datetime not

null

default

current_timestamp comment '';

MySQL追加注釋或者大量修改注釋

mysql追加注釋或者大量修改注釋 2016 01 25 20 28 05 分類 mysql mysql 5.6.14 之前乙個專案比較倉促,開發給的建表語句沒有注釋.現在要補全注釋資訊.但是mysql後期追加注釋比較麻煩 需要使用modify語法。只要不小心寫錯一點,就可能導致表結構的變更,而不是...

為MYSQL加注釋 mysql注釋符

上午插入記錄的時候一直沒有成功,鬱悶不知道為什麼.因為是很多條記錄一起插入,中間一些不用的資料就用 來注釋了,結果沒有效果.沒有辦法,在網上找了找,才發現注釋符 錯了,需要乙個空格,應該是 mysql注釋符有三種 1 2 3 mysql 伺服器支援 到該行結束 到該行結束 以及 行中間或多個行 的注...

MySQL建表語句 新增注釋

mysql建表語句 新增注釋 1.建表 注釋 create table student id intprimary key auto increment comment 學號 name varchar 200 comment 姓名 age int comment 年齡 comment 學生資訊 2....