Blog

It's a Wonderful Life

MySQL comments

2019-05-14 20:00 Posted in Learn with MySQL

In MySQL, you can add comments to a data column or a row by using comment directive.

Adding comments along with table’s creation:

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

If the table was already created, you can also use table alteration directives to add comment properties later on:

alter table test change column id id int not null default 0 comment 'testing table id'

To check all of the comment properties within a table:

show full columns from test;

Add comment to a table directly:

create table test1 ( 
    field_name int comment 'field comment' 
)comment='table comment'; 

Change comment of a table:

alter table test1 comment 'changed comment';

Change comment of a field:

alter table test1 modify column field_name int comment 'changed comment';

To show a table’s comment:

--show in table creation directives
    show  create  table  test1; 
--show in table's metadata
    use information_schema; 
    select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

To show fields’ comment:

--show 
    show  full  columns  from  test1; 
--show in table's metadata
    select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G