简体   繁体   English

MySql对视图列的注释?

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

Is it possible in MySql to store comments for the columns of a view? 在MySql中是否可以存储视图列的注释?

I know how to add comments to normal tables and columns but I am not sure if it is possible to do so for views. 我知道如何向普通表和列添加注释,但我不确定是否可以对视图执行此操作。 All I know is that views (for some aspects) behave just like a table and for this reason it is possible to run a query as such: 我所知道的是,视图(对于某些方面)的行为就像一个表,因此可以运行查询:

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

But I don't know how to add the comment in the first place and haven't found a solution yet! 但我不知道如何首先添加评论,还没有找到解决方案!

The reason I am doing this is that I am storing metadata for my application in the comment field and I would like tables and views to be identical. 我这样做的原因是我在注释字段中存储我的应用程序的元数据,我希望表和视图是相同的。

According to the create view syntax there is no way currently to add comments to the "columns" of a view even if the columns of a view are present in the information_schema.columns table: 根据create view语法,即使视图的列存在于information_schema.columns表中,当前也无法向视图的“列”添加注释:

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

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

Mysql has no metadata for view columns: Mysql没有视图列的元数据:

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

So answer is NO. 所以答案是否定的。

We're up to version 5.7, and this feature still hasn't been implemented, even though it has been requested several times. 我们已达到5.7版本,即使已经多次请求,此功能仍未实现。 There are four active tickets related to this functionality: 有四个与此功能相关的活动票证:

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

...and several marked as duplicates: http://bugs.mysql.com/bug.php?id=19602 , http://bugs.mysql.com/bug.php?id=19602 , http://bugs.mysql.com/bug.php?id=13109 , http://bugs.mysql.com/bug.php?id=14369 , http://bugs.mysql.com/bug.php?id=11082 , http://bugs.mysql.com/bug.php?id=42870 , http://bugs.mysql.com/bug.php?id=38137 , http://bugs.mysql.com/bug.php?id=38137 , http://bugs.mysql.com/bug.php?id=30729 ......和几个标记为重复: 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

If you are interested in this issue, go to the four active tickets, click the "affects me" button, and also add a comment, asking if anyone is working on this feature. 如果您对此问题感兴趣,请转到四个活动票证,单击“影响我”按钮,然后添加注释,询问是否有人正在使用此功能。

This will add visibility, and increase the likelyhood of it being implemented. 这将增加可见性,并增加其实施的可能性。

-- A view shows the column comments of the underlying table. - 视图显示基础表的列注释。

-- 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;

-- now run the information_schema queries again to see the new view in the results - 现在再次运行information_schema查询以查看结果中的新视图

-- information_schema.tables query does not show table-level comments - 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 query shows underlying table column comments - 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