MySql对视图列的注释?

时间:2012-01-14 20:13:07

标签: mysql

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

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

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

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

我这样做的原因是我在评论字段中存储了我的应用程序的元数据,我希望表格和视图完全相同。

5 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:2)

Mysql没有视图列的元数据:

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

所以答案是否定的。

答案 3 :(得分:1)

我们已达到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://bugs.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=42870,{{ 3}},http://bugs.mysql.com/bug.php?id=38137http://bugs.mysql.com/bug.php?id=38137

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

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

答案 4 :(得分:0)

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

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