SQL - Show Databases



Many a times you face a situation where you need to list down all the available databases. MySQL provides a convenient way to list down all the databases using SHOW DATABASES command where as there is no particular command in MS SQL Server to show or list the databases but, you can use the SELECT...FROM command as a work around list down available databases.

List Databases using SQL

The SQL SHOW DATABASES statement is used to list down all the available databases in MySQL database. You can use SHOW SCHEMAS as an alternate command for SHOW DATABASES.

Syntax

Following is the syntax of SQL SHOW DATABASES to list down all the available databases in MySQL −

SHOW DATABASES [LIKE 'pattern' | WHERE expr] ;

We can use LIKE or WHERE clause along with SHOW DATABASES to filter out a list of databases.

Example

Following is an example to list down all the available databases.

SHOW DATABASES;

The output will be displayed as follows. This output depends on the number of databases available in the system −

Database
performance_schema
information_schema
mysql
testDB

Following is an example to list down all the databases whose name starts with test.

SHOW DATABASES LIKE 'test%';

The output will be displayed as follows −

Database (test%)
testDB

The SHOW SCHEMAS Statement

You can use the SHOW SCHEMAS statement as an alternate for the SHOW DATABASES statement.

Syntax

Following is the syntax of the SQL SHOW SCHEMAS statement to list down all the available databases in MySQL −

SHOW SCHEMAS [LIKE 'pattern' | WHERE expr] ;

We can use LIKE or WHERE clause along with SHOW SCHEMAS to filter out a list of databases.

Example

Following is an example to list down all the available databases.

SHOW SCHEMAS;

The output will be displayed as follows. This output depends on the number of databases available in the system −

Database
performance_schema
information_schema
mysql
testDB

Following is an example to list down all the databases whose name starts with test.

SHOW SCHEMAS LIKE 'test%';

The output will be displayed as follows −

Database (test%)
testDB

The SELECT...FROM Statement

If you are working with MS SQL Server then you can use the SELECT...FROM statement to list down all the available databases as shown below.

SQL> SELECT * FROM sys.databases;

Output

If we execute the above query, it returns a table that lists down all the databases and associated information about the databases.

name database_id source_database_id owner_sid
master 1 NULL 001
tempdb 2 NULL 001
model 3 NULL 001
msdb 4 NULL 001
testDB 5 NULL 001000

The EXEC sp_databases Statement

If you are using MS SQL Server then you can use the following EXEC sp_databases statement to list down all the databases −

SQL> EXEC sp_databases;

Output

This will display the same result as we got from SELECT...FROM statement.

name database_id source_database_id owner_sid
master 1 NULL 001
tempdb 2 NULL 001
model 3 NULL 001
msdb 4 NULL 001
testDB 5 NULL 001000
Advertisements