Home  >  Article  >  Database  >  How to add comments to tables in Oracle and MySQL

How to add comments to tables in Oracle and MySQL

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-22 09:12:483624browse

In MySQL and Oracle databases, comments on fields or columns are added using the attribute comment. In the script that creates a new table, you can add comments by adding the comment attribute in the field definition script. The following will introduce the method of adding comments. You can refer to it if necessary.

How to add comments to tables in Oracle and MySQL

Oracle adds a comment

In Oracle database, the comment of a field or column is Use the attribute comment to add it.

1. Add a comment to the table:

comment on table The table name is 'comment content of the table';

The example code is as follows:

 comment on table user is '用户表';

2. Add comments to the fields of the table:

comment on column table name. Field name is 'field comment';

The example code is as follows:

comment on column user.username is '用户表中的用户名字段';

MySQL adds comments (comment)

In the MySQL database, comments on fields or columns are also added using the attribute comment.

1. In the script to create a new table, you can add comments by adding the comment attribute in the field definition script.

The example code is as follows:

create table test( 
    id int not null default 0 comment '用户id' )

2. If the table has been created, you can also use the command to modify the fields, and then add the comment attribute definition , you can add comments

The example code is as follows:

alter table test 
change column id id int not null default 0 comment '测试表id'

How to view the comments of all fields of the existing table?

You can use the command: show full columns from table to view, the example is as follows:

show full columns from test;

Write comments when creating the table

create table test1 ( 
    field_name int comment '字段的注释' 
)comment='表的注释';

Modify table comments

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

Modify field comments

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

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

How to view table comments

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

How to view field comments

--show 
    show  full  columns  from  test1; 
--在元数据的表里面看 
    select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

Related recommendations: "mysql tutorial"

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

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete