Lesson 11 of 60 – USE Database
18%

USE Database in MySQL

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.

Note: Creating a database and selecting a database are two different operations. CREATE DATABASE creates the database, while USE selects it for the current session.

1. What is USE?

The USE statement tells MySQL which database you want to work with.

USE school;

After this command, school becomes the current database.

2. Basic Syntax

The basic syntax is:

USE database_name;

Replace database_name with the name of the database you want to select.

3. Selecting a School Database

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.

4. Creating and Selecting a 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.

5. USE Does Not Create a Database

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.

6. Checking Available Databases

Before selecting a database, you can view databases accessible to the current user.

SHOW DATABASES;

Then select the required database.

USE school;

7. Checking the Current Database

Use the DATABASE() function to check which database is currently selected.

SELECT DATABASE();

For example:

USE school;

SELECT DATABASE();

8. Result of 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.

9. Creating a Table After USE

After selecting a database, you can create tables inside it.

USE school;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100)
);

10. Showing Tables After USE

The SHOW TABLES command displays tables from the currently selected database.

USE school;

SHOW TABLES;

11. Inserting Data After USE

Once the database is selected, you can insert records into its tables.

USE school;

INSERT INTO students
(id, name)
VALUES
(1, 'Rahul');

12. Selecting Data After USE

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.

13. Switching to Another 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.

14. Multiple Database Example

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.

15. USE in MySQL Workbench

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.

16. USE in MySQL Command Line

The same statement can be used in the MySQL command-line client.

mysql> USE school;

MySQL will select the database for the current session.

17. Selecting a Library Database

Suppose a library database already exists.

USE library;

You can then work with tables such as:

books
students
issues
returns

18. Selecting a Company Database

For a company application, you could select:

USE company_db;

You could then work with employee, department, and project tables stored in that database.

19. Selecting a Hospital Database

For a hospital management system:

USE hospital_db;

The selected database can contain tables such as patients, doctors, appointments, and billing.

20. Using Fully Qualified Table Names

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.

21. USE and Table Creation

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.

22. USE and INSERT

After selecting the database, you can insert records into its tables.

USE school;

INSERT INTO teachers
(id, name, subject)
VALUES
(1, 'Amit Kumar', 'Mathematics');

23. USE and UPDATE

You can also update records after selecting the database.

USE school;

UPDATE teachers
SET subject = 'Physics'
WHERE id = 1;

24. USE and DELETE

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.

25. Database Does Not Exist

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.

26. No Database Selected

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;

27. Checking Before Working

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.

28. Common USE Errors

Common problems include:

  • Database name is incorrect
  • Database does not exist
  • User does not have access
  • Database name is misspelled
  • Working with an unexpected database

29. Complete USE Example

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.

30. Practical USE Database Example

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.

📌 Key Points

  • USE selects a database for the current MySQL session.
  • The basic syntax is USE database_name;
  • USE does not create a database.
  • The database should already exist before selecting it.
  • SHOW DATABASES can be used to view accessible databases.
  • SELECT DATABASE() checks the currently selected database.
  • You can switch databases by running another USE statement.
  • Tables can be created after selecting a database.
  • You can use fully qualified names such as school.students.
  • Selecting the correct database helps prevent working with the wrong data.

🧠 Quick Quiz

Question: Which statement is used to select a database in MySQL?