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_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
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)

注释的更新

对已经存在的表和列,可通过相应的更新修改操作来添加注释。

列注释的添加,更新

CHANGEMODIFY 等效,区别在于 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)

相关资源

原文地址:https://www.cnblogs.com/Wayou/p/mysql_comments_for_table_and_column.html

时间: 2024-08-29 14:10:22

MySQL 表和列的注释的相关文章

ORACLE中给表、列增加注释以及读取注释

在ORACLE中给表.列增加注释以及读取注释 1.给表填加注释:SQL>comment on table 表名 is '表注释"; 2.给列加注释:SQL>comment on column 表.列 is '列注释'; 3.读取表注释:SQL>select * from user_tab_comments where comments is not null; 4.读取列注释:SQL>select * from user_col_commnents where comme

SQL_为表和列加注释

***********************************************声明***********************************************************************  原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/39755221 *******

oracle给表及列添加注释

给表添加注释 comment on   table    MW_SYS.MWT_OM_OBJ1 is '业务类型的对象实例.'; 给表中的列添加注释 comment on column MW_SYS.MWT_OM_OBJ1.OBJ_ID is  '对象的唯一标示符.'; comment on column MW_SYS.MWT_OM_OBJ1.OBJ_LOCALID is '对象本地标识.'; comment on column MW_SYS.MWT_OM_OBJ1.OBJ_CAPTION is

解决CodeSmith无法获取MySQL表及列Description说明注释的方案

public ExtendedProperty[] GetExtendedProperties(string connectionString, SchemaObjectBase schemaObject) { List<ExtendedProperty> extendedProperties = new List<ExtendedProperty>(); if (schemaObject is ColumnSchema) { ColumnSchema columnSchema =

完美解决CodeSmith无法获取MySQL表及列Description说明注释的方案

全部代码如下: public ExtendedProperty[] GetExtendedProperties(string connectionString, SchemaObjectBase schemaObject) { List<ExtendedProperty> extendedProperties = new List<ExtendedProperty>(); if (schemaObject is ColumnSchema) { ColumnSchema column

MYSQL表根据列拆分的问题

前言:项目中有这么一个需求,对一张已经成型的表拆分出一张子表,用来保存原来表的大字段的问题.要求子表的一列是主表的主键. 数据库:MYSQL 案例: 主表:user 字段: 图1 子表:user_copy 字段:id,zid,password (user_copy.zid = user.id) 如图: 主表: 图2 操作: 1.复制主表2个字段,id和password,并修改字段id为zid 图3 2.取消zid自增,不取消会在新主键id继续增加:如图: 图4 取消zid主键自增后保存: 在zi

Oracle表、列、约束的操作

获得有关表的信息 可以直接DESCRIBE DESC[RIBE] table_name; 可以通过数据字典 SELECT * FROM user_tables WHERE table_name =xxxx; [or WHERE table_name IN ('xxxx','xxxx')] 重命名表 RENAME table_old_name TO table_new_name; 截断表 删除表中的所有行,并重置表的存储空间,表的表名列属性等会保留.  TRUNCATE table_name; 

快速创建和mysql表对应的java domain实体类

今天创建了一个表有十几个字段,创建完之后必然要写一个与之对应的java domain实体类.这不是重复的工作吗?为什么不先把这个表的所有的字段查出来,然后放到linux环境下,用sed工具在每一行的前面加上"private String ",每一行的后面添加";".这样可以节省很多重复工作.下面上sql代码和sed命令. 查询一个mysql表所有列的列名的sql代码如下: SELECT COLUMN_NAME FROM information_schema.COLU

EF连接Mysql 表&#39;TableDetails&#39;中的列&#39;IsPrimaryKey&#39;的值为DBNull

无法生成模型,因为存在以下异常:'System.Data.StrongTypingException:表'TableDetails'中的列'IsPrimaryKey'的值为DBNull.---> System.InvalidCastException:指定的转换无效. 原文链接http://stackoverflow.com/questions/33575109/mysql-entity-the-value-for-column-isprimarykey-in-table-tabledetail