The USE statement is used to select a database in MySQL. After selecting a database, you can create tables, insert data, update records, and run queries on objects inside that database.
The USE statement tells MySQL which database you want to work with.
USE school;
After this command, school becomes the current database.
The basic syntax is:
USE database_name;
Replace database_name with the name of the database you want to select.
Suppose a database named school already exists.
USE school;
Now SQL statements that do not explicitly specify another database will work with the selected school database.
A common workflow is to create the database first and then select it.
CREATE DATABASE school;
USE school;
The first statement creates the database and the second selects it.
The USE statement does not create a database.
USE school;
The database must already exist and the current user must have appropriate access to it.
Before selecting a database, you can view databases accessible to the current user.
SHOW DATABASES;
Then select the required database.
USE school;
Use the DATABASE() function to check which database is currently selected.
SELECT DATABASE();
For example:
USE school;
SELECT DATABASE();
After selecting the school database, the result can identify the current database:
school
If no database is selected, the result can be NULL.
After selecting a database, you can create tables inside it.
USE school;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100)
);
The SHOW TABLES command displays tables from the currently selected database.
USE school;
SHOW TABLES;
Once the database is selected, you can insert records into its tables.
USE school;
INSERT INTO students
(id, name)
VALUES
(1, 'Rahul');
You can retrieve data from a table in the selected database.
USE school;
SELECT * FROM students;
The query reads data from the students table in the school database.
You can change the current database at any time by using another USE statement.
USE school;
USE library;
After the second command, library becomes the current database.
CREATE DATABASE school;
CREATE DATABASE library;
USE school;
SELECT DATABASE();
USE library;
SELECT DATABASE();
This example shows how the current database changes when another database is selected.
The USE statement can be executed in MySQL Workbench's SQL Editor.
USE school;
After executing it, you can work with the tables inside the selected database.
The same statement can be used in the MySQL command-line client.
mysql> USE school;
MySQL will select the database for the current session.
Suppose a library database already exists.
USE library;
You can then work with tables such as:
books
students
issues
returns
For a company application, you could select:
USE company_db;
You could then work with employee, department, and project tables stored in that database.
For a hospital management system:
USE hospital_db;
The selected database can contain tables such as patients, doctors, appointments, and billing.
You do not always have to select a database before referencing a table. You can specify the database name directly.
SELECT *
FROM school.students;
Here, school.students means the students table inside the school database.
The following example demonstrates the relationship between USE and CREATE TABLE.
USE school;
CREATE TABLE teachers (
id INT PRIMARY KEY,
name VARCHAR(100),
subject VARCHAR(100)
);
The teachers table is created in the selected school database.
After selecting the database, you can insert records into its tables.
USE school;
INSERT INTO teachers
(id, name, subject)
VALUES
(1, 'Amit Kumar', 'Mathematics');
You can also update records after selecting the database.
USE school;
UPDATE teachers
SET subject = 'Physics'
WHERE id = 1;
The selected database can also be used for DELETE operations.
USE school;
DELETE FROM teachers
WHERE id = 1;
Always use a suitable WHERE condition when deleting specific records.
If you try to select a database that does not exist, MySQL will report an error.
USE unknown_database;
Make sure the database exists before using the USE statement.
If no database has been selected and you try to perform an operation that requires a current database, MySQL may report that no database is selected.
You can solve this by selecting the required database:
USE school;
A useful workflow is to check the current database before running important queries.
SELECT DATABASE();
USE school;
SELECT DATABASE();
This helps confirm which database is currently active.
Common problems include:
CREATE DATABASE IF NOT EXISTS school;
SHOW DATABASES;
USE school;
SELECT DATABASE();
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
course VARCHAR(100)
);
INSERT INTO students
(id, name, course)
VALUES
(1, 'Rahul', 'Python');
SELECT * FROM students;
This example creates a database, selects it, checks the current database, creates a table, inserts data, and retrieves the data.
Suppose you have two databases for different applications:
school_db
library_db
You can switch between them as required:
USE school_db;
SELECT DATABASE();
SHOW TABLES;
USE library_db;
SELECT DATABASE();
SHOW TABLES;
The USE statement makes it easy to switch the working database during a MySQL session.
Question: Which statement is used to select a database in MySQL?