Home  >  Article  >  Database  >  How to add fields and comments in mysql

How to add fields and comments in mysql

WBOY
WBOYOriginal
2022-05-16 10:19:0923649browse

In mysql, you can use the ALTER TABLE statement to add fields and comments. This statement can add or modify a data table. The syntax is "ALTER TABLE table name ADD field name type COMMENT 'comment content';"; ADD is used For adding data, COMMENT is used to represent comment content.

How to add fields and comments in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to add fields and comments in mysql

Add field keyword alter Comment keyword comment

The role of ALTER TABLE

ALTER TABLE command is used to add, delete or change columns in existing data tables.

The syntax is

ALTER TABLE 表名 ADD 字段名 varchar(20) COMMENT '注释内容';

No comments, just remove the comment

Examples of adding fields are as follows:

alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认为空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;  //增加一个字段,默认不能为空

If the table has already been built, you can use The command to modify the field, plus the comment attribute.

For example:

alter table users modify name varchar(20)not null comment ‘用户名’;

Here users is the table name, name is the field name, varchar (20) is the type, and not null is the constraint.

Recommended learning: mysql video tutorial

The above is the detailed content of How to add fields and comments in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn