SQL Drop/Delete Database

Syntax:

DROP DATABASE [IF EXISTS] database_name;
  • database_name: The name of the database to be dropped.
  • IF EXISTS: Optional. It prevents an error from occurring if the database does not exist.

Example:

Let's say you have a database called "company:"

CREATE DATABASE company;

To drop the company database:

DROP DATABASE company;

This statement will delete the entire database, including all its tables and data.


Drop Database with IF EXISTS:

To avoid an error in case the database does not exist, you can use the IF EXISTS clause:

DROP DATABASE IF EXISTS company;

This is useful when you want to prevent accidental errors or when you are unsure whether the database exists.


Tips:

  • Double-check the database name before executing the DROP DATABASE statement.
  • Make sure that you have a backup of the data if needed.
  • Consider using IF EXISTS to prevent errors if the database does not exist.