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
select * FROM information_schema.views\G;
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.
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';
SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE 'VIEW';
MySQL query to find all views in a database
Another way to find all View:
SELECT DISTINCT table_name FROM information_schema.TABLES WHERE table_type = 'VIEW'
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.