What is the USE Statement in MySQL and Why Do We Use It?
When you want to use a specific database from the list of available databases, you type the USE database_name statement.
The USE statement is used to select a database as the current database for the current session. After selecting the database, you can execute SQL statements on that database.
Syntax:
USE database_name;
Example:
USE PersonDB;
Output:
If the selected database exists, MySQL selects it as the current database and displays the following message:
Database changed
If the selected database does not exist, MySQL displays the following error message:
ERROR 1049 (42000): Unknown database 'database_name'
How the USE Statement Works Internally
When you want to use a database in MySQL, you type the USE statement. After pressing the Enter key, the USE statement is sent to the MySQL server. The server checks whether the database exists. If it exists, MySQL selects that database as the current database for the current session. If it does not exist, MySQL displays an error message.
Remember that the USE statement does not create, copy, move, or modify any data. It only sets the database context for further SQL statements. All SQL statements run on that database until another USE statement is executed or the current MySQL session is closed.
Flowchart:
👤 User | ▼ ⌨️ USE database_name; | ▼ 🖥️ MySQL Server | ▼ 🔍 Check Database Exists | ┌───────┴───────┐ ▼ ▼ ✅ Yes ❌ No | | ▼ ▼ 📁 Select ⚠️ Error Database Message | ▼ ⚙️ Run SQL Statements on Selected Database
Precautions While Using USE Statement:
- Before using the USE statement, you should know the correct database name and its spelling. Otherwise, MySQL will display an error message.
- Before using the USE statement, make sure that the database exists in the MySQL server.
- You should check that you have the required permission to access the database.
- Before executing queries, always check the selected database; otherwise, queries may run on the wrong database.
How to Use the USE Statement in MySQL: Step-by-Step Guide
If you use the USE statement in MySQL, you first need to check the available databases on the MySQL server. After finding the required database from the list, you use the USE statement to select that database. The complete process is as follows:
Step 1: You connect to the MySQL server by using the MySQL login command.
mysql -u username -p
Step 2: You type the SHOW DATABASES statement to view the list of available databases.
SHOW DATABASES;
Step 3: You select the required database by typing the USE statement.
USE database_name;
Step 4: After selecting the database, you execute SQL statements such as CREATE TABLE, INSERT, UPDATE, DELETE, and SELECT on the selected database.
Note:
- The USE statement allows you to select one database at a time.
- You can switch to another database by executing the USE statement with a different database name.
- The selected database stays active throughout the current MySQL session.
Don't Confuse SHOW DATABASES with the USE Statement
Many beginners get confused between the SHOW DATABASES statement and the USE statement because both are related to databases. However, they perform different tasks. The SHOW DATABASES statement displays the list of available databases on the MySQL server, whereas the USE statement selects a specific database from that list and makes it the current database.
Therefore, when you want to work with a particular database, first find the database name by using the SHOW DATABASES statement (if needed), and then select it by using the USE statement.












