MySQL 表和列的註釋
阿新 • • 發佈:2019-05-12
像程式碼一樣,可以為表以及表中的列添加註釋,方便其他人知曉其功能。對於一些欄位,在經過一定時間後,建立者未必也能想起其具體的含意,所以註釋顯得尤為重要。 註釋的新增註釋的新增是通過在定義表或列的時候在末尾加上 可以在建立表的時候為表和列新增相應的註釋。 CREATE TABLE test_comment ( id SERIAL PRIMARY KEY, col1 INT comment '列的註釋' ) comment '表的註釋'; 執行上面的語句後建立了一個名為 col1 列指定了相應的註釋。
然後可通過 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 TABLE STATUS WHERE name='table_name'; 以下是通過 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 FROM <tablename> 以下是通過 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 FROM information_schema.tables WHERE table_name = 'test_comment'; 執行結果: mysql> SELECT table_comment -> FROM information_schema.tables -> WHERE table_name = 'test_comment'; +---------------+ | TABLE_COMMENT | +---------------+ | 表的註釋 | +---------------+ 1 row in set (0.01 sec) 檢視列的註釋: SELECT column_comment FROM information_schema.columns WHERE column_name = 'col1'; 執行結果: mysql> SELECT column_comment -> FROM information_schema.columns -> WHERE column_name = 'col1'; +----------------+ | COLUMN_COMMENT | +----------------+ | 列的註釋 | +----------------+ 1 row in set (0.00 sec) 註釋的更新對已經存在的表和列,可通過相應的更新修改操作來添加註釋。 列註釋的新增,更新
通過 mysql> ALTER TABLE test_comment CHANGE col1 col1 INT COMMENT '列的註釋2'; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 通過 mysql> ALTER TABLE test_comment MODIFY col1 INT COMMENT '列的註釋2'; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 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) 表註釋的新增,更新通過 mysql> ALTER TABLE test_comment comment '表的註釋2'; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 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: 0 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE test_comment MODIFY col1 INT COMMENT ''; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 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) 相關資源
|