MySQL添加註釋

在數據庫設計中,咱們是建議爲數據庫,數據表以及數據字段進行添加註釋的,MySQL數據庫中, 字段或列的註釋是用屬性comment來添加。數據庫

建立新表的腳本中, 可在字段定義建立表腳本中添加comment屬性來添加註釋。數據庫設計

示例代碼以下:測試

1 create table test( 
2     id int not null default 0 comment '用戶id' ) 

若是是已經建好的表, 也能夠用修改字段的命令,而後加上comment屬性定義,就能夠添加上註釋了。spa

示例代碼以下:設計

1 alter table test 
2     change column id id int not null default 0 comment '測試表id'    

查看已有表的全部字段的註釋呢? 
能夠用命令:show full columns from table_name 來查看, 示例以下:code

1 show full columns from test;

建立表的時候寫註釋

1 create table test1 ( 
2     field_name int comment '字段的註釋' 
3 )comment='表的註釋'; 

修改表的註釋

1 alter table test1 comment '修改後的表的註釋';

修改字段的註釋

1 alter table test1 modify column field_name int comment '修改後的字段註釋'; 
2 
3 --注意:字段名和字段類型照寫就行

查看錶註釋的方法

1 --在生成的SQL語句中看 
2     show  create  table  test1; 
3 --在元數據的表裏面看
4     use information_schema; 
5     select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

查看字段註釋的方法

1 --show 
2     show  full  columns  from  test1; 
3 --在元數據的表裏面看 
4     select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G
相關文章
相關標籤/搜索