Below are four ways to list out the views in a MySQL database using SQL or the command line.
The SHOW TABLES
Command
The SHOW TABLES
command lists the non-TEMPORARY
tables, sequences and views in a given MySQL database. We can use the WHERE
clause to narrow it to just views.
We can also use the FULL
modifier to return a second column that displays the type:
SHOW FULL TABLES
WHERE Table_Type LIKE 'VIEW';
Result:
+-----------------+------------+ | Tables_in_music | Table_type | +-----------------+------------+ | valbumsartists | VIEW | | valbumsgenres | VIEW | | vallalbums | VIEW | | vallartists | VIEW | | vallgenres | VIEW | +-----------------+------------+
Omitting the WHERE
clause returns all types:
SHOW FULL TABLES;
Result:
+-----------------+------------+ | Tables_in_music | Table_type | +-----------------+------------+ | Albums | BASE TABLE | | Artists | BASE TABLE | | Genres | BASE TABLE | | valbumsartists | VIEW | | valbumsgenres | VIEW | | vallalbums | VIEW | | vallartists | VIEW | | vallgenres | VIEW | +-----------------+------------+
The SHOW TABLE STATUS
Command
In MySQL, the SHOW TABLE STATUS
command is similar to the SHOW TABLES
command but provides more extensive information about each (non-TEMPORARY
) table.
Example:
SHOW TABLE STATUS;
Result:
+----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+ | Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment | +----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+ | Albums | InnoDB | 10 | Dynamic | 20 | 819 | 16384 | 0 | 32768 | 0 | 21 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | Artists | InnoDB | 10 | Dynamic | 16 | 1024 | 16384 | 0 | 0 | 0 | 17 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | Genres | InnoDB | 10 | Dynamic | 8 | 2048 | 16384 | 0 | 0 | 0 | 9 | 2021-11-13 12:56:02 | 2021-11-13 12:56:13 | NULL | utf8mb4_0900_ai_ci | NULL | | | | valbumsartists | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:02:24 | NULL | NULL | NULL | NULL | NULL | VIEW | | valbumsgenres | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:09:47 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallalbums | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:12:51 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallartists | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:13:02 | NULL | NULL | NULL | NULL | NULL | VIEW | | vallgenres | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2021-11-15 06:12:36 | NULL | NULL | NULL | NULL | NULL | VIEW | +----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
It also accepts a WHERE
and LIKE
clause in case you want to narrow down the results.
The information_schema.TABLES
Table
We can also query the information_schema.TABLES
table:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_TYPE
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA LIKE 'music' AND TABLE_TYPE LIKE 'VIEW';
Result:
+--------------+----------------+------------+ | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | +--------------+----------------+------------+ | Music | valbumsartists | VIEW | | Music | valbumsgenres | VIEW | | Music | vallgenres | VIEW | | Music | vallalbums | VIEW | | Music | vallartists | VIEW | +--------------+----------------+------------+
In this case, I returned all views from the database called music
. Querying this without filtering the results by TABLE_SCHEMA
returns views from all databases. Similarly, querying it without filtering by TABLE_TYPE
returns all table types.
The mysqlshow
Client
Another way to do it is with the mysqlshow
utility.
To use this option, open a command line prompt/terminal window and run the following:
mysqlshow --user root --password music;
Be sure to replace music
with the database you’re interested in, and root
with the applicable user. The --password
bit results in the user being prompted for the password.
Result:
Enter password: Database: music +----------------+ | Tables | +----------------+ | Albums | | Artists | | Genres | | valbumsartists | | valbumsgenres | | vallalbums | | vallartists | | vallgenres | +----------------+
The mysqlshow
client returns views and tables.
The output displays only the names of those databases, tables, or columns for which the user has some privileges.
If no database is given then all matching databases are shown. If no table is given, then all matching tables in database are shown. If no column is given, then all matching columns and column types in table are shown.