MySQL 表和列的注释
2019-05-12 00:20
549 查看
像代码一样,可以为表以及表中的列添加注释,方便其他人知晓其功能。对于一些字段,在经过一定时间后,创建者未必也能想起其具体的含意,所以注释显得尤为重要。 注释的添加注释的添加是通过在定义表或列的时候在末尾加上 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_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> 以下是通过 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 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: 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) 相关资源 |
相关文章推荐
- Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结
- MySQL my.cnf 配置文件注释
- MySQL注释符号
- mysql通过字段注释查找字段名称
- mysql写注释的几种方法
- PowerDesigner 的mysql PDM 的COMMENT注释
- Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结
- mysql查看表注释和字段注释的方法
- powerdesigner中使用mysql逆向工程生成pdm,附带解决注释乱码
- MySql--三种注释写法
- Mysql 将表结构导入 Powerdesigner ,并将注释作为 name 列
- java读取mysql表的注释及字段注释
- MySql 存储过程实例(附完整注释)
- hive metadata 存mysql 注释中文乱码的有关
- MySQL的SQL语句注释的应用
- 将脚本文件导入到mySql中,表中字段的中文注释变为乱码了
- 如何在MySQL存储过程中保留注释
- 定义不对domain类的hashCode和domain的PO注释引起的mysql内存泄露
- Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)
- MySql 存储过程实战(附完整注释)