Home Tutorials SQL Tutorial Using a Database
Using a Database

Using a Database


Using a Database

Definition: The USE statement is used to select a specific database and tell the database management system (DBMS) that all following commands should be executed within that particular container.

Why: In practice-based lessons, this is a critical "setup" step. Since a server can hold hundreds of different databases, you must explicitly select the one you want to work with before you can create tables or query data inside it.


Syntax

The syntax is very simple. Use the USE keyword followed by the name of the database you previously created.

USE database_name;

Example

To start working with the school database you created in the previous module, you would run:

USE school;

Explanation

  • Active Session: Once this command is executed, the school database becomes the "current" or "active" database for your session.
  • Implicit Context: Without this command, the system might not know which table you are referring to if you try to run a SELECT or INSERT query, resulting in an "object not found" error.

Key Notes

  • Switching Databases: You can use the USE command at any time during your session to switch from one database to another.
  • GUI Tools: If you are using a visual tool (like phpMyAdmin or MySQL Workbench), you can often achieve the same result by simply double-clicking the database name in the sidebar menu.
  • Session Based: The selection is only for your current connection. If you close your SQL tool and reopen it later, you will usually need to run the USE command again to reconnect to the correct database.

🏋️ Test Yourself With Exercises

Take our quiz on Using a Database to test your knowledge.

Browse Quizzes »