• 基于Centos7.6

在MySQL数据库中, 字段或列的注释是用属性comment来添加。

创建新表的脚本中, 可在字段定义脚本中添加comment属性来添加注释。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#建库

create databases school charset utf8;

#建表

CREATE TABLE stu(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学号', # NOT NULL PRIMARY KEY:主键非空 AUTO_INCREMENT:自动递增的唯一编号来标识记录。
sname VARCHAR(255) NOT NULL COMMENT '姓名',
sage TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '年龄', #DEFAULT 0:表示默认值为0,即当插入数据时如未指定该列的值,则该列的值就自动写入默认值。
sgender ENUM('m','f','n') NOT NULL DEFAULT 'n' COMMENT '性别' ,
sfz CHAR(18) NOT NULL UNIQUE COMMENT '身份证',
intime TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '入学时间'
) ENGINE=INNODB CHARSET=utf8 COMMENT '学生表'; #ENGINE=INNODB:存储引擎使用INNODB

#插入表数据

alter table stu add tel varchar(20) not null unique COMMENT '电话';

#查看已有表的所有字段

mysql> show full columns from stu;
+---------+---------------------+-----------------+------+-----+-------------------+----------------+---------------------------------+--------------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+---------+---------------------+-----------------+------+-----+-------------------+----------------+---------------------------------+--------------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | 学号 |
| sname | varchar(255) | utf8_general_ci | NO | | NULL | | select,insert,update,references | 姓名 |
| sage | tinyint(3) unsigned | NULL | NO | | 0 | | select,insert,update,references | 年龄 |
| sgender | enum('m','f','n') | utf8_general_ci | NO | | n | | select,insert,update,references | 性别 |
| sfz | char(18) | utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | 身份证 |
| intime | timestamp | NULL | NO | | CURRENT_TIMESTAMP | | select,insert,update,references | 入学时间 |
| tel | varchar(20) | utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | 电话 |
+---------+---------------------+-----------------+------+-----+-------------------+----------------+---------------------------------+--------------+
7 rows in set (0.00 sec)

#增加数据

mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'110','1');
Query OK, 1 row affected (0.01 sec)

mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'120','2');
Query OK, 1 row affected (0.00 sec)

mysql> insert into stu(sname,intime,tel,sfz) values('zs1',now(),'130','3');
Query OK, 1 row affected (0.00 sec)

#查看数据
mysql> select * from stu;
+----+-------+------+---------+-----+---------------------+-----+
| id | sname | sage | sgender | sfz | intime | tel |
+----+-------+------+---------+-----+---------------------+-----+
| 1 | zs1 | 0 | n | 1 | 2021-09-26 14:07:35 | 110 |
| 2 | zs1 | 0 | n | 2 | 2021-09-26 14:07:42 | 120 |
| 3 | zs1 | 0 | n | 3 | 2021-09-26 14:07:48 | 130 |
+----+-------+------+---------+-----+---------------------+-----+
3 rows in set (0.00 sec)

#查看执行计划(没有此数据为NULL
mysql> desc select * from stu where tel='160';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
1 row in set, 1 warning (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
create table test( 
id int not null default 0 comment '用户id'
)

#如果是已经建好的表, 也可以用修改字段的命令,然后加上comment属性定义,就可以添加上注释了
alter table test modify column id int not null default 0 comment '测试表id'

#查看已有表的所有字段
show full columns from test;

#创建表的时候写注释
create table test1 (
field_name int comment '字段的注释'
)comment='表的注释';

#修改表的注释
alter table test1 comment '修改后的表的注释';

#修改字段的注释
alter table test1 modify column field_name int comment '修改后的字段注释';

-- 注意:字段名和字段类型照写就行

#查看表注释的方法
-- 在生成的SQL语句中看
show create table test1;
-- 在元数据的表里面看
use information_schema;
select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

#查看字段注释的方法
-- show
show full columns from test1;
-- 在元数据的表里面看
select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G