MySQL 表和列的注释
| 像代碼一樣,可以為表以及表中的列添加注釋,方便其他人知曉其功能。對于一些字段,在經過一定時間后,創建者未必也能想起其具體的含意,所以注釋顯得尤為重要。 注釋的添加注釋的添加是通過在定義表或列的時候在末尾加上 COMMENT 關鍵字來實現的,最長支持 1024 個字符。 可以在創建表的時候為表和列添加相應的注釋。 CREATE TABLE test_comment ( id SERIAL 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_commentEngine: InnoDBVersion: 10Row_format: DynamicRows: 0Avg_row_length: 0Data_length: 16384 Max_data_length: 0Index_length: 16384Data_free: 0Auto_increment: 1Create_time: 2019-05-11 15:41:01Update_time: NULLCheck_time: NULLCollation: utf8mb4_general_ciChecksum: NULLCreate_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: idType: bigint(20) unsignedCollation: NULLNull: NOKey: PRIDefault: NULLExtra: auto_increment Privileges: select,insert,update,referencesComment: *************************** 2. row ***************************Field: col1Type: int(11)Collation: NULLNull: YESKey:Default: NULLExtra: Privileges: select,insert,update,referencesComment: 列的注釋 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)注釋的更新對已經存在的表和列,可通過相應的更新修改操作來添加注釋。 列注釋的添加,更新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: 0 Duplicates: 0 Warnings: 0通過 MODIFY 語法: 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)表注釋的添加,更新通過 ALTER TABLE 來完成對表注釋的添加和更新。 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: 0mysql> 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)相關資源
|
轉載于:https://www.cnblogs.com/Wayou/p/mysql_comments_for_table_and_column.html
總結
以上是生活随笔為你收集整理的MySQL 表和列的注释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记02:直播串讲02
- 下一篇: Python3.WRF的投影转换