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';  

 

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