MySQL Append comment or bulk edit comment

Source: Internet
Author: User

MySQL Append comment or a lot of changes comment 2016-01-25 20:28:05

Category: MySQL

MySQL 5.6.14

The previous project was rather hasty, with no comments on the built-in statements.
Now you want to complete the annotation information.
But MySQL late append comment is more troublesome
You need to use the Modify syntax.

As long as you accidentally write a wrong point, it can lead to a change in the table structure, not a comment.

The experimental table is as follows:
  1. Create table T (
  2. C1 int primary key auto_increment,
  3. C2 char (a) not null default ' C2 ' comment ' c2 comment ',
  4. C3 Date default ' 2016-01-25 ' comment ' date type test ',
  5. C4 varchar (+) not null default ' ',
  6. C5 bigint,
  7. C6 text comment ' text test ',
  8. C7 timestamp not null The default current_timestamp on update current_timestamp,
  9. C8 datetime not null default Now ()
  10. );


Through the following SQL, parsing meta-data information, you can directly display the contents of the Modify.
After you append or modify a comment, execute the statement.
This can avoid human error.
  1. SELECT
  2. Concat
  3. ' ALTER TABLE ',
  4. Table_schema, '. ', TABLE_NAME,
  5. ' Modify column ', column_name, ', Column_type, ',
  6. if (is_nullable = ' YES ', ' ', ' not null '),
  7. if (Column_default is NULL, ",
  8. If
  9. Data_type in (' char ', ' varchar ')
  10. OR
  11. Data_type in (' Date ', ' datetime ', ' timestamp ') and column_default! = ' Current_timestamp ',
  12. Concat (' default ', Column_default,'),
  13. Concat (' default ', Column_default)
  14. )
  15. ),
  16. if (extra is null or Extra=",", Concat (", extra)),
  17. ' comment ', column_comment, ' ; '
  18. ) s
  19. From Information_schema.columns
  20. WHERE table_schema = ' Test '
  21. and table_name = ' t '


For an example of an experimental table, the resulting modify statement is as follows.

  1. Alter table TEST.T modify column C1 int (one) not null auto_increment comment ';
  2. Alter table TEST.T modify column C2 char (a) not null default ' C2 ' comment ' C2 ' comment ';
  3. Alter table TEST.T modify column C3 Date default ' 2016-01-25 ' comment ' date type test ';
  4. Alter table TEST.T modify column C4 varchar (a) not null default ' comment ';
  5. Alter table TEST.T modify column C5 bigint (comment ") ;
  6. Alter table TEST.T modify column C6 text comment ' text test ';
  7. Alter table TEST.T modify column C7 timestamp not null default current_timestamp on update current_timestamp comment ";
  8. Alter table TEST.T modify column C8 datetime not null default current_timestamp comment '  ‘;

MySQL Append comment or bulk edit comment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.