HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL Select Database

A MySQL server can hold many databases at the same time. Before you can create tables, insert data, or run queries, you need to tell MySQL which database you want to work with. This is done using the USE statement, which sets the active database for your current session.


Syntax

USE database_name;

After running this command, every subsequent SQL statement — such as CREATE TABLE, SELECT, INSERT — will automatically apply to the selected database until you either switch to another database or close the connection.


Basic Example

To start working with the school database:

USE school;

MySQL responds with:

Database changed

Now any query you run will target the school database. For example, after selecting it, you can view all its tables:

USE school;
SHOW TABLES;

Checking the Currently Active Database

If you are unsure which database is currently selected, use the DATABASE() function:

SELECT DATABASE();

This returns the name of the active database. If no database has been selected yet, it returns NULL.

-- Example output after USE school;
+------------+
| DATABASE() |
+------------+
| school     |
+------------+
Tip: In MySQL Workbench, the active database is shown in bold in the left-side Navigator panel. You can also double-click any database in the Navigator to select it — this is the GUI equivalent of the USE statement.

Switching Between Databases

You can switch to a different database at any point during your session simply by running USE again with the new database name:

USE school;
-- ... work with school database ...

USE ecommerce;
-- ... now working with ecommerce database ...

The switch is instant. There is no need to disconnect and reconnect — MySQL updates the active database for your session immediately.


Accessing Tables Without USE

If you need to query a table in a specific database without switching to it first, you can use the fully qualified table name format: database_name.table_name.

-- Query the 'students' table in the 'school' database
-- without running USE school first
SELECT * FROM school.students;

This is especially useful when you need to join tables across two different databases in a single query:

SELECT s.name, o.order_date
FROM school.students AS s
JOIN ecommerce.orders AS o ON s.id = o.student_id;
Note: The USE statement only affects your current session. Other users connected to the same server are not affected — each session maintains its own active database independently.

Key Points to Remember

  • USE database_name; sets the active database for your current session.
  • After USE, all SQL statements automatically target that database.
  • Use SELECT DATABASE(); to check which database is currently active.
  • You can switch databases at any time by running USE again.
  • Use the database_name.table_name syntax to access tables without switching databases.
  • USE only affects your own session — other connections are unaffected.