MySQL – List User Defined Tables – Two Methods

There are different methods to list out user defined tables in MySQL. We will see two methods in this post.

Let us create the following tables in the database named Test

Create a database named Test


CREATE DATABASE Test;

Create the tables named items and sales in the database Test.


USE TEST;
CREATE TABLE items(item_id INT, item_description VARCHAR(100));
CREATE TABLE sales(sales_id INT auto_increment KEY,item_id INT, 
           sales_date DATETIME, sales_amount DECIMAL(12,2));

Now to list out all user defined tables, use any one of the following methods

Method 1: Use SHOW TABLES command


SHOW TABLES;

If you execute the above statement, it will list out all the user defined tables in the database in which it is run (in this example the database name is Test)

SHOW TABLES.

The result is


Tables_in_test
 --------------
 items
 sales

You can also use database name along with this


SHOW TABLES FROM Test;

Which will give you the same result.

Method 2: Use INFORMATION_SCHEMA view

You can make use of ANSI standard INFORMATION_SCHEMA.TABLES view to do the same shown as below


SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='Test';

The result is

Querying table_name in sql server management studio.


Tables_in_test
 --------------
 items
 sales

This is a very small trick but works great when you want to know what are different tables exist in a MySQL Database.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

MySQL, SQL Scripts, SQL Server, SQL System Table
Previous Post
Tracking Recent Object Changes With modify_date
Next Post
LAG and LEAD: Comparing Each Row With the One Before It

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.