Implementation method of adding field annotation to MySQL create table

Time:2021-10-25

Direct Po codes and cases

#Write comments when creating tables
CREATE TABLE userinfo(
	Id int comment 'number',
	Uname varchar (40) comment 'user name',
	Address varchar (120) comment 'home address',
	Hobby varchar (200) comment 'hobby'
 
)Comment = 'user information table';
 
#Modify comments for tables
Alter table userinfo comment 'user information table';
 
#Modify the comments of the field. Note: the field name and field type are just as they are written
Alter table userinfo modify column uname varchar (40) comment 'name';
 
#The method of viewing table comments is viewed in the generated SQL statement
SHOW CREATE TABLE userinfo;
#Look in the metadata table
USE information_schema;
SELECT * FROM TABLES WHERE TABLE_SCHEMA='shoppingcart' AND TABLE_NAME='userinfo';
 
#How to view field comments
SHOW FULL COLUMNS FROM userinfo;
#Look in the metadata table
SELECT * FROM COLUMNS WHERE TABLE_SCHEMA='shoppingcart' AND TABLE_NAME='userinfo';

Add: MySQL adds fields and comments to existing data tables

Problem description
In the process of developing a system, we often encounter that with the expansion of system service functions, the association between the latter services needs to modify the original table structure appropriately, for example, add some necessary fields.

Example: in the existing device table device, add three fields: device IP, device name and device type.

Problem solving

Mode 1 (command line mode)
Using the MySQL command, execute the following commands in the MySQL client tool or command line:

ALTER TABLE device ADD COLUMN `device_ IP ` varchar (32) default null comment 'device IP';
ALTER TABLE device ADD COLUMN `device_ Name ` varchar (128) default null comment 'device name';
ALTER TABLE device ADD COLUMN `device_ Type ` varchar (32) default null comment 'device type';

perhaps

ALTER TABLE device ADD COLUMN 
(` device_ip ` varchar (32) default null comment 'device IP',
`device_ Name ` varchar (128) default null comment 'device name',
`device_ Type ` varchar (32) default null comment ');

Mode 2 (client tool mode)

Use MySQL visualization tools, such as sqlyog and Navicat.

Right click the table name — > select the design table — > Add field, fill in the corresponding attribute and save.

This is the end of this article about the implementation method of adding field comments to MySQL create tables. For more information about adding field comments to MySQL, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!