mysql修改表或者字段後面的註釋(今天第一次遇見)

今天遇到一個需要修改字段後面的註釋信息,就查了下百度。

 創建表的時候寫註釋。

Sql代碼 

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

        創建學生表示例如下:

 

二.修改表的註釋和創建

Sql代碼 

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

        示例代碼如下:

Sql代碼 

  1. ALTER TABLE `student` COMMENT '學生表2.0';  

        結果如下圖所示: 


 

三. 修改字段的註釋

Sql代碼 

  1. alter table test1 modify column field_name int comment '修改後的字段註釋';  

        注意:字段名和字段類型照寫就行

        修改示例如下:

Sql代碼 

  1. ALTER TABLE `student` MODIFY COLUMN `id` COMMENT '學號';  

        查看字段的信息,代碼如下:

Sql代碼 

  1. SHOW FULL COLUMNS  FROM `student`;  

        結果如圖所示:



 

四.查看錶註釋的方法

1.在生成的SQL語句中看

Sql代碼 

  1. show create table test1;  

        如查看student表的註釋

Sql代碼 

  1. SHOW CREATE TABLE `student`  

2.在元數據的表裏面看

Sql代碼 

  1. use information_schema;  
  2. select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1'  

        或

Sql代碼

  1. select * from information_schema.tables where table_schema='my_db' and table_name='test1';  

五.查看字段註釋的方法

1.show方法

Sql代碼 

  1. show full columns from test1;  

2.在元數據的表裏面看

Sql代碼 

  1. select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1'  

        或

Sql代碼 

  1. select * from information_schema.columns where table_schema='my_db' and table_name='test1'  

 

六.查看錶的約束信息

        mysql 客戶端提供的describe table_name命令,只能顯示一個表的primary key和foreign key。

        mysql所有有關數據schema的信息都保存在INFORMATION_SCHEMA這個database instance裏面。其中的TABLE_CONSTRAINTS和KEY_COLUMN_USAGE表,保存了表的所有key信息。

        TABLE_CONSTRAINTS,保存了表的約束條件,而KEY_COLUMN_USAGE保存了表的詳細column對應的約束條件信息。

        示例如下:

Sql代碼 

  1. select * from information_schema.TABLE_CONSTRAINTS t where t.table_name = 'student';  

Sql代碼 

  1. select * from information_schema.KEY_COLUMN_USAGE t where t.table_name = 'student' and t.CONSTRAINT_NAME = 'PRIMARY';  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章