MySQL数据库中,如何查看表和字段的注释信息,以及如何添加,修改表和字段的注释信息呢?这里简单总结归纳一下。仅供参考。
添加表的注释信息
方法1:创建表的时候添加表的注释信息
create table if not exists employee
(
employee_id int not null comment '员工号',
employee_name varchar(30) comment '员工姓名'
) 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(30) comment '员工姓名',
) comment '员工信息表';
方法2:创建表后,添加字段的注释信息
ALTER TABLE employee CHANGE COLUMN employee_name employee_name varchar(30) DEFAULT NULL COMMENT '员工姓名2' ;
ALTER TABLE employee MODIFY COLUMN employee_name varchar(30) DEFAULT 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 employeeG
*************************** 1. row ***************************
Field: employee_id
Type: int
Collation: NULL
Null: NO
Key:
Default: NULL
Extra:
Privileges: select,insert,update,references
Comment: 员工号
*************************** 2. row ***************************
Field: employee_name
Type: varchar(30)
Collation: utf8mb4_general_ci
Null: YES
Key:
Default: NULL
Extra:
Privileges: select,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;

原文始发于微信公众号(DBA闲思杂想录):MySQL如何查看/添加/修改表以及字段注释信息
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/227259.html