简体   繁体   English

在mysql中为视图创建注释

[英]Create comments for views in mysql

I see that the views have a comment field just like the regular tables, but is by default populated with the "VIEW" value. 我看到视图有一个注释字段,就像常规表一样,但默认情况下填充了“VIEW”值。

[TABLE_CATALOG] => 
[TABLE_SCHEMA] => xxx
[TABLE_NAME] => view__xxxx
[TABLE_TYPE] => VIEW
[ENGINE] => 
[VERSION] => 
[ROW_FORMAT] => 
[TABLE_ROWS] => 
[AVG_ROW_LENGTH] => 
[DATA_LENGTH] => 
[MAX_DATA_LENGTH] => 
[INDEX_LENGTH] => 
[DATA_FREE] => 
[AUTO_INCREMENT] => 
[CREATE_TIME] => 
[UPDATE_TIME] => 
[CHECK_TIME] => 
[TABLE_COLLATION] => 
[CHECKSUM] => 
[CREATE_OPTIONS] => 
[TABLE_COMMENT] => VIEW

When I am trying to create a view with a comment I get an error. 当我尝试使用注释创建视图时,我收到错误。

CREATE OR REPLACE VIEW view__x AS
SELECT 
 * 
FROM `some_table`  
COMMENT = 'some comment'

Is there a way to modify the comment field or that field is used internally for something else and should stay like it is? 有没有办法修改注释字段或该字段在内部用于其他东西,应该保持这样吗?

I've added a feature request to mysql. 我已经向mysql添加了一个功能请求

According to the create view syntax there is no way currently to add comment a view: 根据create view语法,目前无法添加注释视图:

This feature has been requested several times. 已多次请求此功能。 There are four active tickets related to this functionality: 有四个与此功能相关的活动票证:

...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. 这将增加可见性,并增加其实施的可能性。

You can home brew comments on views by creating a table in your schema to store the comment on each view. 您可以通过在架构中创建表来存储对每个视图的注释来对视图进行brew注释。 Then join information_schema.tables to the new table. 然后将information_schema.tables加入到新表中。

-- A view does not show the table-level comments of the underlying table.
-- nor can a view have view-level comments

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;

-- 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
-- MySQL does not allow view-level comments. 

-- create a new table to contain the view-level comments
CREATE TABLE IF NOT EXISTS `schema_view` (
  `schema_view_id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'unique primary key. auto increment. ',
  `schema_view_name` VARCHAR(64) NOT NULL COMMENT 'view name matches information_schema.tables.table_name for VIEW',
  `schema_view_comment` VARCHAR(2048) NULL DEFAULT NULL COMMENT 'the descriptive purpose of the view. ',
  PRIMARY KEY (`schema_view_id`))
ENGINE = InnoDB
COMMENT = 'contains comments for views since MySQL does not store view-level comments. Use this in a join on schema_view_name to information_schema.tables.table_name';

CREATE UNIQUE INDEX `schema_view_name_UNIQUE` ON `schema_view` (`schema_view_name` ASC);

-- insert a view comment
SELECT * FROM schema_view;

INSERT INTO schema_view
(schema_view_name, schema_view_comment)
VALUES ('zztable_vw' , 'a demonstration of documenting view metadata with comments');
COMMIT;

-- modify the query to join to the new schema_view table
-- select the view from information_schema joined to the new table
SELECT ta.table_type, ta.table_name, ta.table_rows, 
    -- show different comments based on table_type
    CASE 
        WHEN ta.table_type = 'BASE TABLE' THEN ta.table_comment
        WHEN ta.table_type = 'VIEW' THEN sv.schema_view_comment
        ELSE NULL
    END AS schema_comment,
    ta.table_comment, 
    sv.schema_view_comment
FROM information_schema.tables ta
-- Show view comments if it exists.
LEFT OUTER JOIN schema_view sv
  ON ta.table_name = sv.schema_view_name
WHERE ta.table_name LIKE 'zztable%'
ORDER BY ta.table_type, ta.table_name;

-- simplify future queries by creating a view
CREATE OR REPLACE VIEW `schema_table_vw`
AS
SELECT ta.table_type, ta.table_name, ta.table_rows, 
    -- show different comments based on type
    CASE 
        WHEN ta.table_type = 'BASE TABLE' THEN ta.table_comment
        WHEN ta.table_type = 'VIEW' THEN sv.schema_view_comment
        ELSE NULL
    END AS schema_comment
FROM information_schema.tables ta
-- Show view comments if it exists.
LEFT OUTER JOIN schema_view sv
  ON ta.table_name = sv.schema_view_name
WHERE ta.table_schema = 'my_schema'
ORDER BY ta.table_type, ta.table_name;

-- view-level and table-level comments now show in schema_comment - 现在在schema_comment中显示视图级和表级注释

 <table width="200" border="1"> <tr> <th scope="col">table_type</th> <th scope="col">table_name</th> <th scope="col">table_rows</th> <th scope="col">schema_comment</th> <th scope="col">table_comment</th> <th scope="col">schema_view_comment</th> </tr> <tr> <td>BASE TABLE</td> <td>zztable</td> <td>0</td> <td>a table demonstrating table, column, and view comments.</td> <td>a table demonstrating table, column, and view comments.</td> <td>NULL</td> </tr> <tr> <td>VIEW</td> <td>zztable_vw</td> <td>NULL</td> <td>a demonstration of documenting view metadata with comments</td> <td>VIEW</td> <td>a demonstration of documenting view metadata with comments</td> </tr> </table> 

I had a similar need, and one way I hacked this in MySQL was to add a truthy predicate in the WHERE clause that served as documentation. 我有类似的需求,我在MySQL中攻击它的一种方法是在WHERE子句中添加一个真正的谓词作为文档。 I admit this is hacky, but wouldn't you agree any documentation is better than no documentation at all? 我承认这很hacky,但是你不同意任何文档比没有文档更好吗? Once nice side-effect of doing your commentary this way will survive a mysqldump . 以这种方式做评论的好副作用将在mysqldump存活下来。 As far as I know, the optimizer will not be hindered by the extra truthy predicate. 据我所知,优化器不会受到额外的真理谓词的阻碍。

Example view creation: 示例视图创建:

CREATE OR REPLACE VIEW high_value_employees AS
SELECT *
FROM `employees`
WHERE salary >= 200000
AND 'comment' != 'This view was made by Josh at the request of an important VP who wanted a concise list of who we might be overpaying. Last modified on 26 July 2019.';

And then viewing the documentation ... 然后查看文档......

> SHOW CREATE TABLE high_value_employees \G
*************************** 1. row ***************************
                View: high_value_employees
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`jhuber`@`%` SQL SECURITY 
DEFINER VIEW `high_value_employees` AS select `employees`.`salary` AS `salary` from
`employees` where ((`employees`.`salary` >= 200000) and ('comment' <> 'This view was
made by Josh at the request of an important VP who wanted a concise list of who we
might be overpaying. Last modified on 26 July 2019.'))
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
1 row in set (0.00 sec)

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

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