SQL Select database
Last Updated :
06 Oct, 2023
In SQL, we always need to Select our Database before starting to work with the database. Once we create a database we need to Select the database first to tell MYSQL in which database we are trying to work. Before proceeding with the creation of tables and inserting records we need to first Select our database. If we directly start creating tables then MYSQL will give an error. So note that it is mandatory to always select or use the database before actually starting working with the database. To Select the database we use the USE command. In this article, we will thoroughly learn about how to Select a database before actually working with it.
Syntax:
USE DATABASE_NAME;
How to Select a Database?
To learn how to Select a Database in SQL follow the below-mentioned steps:
Step 1: Creating the database
To start things with SQL first, we need to create a new database in SQL. We use the CREATE DATABASE command to create a new database in MYSQL and then we mention the name of the database. Now as an example, we will create a new database with the name GeeksForGeeks.
Query
CREATE DATABASE GEEKSFORGEEKS;
Output
Creating new database
Step 2: Verifying the Database
Now we will verify whether the new database that we have just created has been successfully added to our system or not. We use the SHOW DATABASES command and it will return a list of databases to our system. Now we will look for the name of the database (GeeksForGeeks) that we have just created.
Query
SHOW DATABASES;
Output
Database successfully created
Step 3: Selecting the Database
After creating the database our main task is to Select or use the database and for that purpose, we use the USE command. So now we will select our database GeeksForGeeks. After executing the command the output will be Database Changed meaning that the database in which we are writing SQL at the time has changed.
Query
USE GEEKSFORGEEKS;
Output
Selecting the Database
Step 4: Creating a table
Now that we have selected our database we can proceed with creating new tables and then we can insert records also in the table without any problem. But in case we haven’t selected the database MYSQL would have given an error which we will discuss in the next step. We will use the CREATE TABLE command to create a new table.
Query
CREATE TABLE EMPLOYEE(
EMP_ID INT,
NAME VARCHAR(20),
DOB DATE,
AGE INT,
SALARY DECIMAL(7,2));
Output
Creating a table
Creating a Table Without Selecting the Database
Now let’s take a case where we haven’t selected the database but we are creating a new table. In this case, we will get an error from MYSQL stating ‘No database selected’ meaning that as no database was selected MYSQL cannot figure out in which database we are creating this table.
No database selected error is displayed
Conclusion
In SQL, always Select the Database before starting to work with the database. After creating a database we need to Select the database first to tell MYSQL which database we are working on. Before proceeding with operations like the creation of tables or inserting records we need to first Select our database. If we directly start creating tables then MYSQL will give an error stating that ‘No Database Selected’. To Select the database we use the USE command. To learn about how to select a database follow the above-mentioned steps.
FAQs on SQL Select Database
Q.1: Why do we need to Select a Database?
Answer:
We need to Select a Database to state MYSQL about the database we will be using for storing the data.
Q.2: What if we don’t select the database?
Answer:
In case you don’t select the database and start working with it, MYSQL we give you an error ‘No database selected’.
Q.3: How to select a database in MYSQL?
Answer:
To select a database in MYSQL we take help of USE command.
Share your thoughts in the comments
Please Login to comment...