目录
查看已有表的所有字段的注释
创建表的时候写注释
修改表的注释
修改字段的注释
查看表注释的方法
查看字段注释的方法
在MySQL数据库中, 表的注释是极为重要的,那么字段或列的注释是用属性 comment 来添加。
创建新表的脚本中, 可在字段定义脚本中添加 comment 属性来添加注释。
示例如下:
create table server_load(
id int not null default 0 comment '用户id'
)
![](https://img-blog.csdnimg.cn/20200922163550539.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jidmp4MTMxNA==,size_16,color_FFFFFF,t_70)
如果是已经建好的表, 也可以用修改字段的命令,然后加上comment属性定义,就可以添加上注释了。
示例如下:
-- 修改字段的命令,然后加上comment属性定义 以用来添加注释
alter table server_load modify column id int not null default 0 comment '低运行节点id'
![](https://img-blog.csdnimg.cn/20200922163847511.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jidmp4MTMxNA==,size_16,color_FFFFFF,t_70)
新增一个字段 并添加注释 (可 批量 )
#新增一个字段
alter table 表名 add COLUMN 字段名 类型长度 DEFAULT NULL COMMENT '注释内容';
#例如:
alter table device_log_run_operation add COLUMN parser_status VARCHAR(4) DEFAULT NULL COMMENT '解析文件状态,0:解析成功;1:解析失败;';
#批量新增字段,方法一
#事务开始
begin;
alter table device_log_run_operation add COLUMN title VARCHAR(500) DEFAULT NULL COMMENT '日志标题';
alter table device_log_run_operation add COLUMN remote_addr VARCHAR(255) NOT NULL COMMENT '操作ip地址';
commit;
#批量新增字段,提交事务,事务结束
#批量新增字段,方法二
alter table 表名 add (字段名1 类型(长度),字段名2 类型(长度),字段名3 类型(长度));
#例如:
alter table device_log_run_operation
add (
status int(11) DEFAULT NULL COMMENT '状态:0-成功;1-失败',
remote_addrss VARCHAR(255) NOT NULL COMMENT '操作的ip地址',
insert_times datetime DEFAULT NULL COMMENT '创建时间'
);
查看已有表的所有字段的注释
可以用命令:show full columns from table 来查看, 示例如下:
show full columns from server_load;
创建表的时候写注释
create table test2(
field_name int comment '字段的注释'
)comment='表的注释';
![](https://img-blog.csdnimg.cn/20200922164245532.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jidmp4MTMxNA==,size_16,color_FFFFFF,t_70)
修改表的注释
alter table teset2 comment '修改后的表的注释';
// 为表添加注释
ALTER TABLE `student` COMMENT'学生表11111111111119999999999999999999999';
![](https://img-blog.csdnimg.cn/20200922164412657.png)
修改字段的注释
alter table test2 modify column field_name int comment '修改后的字段注释3333';
-- 注意:字段名和字段类型 照写
// 为字段添加注释 varchar(255) 类型
ALTER TABLE student MODIFY COLUMN name varchar(10) DEFAULT 1 COMMENT '所有人的姓名';
![](https://img-blog.csdnimg.cn/20200922164524502.png)
查看表注释的方法
-- 在生成的SQL语句中看
show create table test2;
-- 在元数据的表里面看
use information_schema;
select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test2' \G
![](https://img-blog.csdnimg.cn/20200922164623730.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jidmp4MTMxNA==,size_16,color_FFFFFF,t_70)
查看字段注释的方法
-- show
show full columns from test2;
-- 在元数据的表里面看
select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test2' \G
![](https://img-blog.csdnimg.cn/20200922164707501.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Jidmp4MTMxNA==,size_16,color_FFFFFF,t_70)
|