CREATE, SELECT, and DROP Database

Introduction

In this article, we will be learning how to CREATE, SELECT, and DROP Database.

First, we will learn what a database is and what it can do. Then we will know what a CREATE DATABASE command is. Subsequently, we will learn about the DROP DATABASE command and the USE DATABASE command.

Then we will learn about the syntax of CREATE, SELECT, and DROP Database commands, accordingly and see how to use them in a real-time environment.

We will also see some suitable examples of CREATE, SELECT, and DROP Database commands, making it easier to use them.

Let us begin with the fundamental terminologies.

Database

A database is a well-defined collection of information that we store in a structured manner, typically in a computer system. So we need to create a database first and then try to access it using different SQL commands.

Create a database

The first and foremost thing to learn about SQL is to know how to CREATE a database.

To create a database, we use the CREATE DATABASE command.

Note:- Two database names in the particular SQL schema can not  be the same

Delete a database

If we have learned how to create a database, we will learn how to drop or delete it if we do not require it.  

To delete or drop a database, we use the DROP DATABASE command.

Select a database

If we have created many SQL databases, we must select a particular database to apply the desired operations. We use the USE command in my SQL to choose an existing database.

Note:- We must create a database before using it to apply operations.

Syntax

1.  For CREATE DATABASE Command

The general syntax to use the CREATE DATABASE command is below:-

CREATE DATABASE database_name;

We can give any name to our database according to our convenience.

2. For DROP DATABASE  Command

The general syntax to use the DELETE DATABASE command is below:-

DROP DATABASE database_name;

We should drop only those databases we have created or already exist.

3. For USE Command

The general syntax to use the USE command is as follows;-

USE DatabaseName;

We should select the database on which we want to apply operations.

Examples

1.  Let us now see how can we create a database with the help of the following example

Suppose we want to create a database whose name is demoDb, Then we have to write the following code.

CREATE DATABASE demoDb;

Before creating a database, we must ensure that we have admin privileges ( generally not available on online compilers). Or you can download the MySQL workbench to create a database. Once we have successfully created a database, we can check it in the list of databases by using the SQL command given.

SHOW DATABASE;

Output

SHOW DATABASE;

 

DATABASE

______________________________________________________________________

codingninjas

workspace

demoDb

 

3 rows in set

2. To delete or drop a database. Suppose we want to delete a pre-existing database named codingninjas, then we need to execute the following SQL query. 

DROP DATABASE codingninjas;

3. To select a database. Suppose we want to apply some operations on the database named “demoDb,” then we need to select this. The following SQL query will select the desired database.

USE demoDb;

Frequently asked questions

  1. Write a SQL query to create a database of the name Codestudio?

ANS -

CREATE DATABASE Codestudio;

2. How to change the database which is in use to another database?

ANS- You can change the database according to your need with the help of the USE command. For example, we may execute the following query if we want to use the Codestudio database.

USE Codestudio;

3. How to check whether we have successfully created the database or not?

ANS- You can check the name of the database in the list of databases created with the help of the SHOW DATABASE command.

Key takeaways

This article has successfully taught you how to CREATE, SELECT, and DROP Database.

We also saw some of the queries that will help us use the database. For example, we created a database named “demoDb” with the CREATE DATABASE command.

We learned to delete an existing database that is not of our use. We did it with the help of the DELETE DATABASE command.

We also learned to select a particular database on which we want to work with the help of the USE command.

NInjas! You have taken the first yet most crucial step in learning SQL.

Do not stop here and learn all of the most required topics that will help you ace the placement interviews by joining our courses. You can practice some Important questions here to improve your knowledge of DBMS. Also try CodeStudio for complete interview preparation.

Was this article helpful ?
1 upvote

Comments

No comments yet

Be the first to share what you think