2014-05-22 111 views
1

我想在MySQL中使用CREATE TABLE和SELECT語句來設置列的註釋(MySQL版本爲5.1.69)。
我嘗試下面的查詢。但是,列的評論未設置。如何使用CREATE TABLE和SELECT語句設置列的註釋

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語句設置列的註釋。

+0

SELECT語句與您的表創建有什麼關係?爲什麼它很重要/它有什麼意義? –

+0

實際上,它是SELECT語句的一部分,它添加了插入與其他表的條件匹配的記錄。 – hiro

回答

1

使用CREATE TABLE .... SELECT自定義評論似乎是不可能的。

關於CREATE TABLE ... SELECT的文檔僅指定保留父註釋。它沒有提到如果使用create ... select語法定義了新的評論,那麼行爲是什麼。

再培訓屬性NULL(或NOT NULL),對於擁有它們,CHARACTER SET, COLLATION, COMMENT那些列,以及DEFAULT條款

以下意見說,新的自定義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) 
+0

謝謝。我瞭解'CREATE TABLE ... SELECT'的行爲。我將用'ALTER TABLE'重新定義列註釋。 – hiro

0

create table刪除那SELECT 1 AS parent_id;查詢。看到這裏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

+0

謝謝。用'CREATE TABLE ... SELECT'語句設置列的註釋是不可能的? – hiro

+0

@hiro,是的,你可以。見編輯的答案。如果有幫助,不要忘記接受答案。 – Rahul

+0

再次感謝您的答案。我理解得很好。 – hiro

相關問題