原 所有数据库查询表和列的注释SQL汇总(Oracle、MySQL、PostgreSQL、GreenPlum、SQL Server)
Tags: Oracle原创PGGreenPlumMSSQLSQL ServerPostgreSQLMySQL注释
Oracle
参考:https://www.dbaup.com/oraclezhongduibiaohelietianjiazhushijichaxunzhushi.html
查询表的注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | SELECT D.OWNER, D.TABLE_NAME, D.COMMENTS FROM DBA_TAB_COMMENTS D WHERE D.OWNER = 'LHR' AND D.COMMENTS IS NOT NULL; SELECT table_name, comments AS table_comment FROM all_tab_comments WHERE table_name = 'your_table_name' -- 替换为你的表名称 AND owner = 'your_schema_name'; -- 替换为你的模式名称 |
查询列的注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | SELECT D.OWNER, D.TABLE_NAME,D.COLUMN_NAME, D.COMMENTS FROM DBA_COL_COMMENTS D WHERE D.OWNER = 'LHR' AND D.TABLE_NAME='CMMND_INFO_HSTRY' AND D.COMMENTS IS NOT NULL; SELECT table_name, column_name, comments AS column_comment FROM all_col_comments WHERE table_name = 'your_table_name' -- 替换为你的表名称 AND owner = 'your_schema_name'; -- 替换为你的模式名称 |
请确保将 "your_database_name"、"your_table_name"、"your_schema_name" 替换为你要查询的数据库、表和模式的实际名称。这些查询语句针对不同的数据库管理系统,可以帮助你查询表和列的注释信息。
示例
1 2 3 4 5 6 7 8 9 10 11 12 | CREATE TABLE employees ( employee_id NUMBER, first_name VARCHAR2(50), last_name VARCHAR2(50) ); COMMENT ON TABLE employees IS '员工信息表'; COMMENT ON COLUMN employees.employee_id IS '员工ID'; COMMENT ON COLUMN employees.first_name IS '员工名字'; COMMENT ON COLUMN employees.last_name IS '员工姓氏'; |
MySQL
对于MySQL和MariaDB: 查询表的注释:
1 2 3 4 5 6 7 8 | SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'your_database_name' -- 替换为你的数据库名称 AND TABLE_NAME = 'your_table_name'; -- 替换为你的表名称 |
查询列的注释:
1 2 3 4 5 6 7 8 9 | SELECT TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'your_database_name' -- 替换为你的数据库名称 AND TABLE_NAME = 'your_table_name'; -- 替换为你的表名称 |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户ID', name VARCHAR(50) COMMENT '姓名', age INT COMMENT '年龄', email VARCHAR(100) COMMENT '电子邮件' ) COMMENT '用户信息表'; SHOW CREATE TABLE users; SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'users' AND table_schema = 'lhrdb'; SELECT column_name, column_comment FROM information_schema.columns WHERE table_name = 'users' AND table_schema = 'lhrdb'; alter table table_name comment='表的注释'; alter table employee comment='雇员信息表'; ALTER TABLE employee CHANGE COLUMN employee_name employee_name varchar(30) DEFAULT NULL COMMENT '员工姓名2' ; ALTER TABLE employee MODIFY COLUMN employee_name varchar(30) DEFAULT NULL COMMENT '修改后的字段注释'; |
SQL Server
表或自建视图的定义: