MySQL 表和列的註釋
像程式碼一樣,可以為表以及表中的列添加註釋,方便其他人知曉其功能。對於一些欄位,在經過一定時間後,建立者未必也能想起其具體的含意,所以註釋顯得尤為重要。
註釋的新增
註釋的新增是通過在定義表或列的時候在末尾加上COMMENT
關鍵字來實現的,最長支援 1024 個字元。
可以在建立表的時候為表和列新增相應的註釋。
CREATE TABLE test_comment ( idSERIAL PRIMARY KEY, col1 INT comment '列的註釋' ) comment '表的註釋';
執行上面的語句後建立了一個名為test_comment
的表,並且為表和其中的col1
列指定了相應的註釋。
然後可通過SHOW CREATE TABLE <table_name>
來檢視。
mysql> SHOW CREATE TABLE test_comment\G *************************** 1. row *************************** Table: test_comment Create Table: CREATE TABLE `test_comment` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `col1` int(11) DEFAULT NULL COMMENT '列的註釋', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='表的註釋' 1 row in set (0.00 sec)
註釋的檢視
除了SHOW CREATE TABLE <table_name>
語法,還有其他一些檢視註釋的方式。
SHOW TABLE STATUS
能夠查看錶的註釋,其語法為:
SHOW TABLE STATUS WHERE name='table_name';
以下是通過SHOW TABLE STATUS
檢視的結果:
mysql> SHOW TABLE STATUS WHERE name='test_comment'\G *************************** 1. row *************************** Name: test_comment Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 0 Avg_row_length: 0 Data_length: 16384 Max_data_length: 0 Index_length: 16384 Data_free: 0 Auto_increment: 1 Create_time: 2019-05-11 15:41:01 Update_time: NULL Check_time: NULL Collation: utf8mb4_general_ci Checksum: NULL Create_options: Comment: 表的註釋 1 row in set (0.00 sec)
而通過
SHOW FULL COLUMNS
則可檢視列的註釋,其語法為:
SHOW FULL COLUMNS FROM <tablename>
以下是通過SHOW FULL COLUMNS
檢視的結果:
mysql>SHOW FULL COLUMNS FROM test_comment\G *************************** 1. row *************************** Field: id Type: bigint(20) unsigned Collation: NULL Null: NO Key: PRI Default: NULL Extra: auto_increment Privileges: select,insert,update,references Comment: *************************** 2. row *************************** Field: col1 Type: int(11) Collation: NULL Null: YES Key: Default: NULL Extra: Privileges: select,insert,update,references Comment: 列的註釋 2 rows in set (0.00 sec)
藉助INFORMATION_SCHEMA 中的表 也能查看錶或列的註釋。
比如查看錶的註釋:
SELECT table_comment FROMinformation_schema.tables WHEREtable_name = 'test_comment';
執行結果:
mysql> SELECT table_comment -> FROMinformation_schema.tables -> WHEREtable_name = 'test_comment'; +---------------+ | TABLE_COMMENT | +---------------+ | 表的註釋| +---------------+ 1 row in set (0.01 sec)
檢視列的註釋:
SELECT column_comment FROMinformation_schema.columns WHEREcolumn_name = 'col1';
執行結果:
mysql> SELECT column_comment -> FROMinformation_schema.columns -> WHEREcolumn_name = 'col1'; +----------------+ | COLUMN_COMMENT | +----------------+ | 列的註釋| +----------------+ 1 row in set (0.00 sec)
註釋的更新
對已經存在的表和列,可通過相應的更新修改操作來添加註釋。
列註釋的新增,更新
CHANGE
和MODIFY
等效,區別在於CHANGE
重寫定義列,需要書寫完整的列定義,包括新的列名稱,即使你並不想修改列的免,而MODIFY
則不用指定新的列名稱。
通過CHANGE
語法:
mysql> ALTER TABLE test_comment CHANGE col1 col1 INT COMMENT '列的註釋2'; Query OK, 0 rows affected (0.02 sec) Records: 0Duplicates: 0Warnings: 0
通過MODIFY
語法:
mysql> ALTER TABLE test_comment MODIFY col1 INT COMMENT '列的註釋2'; Query OK, 0 rows affected (0.02 sec) Records: 0Duplicates: 0Warnings: 0
檢視修改結果:
mysql> SHOW CREATE TABLE test_comment\G *************************** 1. row *************************** Table: test_comment Create Table: CREATE TABLE `test_comment` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `col1` int(11) DEFAULT NULL COMMENT '列的註釋2', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='表的註釋' 1 row in set (0.00 sec)
表註釋的新增,更新
通過ALTER TABLE
來完成對錶註釋的新增和更新。
mysql> ALTER TABLE test_comment comment '表的註釋2'; Query OK, 0 rows affected (0.01 sec) Records: 0Duplicates: 0Warnings: 0
檢視更新結果:
mysql> SHOW CREATE TABLE test_comment\G *************************** 1. row *************************** Table: test_comment Create Table: CREATE TABLE `test_comment` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `col1` int(11) DEFAULT NULL COMMENT '列的註釋2', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='表的註釋2' 1 row in set (0.00 sec)
註釋的刪除
更新註釋時指定為空即可。
mysql> ALTER TABLE test_comment COMMENT ''; Query OK, 0 rows affected (0.01 sec) Records: 0Duplicates: 0Warnings: 0 mysql> ALTER TABLE test_comment MODIFY col1 INT COMMENT ''; Query OK, 0 rows affected (0.01 sec) Records: 0Duplicates: 0Warnings: 0
檢視刪除結果:
mysql> SHOW CREATE TABLE test_comment\G *************************** 1. row *************************** Table: test_comment Create Table: CREATE TABLE `test_comment` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `col1` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci 1 row in set (0.00 sec)