繁体   English   中英

MySql对视图列的注释?

[英]MySql comments for the columns of a view?

在MySql中是否可以存储视图列的注释?

我知道如何向普通表和列添加注释,但我不确定是否可以对视图执行此操作。 我所知道的是,视图(对于某些方面)的行为就像一个表,因此可以运行查询:

SELECT 
  column_name, column_comment 
FROM 
  information_schema.columns 
WHERE 
  table_name='myview';

但我不知道如何首先添加评论,还没有找到解决方案!

我这样做的原因是我在注释字段中存储我的应用程序的元数据,我希望表和视图是相同的。

根据create view语法,即使视图的列存在于information_schema.columns表中,当前也无法向视图的“列”添加注释:

http://dev.mysql.com/doc/refman/5.0/en/create-view.html

我不认为您可以向视图“列”添加注释,但您可以使用视图从基础表中检索注释,使用SHOW COLUMNS,就像查询表时一样。

Mysql没有视图列的元数据:

http://dev.mysql.com/doc/refman/5.0/en/views-table.html

所以答案是否定的。

我们已达到5.7版本,即使已经多次请求,此功能仍未实现。 有四个与此功能相关的活动票证:

http://bugs.mysql.com/bug.php?id=5159
http://bugs.mysql.com/bug.php?id=64045
http://bugs.mysql.com/bug.php?id=52429
http://bugs.mysql.com/bug.php?id=15344

......和几个标记为重复: http://bugs.mysql.com/bug.php?id=19602http://bugs.mysql.com/bug.php?id=19602HTTP://错误.mysql.com / bug.php?ID = 13109http://bugs.mysql.com/bug.php?id=14369http://bugs.mysql.com/bug.php?id=11082HTTP :?//bugs.mysql.com/bug.php ID = 42870http://bugs.mysql.com/bug.php?id=38137http://bugs.mysql.com/bug.php?id = 38137http ://bugs.mysql.com/bug.php?id = 30729

如果您对此问题感兴趣,请转到四个活动票证,单击“影响我”按钮,然后添加注释,询问是否有人正在使用此功能。

这将增加可见性,并增加其实施的可能性。

- 视图显示基础表的列注释。

-- this does not show column comments
SHOW COLUMNS FROM zztable_vw;

-- this shows column comments from the underlying table
SHOW FULL COLUMNS FROM zztable_vw;

CREATE TABLE `zztable` (
-- A SQL statement comment. Not stored with the table. Just documents the create table code
  `zz_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'unique primary key. auto increment',
  `zz_descr` varchar(255) NOT NULL COMMENT 'descriptive name. must be unique if not null',
  PRIMARY KEY (`zz_id`),
  UNIQUE KEY `zz_descr_UNIQUE` (`zz_descr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='a table demonstrating table, column, and view comments. ';

-- select the table from information_schema
SELECT table_type, table_name, table_rows, table_comment
FROM information_schema.tables ta
WHERE ta.table_name LIKE 'zztable%'
ORDER BY ta.table_type, ta.table_name;

-- select the table_columns from information_schema
SELECT ta.table_type, co.table_name, co.column_name, co.column_comment
FROM information_schema.columns co
JOIN information_schema.tables ta
  ON co.table_name = ta.table_name
WHERE co.table_name LIKE 'zztable%'
ORDER BY ta.table_type, co.table_name, co.ordinal_position;

-- create a view over the commented table
CREATE OR REPLACE VIEW zztable_vw
AS
SELECT zz_id, zz_descr
FROM zztable;

- 现在再次运行information_schema查询以查看结果中的新视图

- information_schema.tables查询不显示表级注释

 <table> <tr> <th>table_type</th> <th>table_name</th> <th>table_rows</th> <th>table_comment</th> </tr> <tr> <td>BASE TABLE</td> <td>zztable</td> <td>0</td> <td>a table demonstrating table, column, and view comments.</td> </tr> <tr> <td>VIEW</td> <td>zztable_vw</td> <td>NULL</td> <td>VIEW</td> </tr> </table> 

- information_schema.columns查询显示基础表列注释

 <table> <tr> <th>table_type</th> <th>table_name</th> <th>column_name</th> <th>column_comment </tr> <tr> <td>BASE TABLE</td> <td>zztable</td> <td>zz_id</td> <td>unique primary key. auto increment</td> </tr> <tr> <td>BASE TABLE</td> <td>zztable</td> <td>zz_descr</td> <td>descriptive name. must be unique if not null</td> </tr> <tr> <td>VIEW</td> <td>zztable_vw</td> <td>zz_id</td> <td>unique primary key. auto increment</td> </tr> <tr> <td>VIEW</td> <td>zztable_vw</td> <td>zz_descr</td> <td>descriptive name. must be unique if not null</td> </tr> </table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM