Open In App

SQL DROP TABLE

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The DROP TABLE command in SQL is a powerful and essential tool used to permanently delete a table from a database, along with all of its data, structure, and associated constraints such as indexes, triggers, and permissions. When executed, this command removes the table and all its contents, making it unrecoverable unless backed up.

In this article, We will learn about SQL DROP TABLE by understanding its examples, and cover important points to help you manage your database tables effectively.

DROP TABLE in SQL

The DROP TABLE statement in SQL is used to delete a table and all of its data from the database permanently. This operation cannot be undone, and once the table is dropped all data in that table is lost. Once executed, this operation removes the table definition and all of its rows, so the table can no longer be accessed or used. This action is irreversible which means that once a table is dropped, it cannot be recovered unless there is a backup.

Syntax:

DROP TABLE table_name;

The syntax of the command remains the same in Oracle, SQL Server and MySQL.

Example of DROP TABLE in SQL

To demonstrate how to use the DROP TABLE command, let’s first create a database and a table, and then we will drop it.

Step1: Create a Database and Table

First, we will create a database and table on which the SQL queries will be run.

CREATE DATABASE NewCafe;
USE NewCafe;

CREATE TABLE categories (
CategoryID INT NOT NULL PRIMARY KEY,
CategoryName NVARCHAR(50) NOT NULL,
ItemDescription NVARCHAR(50) NOT NULL
);

INSERT INTO categories (CategoryID, CategoryName, ItemDescription)
VALUES
(1, 'Beverages', 'Soft Drinks'),
(2, 'Condiments', 'Sweet and Savory Sauces'),
(3, 'Confections', 'Sweet Breads');

SELECT * FROM categories;

Output:

CategoriesTables

At this point, the categories table has been created with three rows of sample data.

Step2: Drop the Table

Now, let’s use the DROP TABLE statement to delete the categories table permanently

Query:

DROP TABLE categories;

Output:

droptable

Important Points About SQL DROP TABLE

1. The SQL DROP TABLE statement is used to delete tables in a database, along with all associated data, indexes, triggers, constraints and permission specifications.

2. The table will be permanently disable, so use this query with caution.

3. Use DROP TABLE IF EXISTS query to prevent errors when dropping a table that does not exist. This command ensures that the drop operation only occurs if the table exists in the database.

DROP TABLE IF EXISTS categories;

4. When dropping a partitioned table, the DROP TABLE statement removes the table definition, all partitions, all data stored in those partitions, and all partition definitions. This operation also removes the partitioning scheme if no other tables use it.

5. The DROP TABLE statement can be used to drop temporary tables by including the TEMPORARY keyword.

DROP TEMPORARY TABLE temp_table_name;

6. To verify if a table is dropped, you can use the SHOW TABLES (MySQL) or SELECT * FROM INFORMATION_SCHEMA.TABLES (SQL Server, PostgreSQL) commands. Alternatively, in some systems, the DESC or DESCRIBE command can be used to check the table structure, but it will return an error if the table no longer exists.

SHOW TABLES;

Conclusion

The DROP TABLE command is an essential SQL function that helps in maintaining and cleaning up database environments. Its ability to permanently remove tables and all related data makes it a powerful tool, but it should be used cautiously to avoid accidental data loss. Understanding its syntax, options for error prevention, and its role in managing both regular and temporary tables is key to effective database management.



Next Article
Article Tags :

Similar Reads