使用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)
SELECT語句與您的表創建有什麼關係?爲什麼它很重要/它有什麼意義? –
實際上,它是SELECT語句的一部分,它添加了插入與其他表的條件匹配的記錄。 – hiro