Skip to content

SQL DROP TABLE

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 so on.

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 and 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 of SQL DROP TABLE

The syntax to use the DROP TABLE command in SQL is:

DROP TABLE table_name;

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

Examples

Let’s look at some examples of the DROP TABLE statement in SQL to understand its working.

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

CREATE DATABASE Newcafe;
USE Newcafe;
CREATE TABLE [dbo].[categories]
(
    [CategoryID] INT NOT NULL PRIMARY KEY, 
    [CategoryName] NVARCHAR(50) NOT NULL,
    [ItemDescription] NVARCHAR(50) NOT NULL
);
INSERT INTO [dbo].[categories] ([CategoryID], [CategoryName], [ItemDescription])
VALUES
(1, 'Beverages', 'SoftDrink'),
(2, 'Condiments', 'Sweet and Savoury sauces'), 
(3, 'Confections', 'Sweet bread')

SELECT * FROM  categories;

The resulting table will look like this:

Categories table

Example of DROPPING A TABLE

In this example, we will drop the categories table using DROP TABLE statement.

Query:

DROP TABLE categories;

Output:

Drop table

Important Points About SQL DROP TABLE

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

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

  • Use DROP TABLE IF EXISTS query to prevent errors when dropping a table that does not exist

  • 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.

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

  • To verify if a table is dropped, you can use the DESC command.

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.