Home > Database > Mysql Tutorial > body text

How to add comments to columns (fields) in mysql

青灯夜游
Release: 2022-06-15 19:12:13
Original
21005 people have browsed it

Two adding methods: 1. Use the "CREATE TABLE" statement and the comment keyword to comment the column when creating the table, the syntax is "create table name (column name field type comment 'column comment content') ;"; 2. Use the "ALTER TABLE" statement and the comment keyword to comment the column when modifying the table. The syntax is "alter table table name modify column column name data type comment 'column comment content';".

How to add comments to columns (fields) in mysql

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

Mysql method of adding comments to columns (fields)

In the MySQL database, comments on fields or columns are added using the attribute comment . There are two ways to add.

1. Use the CREATE TABLE statement and the comment keyword to comment the columns when creating the table

Syntax:

create 表名 ( 
    列名 字段类型 comment '列的注释内容'
);
Copy after login

Example: Create the users table , there is an id field in it, add a comment to the field

create table users(id int(11) primary key comment '用户id');
Copy after login

How to add comments to columns (fields) in mysql

## After the addition is completed, look at the field comment:


show full columns from users;
Copy after login
Copy after login

How to add comments to columns (fields) in mysql

2. Use the ALTER TABLE statement and comment keyword to comment the columns when modifying the table

Syntax:

alter table 表名 modify column 列名 数据类型 comment '列的注释内容'; 

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

Example:

In the users table, add a name field

ALTER TABLE users ADD name varchar(20);
Copy after login

How to add comments to columns (fields) in mysql

View the table structure

How to add comments to columns (fields) in mysql

Add comments to the name field

alter table users modify name varchar(20) not null comment '用户名';
Copy after login

How to add comments to columns (fields) in mysql

After the addition is completed, take a look at the field comments:


show full columns from users;
Copy after login
Copy after login

How to add comments to columns (fields) in mysql

[Related recommendations:

mysql Video tutorial

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!