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.
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.
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;
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 |
+------------+
USE statement.
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.
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;
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.
USE database_name; sets the active database for your current session.USE, all SQL statements automatically target that database.SELECT DATABASE(); to check which database is currently active.USE again.database_name.table_name syntax to access tables without switching databases.USE only affects your own session — other connections are unaffected.