Working with Drop Database in MySQL

02.21.2022

Intro

The DROP DATABASE statement allows you to completely delete a database an all its contents. In this article, we will learn how to drop database in MySQL.

The Syntax

The basic syntax of DROP DATABASE is as follows:

DROP DATABASE [dbname];

You can also use the IF EXISTS which helps when running migrations from scratch.

DROP DATABASE IF EXISTS [dbname];

Getting Setup

We will be using docker in this article, but feel free to install your database locally instead. Once you have docker installed, create a new file called docker-compose.yml and add the following.

version: '3'
 
services:
  db:
    image: mysql:latest
    container_name: db
    environment:
      MYSQL_ROOT_PASSWORD: root_pass
      MYSQL_DATABASE: app_db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_user_pass
    ports:
      - "6033:3306"
    volumes:
      - dbdata:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: pma
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80

volumes:
  dbdata:

Next, run docker-compose up.

Now, navigate to http://localhost:8081/ to access phpMyAdmin. Then log in with the username root and pass root_pass.

Click the SQL tab and you are ready to go.

An Example

Let’s create a basic database for our workplace.

CREATE DATABASE workplace;

Now, we can check to make sure our database using the SHOW keyword.

SHOW DATABASES;

+--------------------+ | Database | +--------------------+ | workplace | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)

Next, let’s drop our database.

DROP DATABASE workplace;

We check to make sure our database is dropped by using SHOW again.

SHOW DATABASES;

+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)