This tutorial is part of the series Learn Basic SQL Queries Using MySQL. In this tutorial, we will discuss SQL queries to delete or drop a table in MySQL.
Drop Table Query
The query DROP TABLE can be used to delete or drop a database table. In case you are remotely logged in to the database, you will also need the DROP privilege for the table in order to delete a table.
The below-mentioned query drops the table user with all the row data in the enterprise database. We need to specify the table name while dropping a table.
# TODO - Drop/Delete Table
# Query - DROP TABLE <table name>
# It might throw error in case database do not exist or table already exist with the same name
DROP TABLE `enterprise`.`user`;
# Good to go
DROP TABLE IF EXISTS `enterprise`.`user`;
This is the very basic query to delete or drop a table in MySQL database. You must take table backup before dropping it to avoid any accidental data loss. Once dropped, the data can't be recovered and complete information will be lost.
Instead of dropping a table, we can also delete the table data without deleting the table using the TRUNCATE TABLE query as shown below.
# TODO - Delete Table Data
# Query - TRUNCATE TABLE <table name>
# It might throw error
TRUNCATE TABLE `enterprise`.`user`;
# Good to go
TRUNCATE TABLE IF EXISTS `enterprise`.`user`;
This is how we can drop a table in MySQL using the SQL query.