Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

CREATE and DROP DATABASE in MySQL | Operations on a Database in MySQL

Ujjwal Abhishek
Ujjwal Abhishek

CREATE DATABASE

The CREATE DATABASE statement is used in MySQL to create a new database on a MySQL server. The syntax for this is shown below:

Syntax:

CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name]

In the above syntax:

  • database_name is specified after the CREATE DATABASE statement, and it must be unique.
  • IF NOT EXISTS is used to create the database only if it already doesn't exist on the server.
  • After that, the character set and collation are specified for the new database. This is optional, if not defined MySQL will set them to default for the new database.

For example:

CREATE DATABASE testdatabase;

Selecting database using the USE statement

The USE statement is used in MySQL to select an existing database on a MySQL server. The syntax for this is shown below:

Syntax:

USE database_name;

In the above syntax, the name of the database that we want to set is used with the USE statement to set it to the current database.

For example:

USE testdatabase;

The above query is used to set the current database to testdatabase.

It will show the message “database changed” which means the current database has been successfully set to testdatabase.

It can be verified by using the SELECT database() statement.

SELECT database();

The above query will return something like shown below:

DROP DATABASE

DROP DATABASE statement is used in MySQL to delete all the tables of a database and delete the database permanently from the server.

The syntax for this is shown below:

DROP DATABASE [IF EXISTS] database_name;

In the above syntax:

  • database_name is the name of the database that we want to delete from the server.
  • If the specified database doesn't exist, it will show an error.
  • The IF EXISTS condition is used to prevent that error, as it checks first that if the database specified to drop exists on the server or not.

All the existing databases can be shown using the SHOW DATABASES statement.

For example:

SHOW DATABASES 

It will return all the existing databases as shown below:

Now, the DROP DATABASE statement can be used to delete an existing database.

DROP DATABASE testdatabase;

The above query will delete all the tables and the testdatabase from the MySQL server.

Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet