我看到视图有一个注释字段,就像常规表一样,但默认情况下填充有“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
当我尝试创建带有注释的视图时,出现错误。
CREATE OR REPLACE VIEW view__x AS
SELECT
*
FROM `some_table`
COMMENT = 'some comment'
有没有办法修改评论字段,或者该字段在内部用于其他用途并且应该保持原样?
我已向 mysql 添加了功能请求。
根据创建视图语法,当前无法为视图添加注释:
此功能已被多次请求。有四个与此功能相关的有效票证:
...还有几个标记为重复项: 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
如果您对此问题感兴趣,请转到四个有效票证,单击“影响我”按钮,并添加评论,询问是否有人正在研究此功能。
这将增加可见性,并增加实施的可能性。
更新 - 我在 2013 年 12 月发布了此评论。当时版本是 5.7。截至 2023 年 8 月,最新版本为 8.1,CREATE VIEW 文档页面没有变化。
我有类似的需求,我在 MySQL 中解决这个问题的一种方法是在用作文档的
WHERE
子句中添加一个 true 谓词。我承认这很糟糕,但是您是否同意任何文档都比没有文档好?一旦以这种方式进行评论的良好副作用将持续存在mysqldump
。据我所知,优化器不会受到额外的 true 谓词的阻碍。
视图创建示例:
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.';
然后查看文档...
> 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)
您可以通过在架构中创建一个表来存储每个视图的注释,从而在视图上创建注释。然后将 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;
-- 视图级和表级注释现在显示在 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>
我希望能够标记开发中的视图版本,以便我可以跟踪其他环境中的不同版本。 我的解决方案很简单:我只需将包含版本的常量字段添加到字段列表中。我喜欢每个检索到的视图记录都附带视图版本的优势。
VIEW `myview` AS
SELECT
'2022-10-11' AS `ViewVersion`,
....