Use MySQL statements to add comments to the tables and fields of the database

Add comments to MySQL statements

​ MySQL uses "comment" to add comments to database tables and fields. The following are several situations for adding comments:

One, add notes to the table

​ 1) Add a comment when creating the table

CREATE TABLE table2 (
	tname INT COMMENT 'tname字段的注释'
) COMMENT = 'table2表的注释';

​ 2) Add a comment after creating the table

ALTER TABLE table2 COMMENT '修改表table2的注释';

Two, add notes to the field

​ 1) Add notes to the fields in the table when creating a new table

CREATE TABLE table1 (
	tid INT NOT NULL DEFAULT 0 COMMENT '表的id'
)

​ 2) After creating the table, modify a field in the table and add a comment——CHANGE

ALTER TABLE table1 
CHANGE COLUMN tid id INT NOT NULL DEFAULT 0 COMMENT '修改表的id'  ## tid是旧的id字段名,id是修改后新的字段名

Tips: Here you can view the added comment information with one line of command (you can also view it in the Navicat client)

​ Command view:

SHOW FULL COLUMNS FROM table1;

​ Navicat client view

Insert picture description here

3) After creating the table, modify a field in the table and add a comment——MODIFY

ALTER TABLE table2 MODIFY COLUMN tname VARCHAR(20) COMMENT '修改后的tname字段的注释';

Insert picture description here


Description:MODIFY和CHANGEBoth can modify the fields in the table, there is a difference between the two:

  1. ​ MODIFY is to modify the type of the field ( INT is changed to VARCHAR(20) )

  2. ​ CHANGE is to modify the name of the field (the front tid is changed to id )

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/106474103