更改mysql表注释


35

我知道mysql 表注释可以在创建时定义为:

create table (...)comment='table_comment';

您可以通过以下方式显示评论:

show table status where name='table_name';

创建表注释后如何更改(更改?)表注释。我的意思是全部删除并重新创建表。

Answers:


38
DROP TABLE IF EXISTS test_comments;
Query OK, 0 rows affected (0.08 sec)

CREATE TABLE test_comments (ID INT, name CHAR(30)) COMMENT 'Hello World';
Query OK, 0 rows affected (0.22 sec)

检查表结构中的注释

show create table test_comments\G
*************************** 1. row ***************************
       Table: test_comments
Create Table: CREATE TABLE `test_comments` (
  `ID` int(11) DEFAULT NULL,
  `name` char(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Hello World'
1 row in set (0.00 sec)

您也可以像下面这样查看来自information_schema的评论

SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_NAME = 'test_comments';
+---------------+
| TABLE_COMMENT |
+---------------+
| Hello World   |
+---------------+
1 row in set (0.00 sec)

修改表以修改注释

ALTER TABLE test_comments COMMENT = 'This is just to test how to alter comments';
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

检查修改后的评论

show create table test_comments\G
*************************** 1. row ***************************
       Table: test_comments
Create Table: CREATE TABLE `test_comments` (
  `ID` int(11) DEFAULT NULL,
  `name` char(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='This is just to test how to alter comments'
1 row in set (0.00 sec)

SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_NAME = 'test_comments';
+--------------------------------------------+
| TABLE_COMMENT                              |
+--------------------------------------------+
| This is just to test how to alter comments |
+--------------------------------------------+
1 row in set (0.00 sec)

1
感谢您的详细解释,alter table修改评论正是我想要的
v14t 2014年

奖金的问题:它是安全的直接修改column_commentinformation_schema.columns (因为alter table ...需要指定所有列定义,再次)?
环Ø
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.