SQL DROP TABLE

SQL DROP TABLE statement deletes data and structure of a table from the database.

DROP TABLE in SQL

DROP TABLE in SQL means that all of the table’s data, structure, constraints, permissions, etc. will be removed from the database.

DROP TABLE command is irreversible, so once a DROP TABLE command is executed for a table, all information about that table will be removed permanently.

SQL DROP TABLE Syntax

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.

SQL
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:

DROP TABLE Example

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

Query:

DROP TABLE categories;

Output:

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.