LogIn
I don't have account.

How to Drop a Table in SQL

DevSniper
157 Views

The DROP TABLE command in SQL is part of the Data Definition Language (DDL) and is used to permanently delete a table from a database. This action is irreversible and deletes the table structure, its data and associated metadata, including indexes, constraints and permissions.

This command should be used with caution, because it permanently deletes the table and all its contents.

In this article, we will explore how DROP TABLE command works, its syntax, use cases and so on. By the end of this article you have clear understanding of DROP TABLE operation on table.

Syntax of DROP TABLE Command

DROP TABLE TableName;

Key Features of DROP TABLE Command

  • Completely removes a table and its data from the database.
  • Deletes the table's structure, indexes, constraints and relationships.
  • Cannot be rolled back once executed (in most cases).
  • Requires DROP privileges to run this query.

Let's Understand DROP TABLE Command Behavior with Example

Let's consider a scenario, where we create an Employees table and insert some records in table.

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(50),
    Mobile VARCHAR(50),
    DepartmentID INT,
    Salary DECIMAL(10, 2)
);
INSERT INTO Employees (Name, Mobile, DepartmentID, Salary)
VALUES
    ('Ram', '1234567890', 101, 50000.00),
    ('Rajesh', '9876543210', 102, 60000.00),
    ('Deepak', '5551234567', 103, 55000.00);

Now run SELECT Query on Employess

SELECT * FROM Employees;
EmployeeID Name	     Mobile	DepartmentID	Salary
1	   Ram	     1234567890	  101	      50000.00
2	   Rajesh    9876543210	  102	      60000.00
3	   Deepak    5551234567	  103	      55000.00

Now run DROP TABLE query and after that SELECT query.

DROP TABLE Employees;
SEELECT * FROM Employees;
00:23:58   SELECT * FROM Employees LIMIT 0, 1000	
Error Code: 1146. Table 'Employees' doesn't exist  0.016 sec

Using IF EXISTS Clause

If the table does not exist, you will get an error. To avoid this, you can use the IF EXISTS clause .

If you are planning to drop a table but not sure at the time of query execution, is table exist or not or if there is chance that your query might run more than one times. In such cases to avoid error IF EXIST clause is helpful

DROP TABLE IF EXISTS TableName;
Example

Let's consider a scenario, where you need to drop the Employees table, and there is a possibility that the query might execute multiple times. However, your goal is to ensure that the table is deleted if it exists, without encountering any error if the table does not exist. In such case you should use IF EXIST clause. so your drop query will be like

DROP TABLE IF EXISTS Employees;

Dropping Multiple Tables

If you need to delete more than one table in single query. you can drop multiple tables in a single statement by separating them with commas.

DROP TABLE TableName1, 
TableName2, TableName3, ......;

Attempting to Drop a Table with a Foreign Key

If we attempt to drop a table which column is used as Foreign key in some other tables. the drop table query will throw an error

Let's explore with Example, Create two tables Departments and Employees with Foreign Key

CREATE TABLE Departments ( 
    Id INT PRIMARY KEY AUTO_INCREMENT, 
    Name VARCHAR(100) 
);
CREATE TABLE Employees (
    EmployeeId INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(100),
    Mobile VARCHAR(15),
    DepartmentId INT,
    Salary DECIMAL(10, 2),
    CONSTRAINT FK_Department FOREIGN KEY (DepartmentId) REFERENCES Departments(Id)
);

Now let's Drop Departments table.

DROP TABLE Departments;
01:07:28 DROP TABLE Departments	
Error Code: 3730. Cannot drop table 'departments' referenced by a foreign key constraint 'FK_Department' on table 'Employees'. 0.000 sec

Notes

  • DROP TABLE command is a powerful and efficient way to remove a table from a database.
  • There is no way to recover the table including its data, so be careful before executing this query.
  • Once a table is dropped, its data and structure are lost permanently.
  • Always ensure you have backups before performing this operation.
  • Dropping a table that is referenced by foreign keys in other tables may result in errors unless you explicitly drop or update the constraints.
  • To execute DROP query on table you need DROP privilege on the table.
  • It is useful to remove obsolete or tables that is no longer required.
  • Dropping a table frees up the space occupied by its data and indexes.
  • The table being dropped is locked during the operation to ensure data integrity.
  • Dropping large tables with significant data or indexes can take time as the database releases all associated resources.