[英]How to set column's comment with CREATE TABLE and SELECT statement
I want to set column's comment with CREATE TABLE and SELECT statement in MySQL(MySQL version is 5.1.69.). 我想在MySQL(MySQL版本为5.1.69。)中使用CREATE TABLE和SELECT语句设置列的注释。
I try the following query. 我尝试以下查询。 However column's comment is not set. 但是,未设置列的注释。
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)
How do I set column's comment with CREATE TABLE and SELECT statement. 如何使用CREATE TABLE和SELECT语句设置列的注释。
Custom comment using CREATE TABLE .... SELECT
seems not possible. 使用CREATE TABLE .... SELECT
自定义注释似乎不可能。
Documentation on CREATE TABLE ... SELECT
does only specify that the parent comments are retained. 有关CREATE TABLE ... SELECT
文档仅指定保留父注释。 It did not mention anything if an in line new comment is defined using create ... select
syntax, what the behaviour is. 如果使用create ... select
语法定义了行内新注释,则它什么也没有提及。
Retrained attributes are
NULL
(orNOT NULL
) and, for those columns that have them,CHARACTER SET, COLLATION, COMMENT
, and theDEFAULT
clause 重新训练的属性为NULL
(或NOT NULL
),对于具有它们的那些列,则为CHARACTER SET, COLLATION, COMMENT
和DEFAULT
子句
Following observations say that that the new custom comment
being tried on new column is ignored. 以下观察结果表明,将忽略在新列上尝试的新自定义comment
。
And if you still want to re-define your own comment, you have to use alter table
command to add on the newly created table column. 如果仍然要重新定义自己的注释,则必须使用alter table
命令在新创建的表列上添加。
Example : 范例 :
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)
Now, try to create a table based on the previously created table but with custom comment. 现在,尝试基于先前创建的表创建一个表,但带有自定义注释。
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)
You can clearly see that custom comment is ignored. 您可以清楚地看到自定义注释将被忽略。
Now, try with no custom comment on the new table's column. 现在,尝试在新表的列上没有自定义注释。
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)
You can see that parent comment is retained. 您可以看到保留了父注释。 Now, you can alter the new table's column to add custom comment. 现在,您可以更改新表的列以添加自定义注释。
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)
And, you can't define new comment though the table column being selected does not have a comment
on it. 而且,虽然被选择的表列没有一个你无法定义新的评论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)
Remove that SELECT 1 AS parent_id;
删除该SELECT 1 AS parent_id;
from create table
query. 从create table
查询。 See a demo fiddle here http://sqlfiddle.com/#!2/cc46e1/1 在此处查看演示小提琴http://sqlfiddle.com/#!2/cc46e1/1
your CREATE TABLE
query should look like 您的CREATE TABLE
查询应该看起来像
CREATE TABLE t (
parent_id INT(10) NULL COMMENT 'test'
) ENGINE=INNODB
EDIT: 编辑:
Yes you can but if you are defining a new column in CREATE TABLE AS SELECT ...
example as below. 是的,可以,但是如果您要在CREATE TABLE AS SELECT ...
示例中定义一个新列,如下所示。 if you say this then comment will be same as original table 如果您说这个,那么注释将与原始表相同
CREATE TABLE t1
as
select parent_id from t;
But if you define a new column then you will get a new column comment. 但是,如果您定义一个新列,那么您将获得一个新列注释。 See a update fiddle here http://sqlfiddle.com/#!2/44567/1 在这里查看更新小提琴http://sqlfiddle.com/#!2/44567/1
CREATE TABLE t2(id int null comment 'NEW TEST')
as
select parent_id from t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.