SQL SELECT Database
In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples.
The database user or the administrator wants to create the tables, indexes, and views in the already present database in the SQL. We must select the database already in the SQL Server to create these objects.
The database users and the administrators can select the database from the server by executing the USE keyword followed by the database name in SQL.
The syntax of the select database is as follows:
USE Database_Name;
In the above syntax, we have used the USE keyword followed by the database name present in the Structured Query Language to select the database.
Let’s understand how to select the database with the help of examples.
Before selecting the database, we must be known the database name already present on the server. To be familiar with the database inside the server, we will execute the SHOW DATABASES query shown below:
SHOW DATABASES;
The output of the above query is as follows:
Database |
Copy |
Ids |
Information_schema |
Javatpoint |
Mumbai |
Mysql |
Performance_Schema |
Phpadmin |
Registration |
Student |
Student_1 |
Testdb |
Wordpress |
World |
Example 1: Suppose we want to create the tables, views, and indexes inside the ids name database, then we will execute the below query to select the ids database as shown below:
USE Ids;
In the above query, we have selected the Ids database, and now we are ready to create the tables, views, and indexes inside the ids database.
The output of the query is as follows:
The Ids database is now selected, and we are ready to create the tables, views, and indexes inside the ids database.
Example 2: Suppose we want to create the tables, views, and indexes inside the copy name database, then we will execute the below query to select the copy database as shown below:
USE copy;
In the above query, we have selected the copy database, and now we are ready to create the tables, views, and indexes inside the copy database.
The output of the query is as follows:
The copy database is now selected, and we are ready to create the tables, views, and indexes inside the copy database.
Suppose we want to work on another database not present on the server. For this, we will create the new database and execute the USE keyword followed by that database name to select the database.
Let’s create the new database name Sports as shown in the below query:
CREATE DATABASE Sports;
Now, we will execute the SHOW DATABASES query to check whether the Sports database has been created successfully or not:
SHOW Databases;
The output of the above query is as follows:
Database |
Copy |
Ids |
Information_schema |
Javatpoint |
Mumbai |
Mysql |
Performance_Schema |
Phpadmin |
Registration |
Sports |
Student |
Student_1 |
Testdb |
Wordpress |
World |
As we can see, the sports named database is successfully created. Now, we are ready to select the Sports named database.
Example 3: Suppose we want to create the tables, views, and indexes inside the Sports name database, then we will execute the below query to select the Sports database as shown below:
USE Sports;
In the above query, we have selected the Sports database, and now we are ready to create the tables, views, and indexes inside the Sports database.
The output of the query is as follows:
The Sports database is now selected, and we are ready to create the tables, views, and indexes inside the Sports database.
Let’s create another new database name Player, as shown in the below query:
CREATE DATABASE Player;
Now, we will execute the SHOW DATABASES query to check whether the Sports database has been created successfully or not:
SHOW Databases;
The output of the above query is as follows:
Database |
Copy |
Ids |
Information_schema |
Javatpoint |
Mumbai |
Mysql |
Performance_Schema |
Phpadmin |
Player |
Registration |
Sports |
Student |
Student_1 |
Testdb |
Wordpress |
World |
Example 4: Suppose we want to create the tables, views, and indexes inside the Player name database, then we will execute the below query to select the Player database as shown below:
USE Player;
In the above query, we have selected the Player database, and now we are ready to create the tables, views, and indexes inside the Player database.
The output of the query is as follows:
The Player database is now selected, and we are ready to create the tables, views, and indexes inside the Player database.