MySQL数据库中,如何查看表和字段的注释信息,以及如何添加,修改表和字段的注释信息呢?这里简单总结归纳一下。仅供参考。

添加表的注释信息

方法1:创建表的时候添加表的注释信息

create table  if not exists employee

    employee_id int not null comment '员工号',
    employee_name varchar(30comment '员工姓名'
comment '员工信息表';  

方法2:使用ALTER TABLE给表添加注释

alter table table_name comment='表的注释';

alter table employee comment='雇员信息表';

修改表注释信息

如果修改表的注释信息,只能使用上面的方法2.

alter table table_name comment='表的注释';

查看表的注释信息

方法1:查看表的创建脚本,会显示表的注释信息

show  create  table  employee; 

方法2: 查询information_schema.tables表。

select table_schema
      ,table_name
      ,table_comment 
from information_schema.tables
where table_schema='kerry' 
  and table_name='employee'\G

字段添加注释信息

方法1:创建表的时候给字段添加注释

create table employee

    employee_id int not null comment '员工号',
    employee_name varchar(30comment '员工姓名',
comment '员工信息表';

方法2:创建表后,添加字段的注释信息

ALTER TABLE employee CHANGE COLUMN employee_name employee_name varchar(30DEFAULT NULL  COMMENT '员工姓名2' ;

ALTER TABLE employee MODIFY COLUMN employee_name varchar(30DEFAULT NULL  COMMENT '修改后的字段注释'

不过有点奇怪的是,MySQL数据库修改字段的注释或给字段添加注释,都必须加上字段类型,如果你不加字段类型,则会报错。这样给人的感觉非常
别扭与怪异。如下所示

mysql>ALTER TABLE employee CHANGE COLUMN employee_name COMMENT '员工姓名2' ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''员工姓名2'' at line 1

这个跟Oracle数据库有些不同。 Oracle数据库给表字段添加注释语句如下:

--添加字段注释:
COMMENT ON COLUMN employee.employee_name IS '员工姓名';

修改字段注释信息

MySQL表修改字段的注释信息,可以使用上面的方法2.

字段注释信息查看

方法1:查看表的创建脚本,会显示表的字段注释信息

show  create  table  employee;

方法2: show full columns from xxx查看

mysql> show full columns from employee\G
*************************** 1. row ***************************
     Field: employee_id
      Typeint
 CollationNULL
      NullNO
       Key
   DefaultNULL
     Extra: 
Privilegesselect,insert,update,references
   Comment: 员工号
*************************** 2. row ***************************
     Field: employee_name
      Typevarchar(30)
 Collation: utf8mb4_general_ci
      Null: YES
       Key
   DefaultNULL
     Extra: 
Privilegesselect,insert,update,references
   Comment: 修改后的字段注释
2 rows in set (0.00 sec)

mysql>

方法3:查看information_schema.columns系统表

select table_schema,table_name,column_name,column_comment from information_schema.columns 
where table_schema='kerry' 
  and table_name='employee'
order by ordinal_position;