翼度科技»论坛 编程开发 mysql 查看内容

MySQL如何查看添加修改表以及字段注释信息

5

主题

5

帖子

15

积分

新手上路

Rank: 1

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

方法1:创建表的时候添加表的注释信息
  1. create table  if not exists employee

  2.     employee_id int not null comment '员工号',
  3.     employee_name varchar(30) comment '员工姓名'
  4. ) comment '员工信息表';  
复制代码
方法2:使用ALTER TABLE给表添加注释
  1. alter table table_name comment='表的注释';

  2. alter table employee comment='雇员信息表';
复制代码
修改表注释信息

如果修改表的注释信息,只能使用上面的方法2.
  1. alter table table_name comment='表的注释';
复制代码
查看表的注释信息

方法1:查看表的创建脚本,会显示表的注释信息
  1. show  create  table  employee; 
复制代码
方法2: 查询information_schema.tables表。
  1. select table_schema
  2.       ,table_name
  3.       ,table_comment 
  4. from information_schema.tables
  5. where table_schema='kerry' 
  6.   and table_name='employee'\G
复制代码
字段添加注释信息

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

  2.     employee_id int not null comment '员工号',
  3.     employee_name varchar(30) comment '员工姓名',
  4. ) comment '员工信息表';
复制代码
方法2:创建表后,添加字段的注释信息
  1. ALTER TABLE employee CHANGE COLUMN employee_name employee_name varchar(30) DEFAULT NULL  COMMENT '员工姓名2' ;

  2. ALTER TABLE employee MODIFY COLUMN employee_name varchar(30) DEFAULT NULL  COMMENT '修改后的字段注释'; 
复制代码
不过有点奇怪的是,MySQL数据库修改字段的注释或给字段添加注释,都必须加上字段类型,如果你不加字段类型,则会报错。这样给人的感觉非常别扭与怪异。如下所示
  1. mysql>ALTER TABLE employee CHANGE COLUMN employee_name COMMENT '员工姓名2' ;
  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数据库给表字段添加注释语句如下:
  1. --添加字段注释:
  2. COMMENT ON COLUMN employee.employee_name IS '员工姓名';
复制代码
修改字段注释信息

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

方法1:查看表的创建脚本,会显示表的字段注释信息
  1. show  create  table  employee;
复制代码
方法2: show full columns from xxx查看
  1. mysql> show full columns from employee\G
  2. *************************** 1. row ***************************
  3.      Field: employee_id
  4.       Type: int
  5.  Collation: NULL
  6.       Null: NO
  7.        Key: 
  8.    Default: NULL
  9.      Extra: 
  10. Privileges: select,insert,update,references
  11.    Comment: 员工号
  12. *************************** 2. row ***************************
  13.      Field: employee_name
  14.       Type: varchar(30)
  15.  Collation: utf8mb4_general_ci
  16.       Null: YES
  17.        Key: 
  18.    Default: NULL
  19.      Extra: 
  20. Privileges: select,insert,update,references
  21.    Comment: 修改后的字段注释
  22. 2 rows in set (0.00 sec)

  23. mysql>
复制代码
方法3:查看information_schema.columns系统表
  1. select table_schema,table_name,column_name,column_comment from information_schema.columns 
  2. where table_schema='kerry' 
  3.   and table_name='employee'
  4. order by ordinal_position;
复制代码
扫描上面二维码关注我如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
来源:https://www.cnblogs.com/kerrycode/p/18039406
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具