Using a Database
Introduction to SQL
Database
SQL Basics
Creating a Database
Using a Database
Creating a Table
Common Data Types
Inserting Data
Selecting Data
WHERE Clause
Comparison Operators
AND, OR, and NOT
ORDER BY Clause
LIMIT Clause
DISTINCT
LIKE Operator
IN, BETWEEN, and IS NULL
Updating Data
Deleting Data
ALTER TABLE
DROP TABLE
PRIMARY KEY and NOT NULL
FOREIGN KEY and Relationships
Aggregate Functions
GROUP BY Clause
HAVING Clause
JOIN Basics
INNER JOIN
LEFT JOIN
Aliases
Subqueries
Indexes
User Permissions and Security
Common Mistakes Beginners Make
Practice Ideas
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
schooldatabase 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
SELECTorINSERTquery, resulting in an "object not found" error.
Key Notes
- Switching Databases: You can use the
USEcommand 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
USEcommand again to reconnect to the correct database.
🏋️ Test Yourself With Exercises
Take our quiz on Using a Database to test your knowledge.
Browse Quizzes »