How to get a list of MySQL views?

后端 未结 9 1491
清歌不尽
清歌不尽 2020-11-28 20:12

I\'m looking for a way to list all views in a database.

Initially I found and tried an answer on the MySQL forums:

SELECT table_name
FROM information         


        
相关标签:
9条回答
  • 2020-11-28 20:40
     select * FROM information_schema.views\G; 
    
    0 讨论(0)
  • 2020-11-28 20:40

    Try moving that mysql.bak directory out of /var/lib/mysql to say /root/ or something. It seems like mysql is finding that and it may be causing that ERROR 1102 (42000): Incorrect database name 'mysql.bak' error.

    0 讨论(0)
  • 2020-11-28 20:47

    Here's a way to find all the views in every database on your instance:

    SELECT TABLE_SCHEMA, TABLE_NAME 
    FROM information_schema.tables 
    WHERE TABLE_TYPE LIKE 'VIEW';
    
    0 讨论(0)
  • 2020-11-28 20:50
    SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE 'VIEW';
    

    MySQL query to find all views in a database

    0 讨论(0)
  • 2020-11-28 20:50

    Another way to find all View:

    SELECT DISTINCT table_name FROM information_schema.TABLES WHERE table_type = 'VIEW'

    0 讨论(0)
  • 2020-11-28 20:52

    To complement about to get more info about a specific view

    Even with the two valid answers

    SHOW FULL TABLES IN your_db_name WHERE TABLE_TYPE LIKE 'VIEW';
    
    SELECT TABLE_SCHEMA, TABLE_NAME 
    FROM information_schema.TABLES 
    WHERE TABLE_TYPE LIKE 'VIEW' AND TABLE_SCHEMA LIKE 'your_db_name';
    

    You can apply the following (I think is better):

    SELECT TABLE_SCHEMA, TABLE_NAME 
    FROM information_schema.VIEWS 
    WHERE TABLE_SCHEMA LIKE 'your_db_name';
    

    is better work directly with information_schema.VIEWS (observe now is VIEWS and not TABLES anymore), thus you can retrieve more data, use DESC VIEWS for more details:

    +----------------------+---------------------------------+------+-----+---------+-------+
    | Field                | Type                            | Null | Key | Default | Extra |
    +----------------------+---------------------------------+------+-----+---------+-------+
    | TABLE_CATALOG        | varchar(64)                     | YES  |     | NULL    |       |
    | TABLE_SCHEMA         | varchar(64)                     | YES  |     | NULL    |       |
    | TABLE_NAME           | varchar(64)                     | YES  |     | NULL    |       |
    | VIEW_DEFINITION      | longtext                        | YES  |     | NULL    |       |
    | CHECK_OPTION         | enum('NONE','LOCAL','CASCADED') | YES  |     | NULL    |       |
    | IS_UPDATABLE         | enum('NO','YES')                | YES  |     | NULL    |       |
    | DEFINER              | varchar(93)                     | YES  |     | NULL    |       |
    | SECURITY_TYPE        | varchar(7)                      | YES  |     | NULL    |       |
    | CHARACTER_SET_CLIENT | varchar(64)                     | NO   |     | NULL    |       |
    | COLLATION_CONNECTION | varchar(64)                     | NO   |     | NULL    |       |
    +----------------------+---------------------------------+------+-----+---------+-------+
    

    For example observe the VIEW_DEFINITION field, thus you can use in action:

    SELECT TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION 
    FROM information_schema.VIEWS 
    WHERE TABLE_SCHEMA LIKE 'your_db_name';
    

    Of course you have more fields available for your consideration.

    0 讨论(0)
提交回复
热议问题