In an ideal scenario, there are several databases present on an a database server. In order to perform an operation on data, you must first select the database using USE DbName statement and then you can perform an operation on a table, view or indexes.
It is important to select the database as more than one databases can have tables with the same name, if you are not selecting the database then you may accidentally perform an operation on a wrong database.
Syntax of USE statement
USE is the keyword and
DbName is the database name.
Example of USE statement in SQL
Generally we first list down all the databases before selecting the database, this make sure that we are scanning all the databases and selecting the right database.
This command list down all the databases that are present on the database server.
Let’s say it lists down the following databases.
SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | AbcTest | | Test | | Demo | | DB1 | | Employee | +--------------------+ 4 rows in set (0.00 sec)
To select the Employee database from the list, use the following statement.
This will select the Employee database for the further SQL commands and queries.
After this statement whatever operation you will perform like Create table, delete table etc. would be performed on the database “Employee”. Let’s say after making some modification, you may want to work with another database “Demo” then you can simply change the database by simply using the USE statement again.
This will select the database “Demo” for the further SQL queries and processing of data.