The Wayback Machine - https://web.archive.org/web/20240930225933/https://www.geeksforgeeks.org/sql-drop-table-statement/
Open In App

SQL DROP TABLE

Last Updated : 26 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

CategoriesTables

Example of DROPPING A TABLE

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

Query:

DROP TABLE categories;

Output:

droptable

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.

FAQs on SQL DROP TABLE

What happens if I try to drop a table that doesn’t exist?

If you try to drop a non-existent table, SQL will return an error. To prevent this, you can use the DROP TABLE IF EXISTS statement, which will only execute the drop if the table exists.

Can I recover a table after using the DROP TABLE command?

No, once a table is dropped, all its data, structure, and constraints are permanently deleted. You cannot recover it unless you have a backup of the database.

Is there a difference between dropping a regular table and a temporary table?

Yes, when dropping a temporary table, you must include the TEMPORARY keyword in the DROP TABLE statement. This ensures that only the temporary table is deleted without affecting any regular tables in the database


Previous Article
Next Article

Similar Reads

SQL Server DROP TABLE
In SQL Server, the DROP TABLE statement is used to remove or delete permanently from the database. In SQL Server after performing the DROP Operation we cannot revoke the table or database again, because it is an irreversible action. The Drop Operation is different from the DELETE Command, In the DELETE command we can revoke the database or table af
3 min read
SQL Server ALTER TABLE DROP COLUMN
In SQL Server, there could be some situations when we may have to delete or drop a column from a table. Sometimes the column in a table was created wrongly or maybe it is no longer required and has become obsolete. So, to drop a column from a table, the ALTER TABLE, DROP COLUMN SQL query is used. In this article, we will discuss how to drop a colum
4 min read
SQL Server DELETE and DROP TABLE Commands
In SQL Server, managing data involves not only inserting and updating records but also removing them when they're no longer needed. Two important commands for data removal are DELETE and DROP TABLE. These commands play crucial roles in maintaining database integrity and managing database structures. In this article, we'll learn about the DELETE and
4 min read
Table 20 to 25 - Multiplication Table Chart and 20-25 Times Table
Multiplication Tables of 20, 21, 22, 23, 24, and 25 are provided here. Tables from 20 to 25 is a list of tables that contain multiplication of numbers 20 to 25 with integers 1 to 10. For eg. a multiplication table of 21 will show results when 21 is multiplied by numbers 1 to 10. Tables in Maths are also called Multiplication Tables. Table of Conten
6 min read
Table of 2 - Multiplication Table Chart and 2 Times Table
Table of 2 is a multiplication table in which multiples of two are written. Table of 2 is the most important table taught to students to speed up their calculations. Learning table of 2 is very important for every student to perform in good mathematics. Students are asked to learn tables especially the table of 2 to speed up their calculations. The
8 min read
How to Select All Records from One Table That Do Not Exist in Another Table in SQL?
We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries. In this let us see How to select All Records from One Table That Do Not Exist in Another Table step-by-step. Creating a Database Use the below command to create a database named Geeks
2 min read
SQL Query to Filter a Table using Another Table
In this article, we will see, how to filter a table using another table. We can perform the function by using a subquery in place of the condition in WHERE Clause. A query inside another query is called subquery. It can also be called a nested query. One SQL code can have one or more than one nested query. Syntax: SELECT * FROM table_name WHERE col
2 min read
Python MariaDB - Drop Table using PyMySQL
MariaDB is an open source Database Management System and its predecessor to MySQL. The pymysql client can be used to interact with MariaDB similar to that of MySQL using Python. In this article we will look into the process of Dropping a table from a database using pymysql. To drop a table use any of the below syntax: Syntax : DROP TABLE tablename;
2 min read
Drop Table in MariaDB
MariaDB is one of the most widely used open-source relational database management systems, it offers many useful commands for manipulating database structures. It supports the same features that MySQL does but with additional features. Some of the most significant features are new storage engines, JSON API support, and parallel data replication. On
4 min read
MySQL DROP TABLE Statement
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides various rich features to create databases and tables, insert data in them, and further manipulate them as the system evolves. In this article, we are
6 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Drop login in SQL Server
The DROP LOGIN statement could be used to delete an user or login that used to connect to a SQL Server instance. Syntax - DROP LOGIN loginname; GO Permissions : The user which is deleting the login must have ALTER ANY LOGIN permission on the server. Note - A login which is currently logged into SQL Server cannot be dropped. If you try to drop login
1 min read
DROP SCHEMA in SQL Server
The DROP SCHEMA statement could be used to delete a schema from a database. SQL Server have some built-in schema, for example : dbo, guest, sys, and INFORMATION_SCHEMA which cannot be deleted. Syntax : DROP SCHEMA [IF EXISTS] schema_name; Note : Delete all objects from the schema before dropping the schema. If the schema have any object in it, the
1 min read
SQL Query to Drop Unique Key Constraints Using ALTER Command
Here, we see how to drop unique constraints using alter command. ALTER is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Syntax : ALTER TABLE table_name DROP CONSTRAINT unique_constraint; For instance, consider the below table 'Employee'. Create a Table: C
2 min read
SQL DROP CONSTRAINT
In SQL, the DROP CONSTRAINT command is used to remove constraints from the columns of a table. Constraints are used to limit a column on what values it can take. We add constraints to our columns while we are creating the structure of tables in SQL and when we are actually inserting values in those tables sometimes we find that there is no further
3 min read
SQL DROP COLUMN
The Data Definition Language (DDL) command DROP COLUMN is used in SQL (Structured Query Language). The structure of database objects like tables can be defined, altered, or deleted using DDL statements in SQL. A column can be removed from a database table using the DROP COLUMN command. The table is altered permanently by this command. There is no g
3 min read
PL/SQL DROP VIEW
Views serve as an important tool for simplifying complex queries, enhancing data security, and streamlining database management. They provide a virtual layer that presents data from underlying tables in a structured format. However, views like other database objects need to be managed effectively to maintain data integrity and optimize performance
3 min read
SQL DROP INDEX Statement
The SQL DROP INDEX statement removes an existing Index from a database table. SQL DROP INDEXThe SQL DROP INDEX Command is used to remove an index from the table. Indexes occupy space, which can cause extra time consumption on table modification operations. Benefits of Using DROP INDEX:Removing unused indexes can improve INSERT, UPDATE, and DELETE o
3 min read
DROP and TRUNCATE in SQL
DROP and TRUNCATE in SQL remove data from the table. The main difference between DROP and TRUNCATE commands in SQL is that DROP removes the table or database completely, while TRUNCATE only removes the data, preserving the table structure. Let's understand both these SQL commands in detail below: What is DROP Command?DROP command in SQL is used to
4 min read
Create, Alter and Drop schema in MS SQL Server
Schema management in MS SQL Server involves creating, altering, and dropping database schema elements such as tables, views, stored procedures, and indexes. It ensures that the database structure is optimized for data storage and retrieval. In this article, we will be discussing schema and how to create, alter, and drop the schema. What is Schema?A
4 min read
SQL Query to Drop Foreign Key Constraint Using ALTER Command
A foreign key is included in the structure of a table, to connect multiple tables by referring to the primary key of another table. Sometimes, you might want to remove the primary key constraint from a table for performance issues or other requirements. A foreign key constraint can be removed from a table using the ALTER TABLE command and DROP CONS
3 min read
Difference between DROP and TRUNCATE in SQL
In SQL, the DROP and TRUNCATE commands are used to remove data, but they operate differently. DROP deletes an entire table and its structure, while TRUNCATE removing only the table data. Understanding their differences is important for effective database management. In this article, We will learn about the difference between DROP and TRUNCATE in SQ
3 min read
CREATE and DROP INDEX Statement in SQL
The CREATE INDEX statement in SQL is a powerful tool used to enhance the efficiency of data retrieval operations by creating indexes on one or more columns of a table. Indexes are crucial database objects that significantly speed up query performance, especially when dealing with large datasets. In this article, We will learn about How to CREATE an
4 min read
SQL DROP DATABASE
SQL DROP DATABASE statement is a powerful command used to permanently delete an existing database from a database management system (DBMS). In this article, We will learn about SQL DROP DATABASE in detail by understanding various examples in detail. SQL DROP DATABASESQL DROP DATABASE statement is used to delete an existing database from the databas
3 min read
PL/SQL Drop Index
In PL/SQL an index is a database object that improves the speed of the data retrieval operations on the table. However, there are situations where we might want to remove an index either to optimize performance or to retrieve disk space. In this article, we will provide a comprehensive guide on how to use the DROP INDEX statement in PL/SQL along wi
4 min read
Multiplication table till N rows where every Kth row is table of K upto Kth term
Given a number N, the task is to print N rows where every Kth row consists of the multiplication table of K up to Kth term. Examples: Input: N = 3 Output: 1 2 4 3 6 9 Explanation: In the above series, in every Kth row, multiplication table of K upto K terms is printed. Input: N = 5 Output: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 Approach: The idea is t
5 min read
How to Update a Table Data From Another Table in SQLite
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queries and operations on the database. In this article
3 min read
How to Select Rows from a Table that are Not in Another Table?
In MySQL, the ability to select rows from one table that do not exist in another is crucial for comparing and managing data across multiple tables. This article explores the methods to perform such a selection, providing insights into the main concepts, syntax, and practical examples. Understanding how to identify and retrieve rows that are absent
3 min read
Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another table, using an ID match. ID match ensures that the
3 min read