Mysql--表注释,字段注释

information_schema数据库是MySQL数据库自带的数据库,里面存放的MySQL数据库所有的信息,包括数据表、数据注释、数据表的索引、数据库的权限等等。

1、添加表、字段注释

create table 表名
(
  cluster_id varchar(40) NOT NULL COMMENT '''集群id''',
  cluster_name varchar(100) NOT NULL COMMENT '''集群名''',
  PRIMARY KEY (`cluster_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT = '集群信息';

 

2、修改表注释

alter table 表名 COMMENT '集群信息2';

 

3、修改字段注释(修改时要把该字段的完整定义写上,如字段类型、属性等,不能因修改注释,而把该字段定义的其它属性给覆盖掉了)

alter table 表名 modify column 字段名 varchar(40) NOT NULL COMMENT '''集群id2''',

 

4、查询

#查看单个表的字段注释
show full columns from 表名;

#查看所有表的表注释
select table_name 表名,
table_comment 表注释 
from information_schema.tables 
where table_schema = '数据库的名称';

#查看表的所有字段注释
select column_name 字段名,
column_comment 字段注释,
column_type 字段类型,
column_key 约束 
from information_schema.columns 
where table_schema='数据库的名称' and table_name='表名';

#查看所有表的表注释及字段注释
SELECT
a.table_name 表名,
a.table_comment 表说明,
b.COLUMN_NAME 字段名,
b.column_comment 字段说明,
b.column_type 字段类型,
b.column_key 约束
FROM
information_schema. TABLES a
LEFT JOIN information_schema. COLUMNS b ON a.table_name = b.TABLE_NAME
WHERE
a.table_schema = '数据库的名称'
ORDER BY
a.table_name

 

posted @ 2022-05-30 19:15  心恩惠动  阅读(986)  评论(0编辑  收藏  举报