如何使用CREATE TABLE和SELECT语句设置列的注释

时间:2014-05-22 04:50:52

标签: mysql

我想在MySQL中使用CREATE TABLE和SELECT语句设置列的注释(MySQL版本为5.1.69。)。
我尝试以下查询。但是没有设置专栏评论。

CREATE TABLE t (
  parent_id INT(10) NULL COMMENT 'test'
) ENGINE=INNODB 
SELECT 1 AS parent_id;

SHOW FULL COLUMNS FROM t;

+-----------+---------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Field     | Type    | Collation | Null | Key | Default | Extra | Privileges                      | Comment |
+-----------+---------+-----------+------+-----+---------+-------+---------------------------------+---------+
| parent_id | int(10) | NULL      | YES  |     | NULL    |       | select,insert,update,references |         |
+-----------+---------+-----------+------+-----+---------+-------+---------------------------------+---------+
1 row in set (0.00 sec)

如何使用CREATE TABLE和SELECT语句设置列的注释。

2 个答案:

答案 0 :(得分:1)

使用CREATE TABLE .... SELECT的自定义评论似乎无法实现。

CREATE TABLE ... SELECT上的文档仅指定保留父注释。如果使用create ... select语法定义内联新注释,则没有提及任何内容,行为是什么。

  

重新定位的属性为NULL(或NOT NULL),对于包含这些属性的列,CHARACTER SET, COLLATION, COMMENTDEFAULT子句 < / p>

以下观察结果表明,在新列上尝试的新自定义comment将被忽略。

如果您仍想重新定义自己的注释,则必须使用alter table命令添加新创建的表列。

示例

mysql> create table tbl_so_q23798048_1( i int not null comment 'parent comment' );
Query OK, 0 rows affected (0.41 sec)

mysql> select table_name, column_name, column_comment
    ->   from information_schema.columns
    ->  where table_schema='so' and length(column_comment)>0;
+--------------------+-------------+-----------------+
| table_name         | column_name | column_comment  |
+--------------------+-------------+-----------------+
| tbl_so_q23798048_1 | i           | parent comment  |
+--------------------+-------------+-----------------+
1 row in set (0.01 sec)

现在,尝试根据以前创建的表创建一个表,但使用自定义注释。

mysql> create table tbl_so_q23798048_2( i int comment 'custom comment' )
    ->     as select i from tbl_so_q23798048_1;
Query OK, 0 rows affected (0.42 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select table_name, column_name, column_comment
    ->   from information_schema.columns
    ->  where table_schema='so' and length(column_comment)>0;
+--------------------+-------------+-----------------+
| table_name         | column_name | column_comment  |
+--------------------+-------------+-----------------+
| tbl_so_q23798048_1 | i           | parent comment  |
| tbl_so_q23798048_2 | i           | parent comment  |
+--------------------+-------------+-----------------+
2 rows in set (0.01 sec)

您可以清楚地看到自定义评论被忽略。

现在,请尝试在新表的列中没有自定义注释。

mysql> create table tbl_so_q23798048_3( i int )
    ->     as select i from tbl_so_q23798048_1;
Query OK, 0 rows affected (0.35 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select table_name, column_name, column_comment
    ->   from information_schema.columns
    ->  where table_schema='so' and length(column_comment)>0;
+--------------------+-------------+-----------------+
| table_name         | column_name | column_comment  |
+--------------------+-------------+-----------------+
| tbl_so_q23798048_1 | i           | parent comment  |
| tbl_so_q23798048_2 | i           | parent comment  |
| tbl_so_q23798048_3 | i           | parent comment  |
+--------------------+-------------+-----------------+
3 rows in set (0.01 sec)

您可以看到保留了父评论。 现在,您可以更改新表格的列以添加自定义评论。

mysql> alter table tbl_so_q23798048_3
    ->       modify column i int comment 'custom comment';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select table_name, column_name, column_comment
    ->   from information_schema.columns
    ->  where table_schema='so' and length(column_comment)>0;
+--------------------+-------------+-----------------+
| table_name         | column_name | column_comment  |
+--------------------+-------------+-----------------+
| tbl_so_q23798048_1 | i           | parent comment  |
| tbl_so_q23798048_2 | i           | parent comment  |
| tbl_so_q23798048_3 | i           | custom comment  |
+--------------------+-------------+-----------------+
3 rows in set (0.01 sec)

而且,虽然选中的表格列上没有comment,但您无法定义新评论。

mysql> create table tbl_so_q23798048_4( i int );
Query OK, 0 rows affected (0.68 sec)

mysql> create table tbl_so_q23798048_5( i int comment 'new comment' )
    -> as select i from tbl_so_q23798048_4;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select table_name, column_name, column_comment
    ->   from information_schema.columns
    ->  where table_schema='so' and length(column_comment)>0
    ->    and table_name in ( 'tbl_so_q23798048_4', 'tbl_so_q23798048_5' );
Empty set (0.01 sec)

答案 1 :(得分:0)

SELECT 1 AS parent_id;查询中删除create table。在这里查看演示小提琴http://sqlfiddle.com/#!2/cc46e1/1

您的CREATE TABLE查询应该看起来像

CREATE TABLE t (
  parent_id INT(10) NULL COMMENT 'test'
) ENGINE=INNODB 

修改

是的,您可以,但如果您要在CREATE TABLE AS SELECT ...示例中定义新列,如下所示。如果你这样说,那么评论将与原始表格相同

CREATE TABLE t1
as
select parent_id from t;

但是如果您定义了一个新列,那么您将获得一个新的列注释。请在此处查看更新小提琴http://sqlfiddle.com/#!2/44567/1

CREATE TABLE t2(id int null comment 'NEW TEST')
as
select parent_id from t;

enter image description here