What is the USE command in SQL?

It’s usual to have multiple schemas in a database to organize objects into logical groups. Multiple schemas within a database make it more manageable and prevent users from interfering with each other’s database schemas.

In this shot, we will learn how to switch user context between different database schemas to perform operations on them.

The USE statement

Before giving an SQL query or command, you need to set the database’s context for where you need to perform the SQL query or command. You can set the context to a particular database in your DBMS by giving the USE command.

Syntax

USE <databaseName>

Parameters

<databaseName> is the name of the database schema where the user context must be switched to execute the SQL query or command.

Example

To perform operations on the database schema employee_db, you would use the following command to change the context to the employee_db database schema:

USE employee_db

The context switch to employee_db takes effect immediately after executing the command. The context remains in employee_db throughout the session, or until another USE command is executed.

Note: The user would need the required permission on the target database to execute this command.

Free Resources