Create comments for views in mysql

后端 未结 3 1491
北恋
北恋 2020-12-09 16:05

I see that the views have a comment field just like the regular tables, but is by default populated with the \"VIEW\" value.

[TABLE_CATALOG] => 
[TABLE_S         


        
3条回答
  •  难免孤独
    2020-12-09 16:19

    You can home brew comments on views by creating a table in your schema to store the comment on each view. Then join information_schema.tables to the new table.

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

    table_type table_name table_rows schema_comment table_comment schema_view_comment
    BASE TABLE zztable 0 a table demonstrating table, column, and view comments. a table demonstrating table, column, and view comments. NULL
    VIEW zztable_vw NULL a demonstration of documenting view metadata with comments VIEW a demonstration of documenting view metadata with comments

提交回复
热议问题