Lesson 12 of 60 – SHOW DATABASES
20%

SHOW DATABASES in MySQL

The SHOW DATABASES statement is used to display the databases that are available to the current MySQL user. It is one of the basic commands used when exploring and managing a MySQL server.

Note: SHOW DATABASES displays databases that the current user has permission to see. It does not necessarily mean that every database on the server will be visible to every user.

1. What is SHOW DATABASES?

SHOW DATABASES is a MySQL statement used to list databases accessible to the current user.

SHOW DATABASES;

The result is displayed as a list of database names.

2. Basic Syntax

The basic syntax is:

SHOW DATABASES;

The statement is normally terminated with a semicolon.

3. Example of SHOW DATABASES

Run the following command:

SHOW DATABASES;

You may see results similar to:

information_schema
mysql
performance_schema
sys
school
library

The exact list depends on your MySQL installation and user permissions.

4. System Databases

MySQL installations commonly contain system databases used internally by MySQL.

Examples can include:

  • information_schema
  • mysql
  • performance_schema
  • sys

These databases serve different administrative and metadata purposes.

5. User-Created Databases

Developers can create their own databases for applications.

CREATE DATABASE school;
CREATE DATABASE library;

After creation, these databases can appear when you run:

SHOW DATABASES;

6. SHOW DATABASES After Creating a Database

CREATE DATABASE school;

SHOW DATABASES;

The result can include school along with other databases accessible to the current user.

7. SHOW DATABASES in Command Line

You can run SHOW DATABASES from the MySQL command-line client.

mysql> SHOW DATABASES;

MySQL returns the available database names in a text-based result.

8. SHOW DATABASES in Workbench

You can also execute the command in MySQL Workbench.

SHOW DATABASES;

The result appears in the SQL Editor's result area.

9. SHOW DATABASES and USE

A common workflow is to first display databases and then select one.

SHOW DATABASES;

USE school;

SHOW DATABASES lists available databases, while USE selects one for the current session.

10. Checking the Current Database

SHOW DATABASES lists databases, but it does not tell you which one is currently selected.

Use:

SELECT DATABASE();

11. Creating Multiple Databases

You can create multiple databases and then view them using SHOW DATABASES.

CREATE DATABASE school;
CREATE DATABASE library;
CREATE DATABASE company;

SHOW DATABASES;

12. Database Names in the Result

The command returns a column containing database names.

Database
--------
school
library
company

The exact formatting may differ depending on the client you use.

13. SHOW DATABASES and Permissions

The databases displayed by SHOW DATABASES depend on the privileges and visibility available to the current MySQL user.

A user with limited permissions may see fewer databases than an administrator.

14. SHOW DATABASES with a User Account

Suppose a user connects to MySQL:

mysql -u student -p

After logging in, the user can run:

SHOW DATABASES;

The returned list depends on the privileges of that account.

15. SHOW DATABASES vs SHOW TABLES

Command Purpose
SHOW DATABASES; Displays accessible databases
SHOW TABLES; Displays tables in the selected database

16. SHOW DATABASES vs USE

Command Purpose
SHOW DATABASES; Lists databases
USE school; Selects the school database

These commands perform different tasks.

17. SHOW DATABASES and CREATE DATABASE

You can use both commands together when setting up a new application.

CREATE DATABASE IF NOT EXISTS school;

SHOW DATABASES;

This creates the database if necessary and then displays accessible databases.

18. SHOW DATABASES and DROP DATABASE

After dropping a database, you can use SHOW DATABASES to verify the current list.

DROP DATABASE school;

SHOW DATABASES;

Warning: DROP DATABASE removes the database and its objects. Use it carefully.

19. Filtering Database Names

MySQL also provides a form that can be used to display databases matching a pattern.

SHOW DATABASES LIKE 'school%';

This can help find database names that match a specific pattern.

20. LIKE with SHOW DATABASES

The LIKE pattern can be used with SHOW DATABASES.

For example:

SHOW DATABASES LIKE '%school%';

This looks for database names containing school in the pattern.

21. SHOW SCHEMAS

MySQL also supports SHOW SCHEMAS as a synonym for SHOW DATABASES.

SHOW SCHEMAS;

It can be used to display the databases accessible to the current user.

22. Database Discovery Workflow

1. Connect to MySQL
2. Run SHOW DATABASES
3. Find the required database
4. Use the database
5. Run SHOW TABLES
6. Work with the tables

23. Practical School Example

CREATE DATABASE school;

SHOW DATABASES;

USE school;

SHOW TABLES;

This is a simple workflow for creating and exploring a school database.

24. Practical Library Example

CREATE DATABASE library;

SHOW DATABASES;

USE library;

SHOW TABLES;

The database can then be used to create tables for books, members, and transactions.

25. Common Errors

SHOW DATABASES is simple, but you may still face connection-related problems.

  • MySQL Server is not running
  • User authentication failed
  • Connection to the server failed
  • Insufficient access privileges
  • Incorrect connection configuration

26. SHOW DATABASES in PHP Applications

Database applications normally use a selected database in their connection configuration.

Administrators or development tools can use SHOW DATABASES to inspect available databases, depending on the account's permissions.

SHOW DATABASES;

27. Database List and Application Development

When developing multiple projects on the same MySQL server, SHOW DATABASES can help you identify available databases.

school_db
library_db
company_db
inventory_db

You can then select the required database with USE.

28. Important Difference

Remember the difference between these commands:

SHOW DATABASES;
USE school;
SHOW TABLES;
  • SHOW DATABASES → lists databases
  • USE → selects a database
  • SHOW TABLES → lists tables in the selected database

29. Complete SHOW DATABASES Example

CREATE DATABASE IF NOT EXISTS school;
CREATE DATABASE IF NOT EXISTS library;

SHOW DATABASES;

USE school;

SELECT DATABASE();

SHOW TABLES;

This example creates two databases, displays the database list, selects the school database, checks the current database, and displays its tables.

30. Practical Database Discovery Example

Suppose a MySQL server contains several project databases. You can discover and select the required database as follows:

SHOW DATABASES;

SHOW DATABASES LIKE '%library%';

USE library_db;

SELECT DATABASE();

SHOW TABLES;

This workflow helps you find an available database, select it, verify the selection, and then explore its tables.

📌 Key Points

  • SHOW DATABASES lists databases accessible to the current user.
  • The basic syntax is SHOW DATABASES;
  • It can be used from MySQL Workbench or the command line.
  • The result can contain both system and user-created databases.
  • Database visibility depends on the user's privileges.
  • USE selects a database after you find it.
  • SHOW TABLES displays tables inside the selected database.
  • SHOW DATABASES LIKE can be used to match database names using a pattern.
  • SHOW SCHEMAS is a synonym for SHOW DATABASES.
  • SHOW DATABASES is useful when exploring and managing a MySQL server.

🧠 Quick Quiz

Question: Which command is used to display databases accessible to the current MySQL user?