Table of contents
1.
Introduction
2.
What is a DROP statement?
3.
Examples of DROP Statements
3.1.
Create a Table
3.2.
Create Index
3.3.
Insert Values
3.4.
Dropping Column and Index
4.
What are TRUNCATE Statements?
5.
Example of TRUNCATE Statement
5.1.
Create a Table
5.2.
Insert Values
5.3.
Truncate table
6.
Difference between DROP and TRUNCATE in SQL
7.
Frequently Asked Questions
7.1.
What is the use of DROP Statements?
7.2.
What is the use of TRUNCATE Statements?
7.3.
What are DDL Statements in SQL?
7.4.
Which is faster, DROP or TRUNCATE statements in SQL?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference between DROP and TRUNCATE in SQL

Author Nidhi Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A statement or command in SQL is a set of instructions that properly executes queries. SQL statements include parameters, identifiers, variables, names, data types, and SQL-reserved words. It is the basic building block of SQL.

Difference between DROP and TRUNCATE

In this article, we will see the Difference between DROP and TRUNCATE in SQL and will cover both of statements in detail with the help of examples. 

What is a DROP statement?

Before discussing the difference between drop and truncate commands, let’s discuss the drop statement first.

The Drop statements are used to delete the existing tables in the database. In SQL, entities like tables, columns, databases, indexes, constraints, etc., are dropped with the DROP command. While using the drop statement, all the data or information related to these entities are lost.

The Syntax of the DROP statement is as follows:

DROP TABLE <table_name>

 

One can drop the entire database and all the related indexes, constraints, and columns using the following command:

DROP DATABASE<database_name>

 

Similarly, you can drop indexes, columns and constraints.

For dropping an index, use the following command:

DROP INDEX <index_name>;

 

Rules for the data in a table can be specified using SQL constraints. The categories of data that can be entered into a table are restricted by constraints. For dropping constraints, use the following command:

ALTER TABLE <table_name>
DROP CONSTRAINT <constraint_name>;

 

For dropping a column, use the following command:

ALTER TABLE <table_name>
DROP COLUMN <column_name>;

Also see, SQL EXCEPT

Examples of DROP Statements

Before understanding the difference between drop and truncate statements, use an example to understand the DROP statement thoroughly. Here is an example of a DROP Statement.

Create a Table

First, we will create the Ninja table with columns NInja_ID, Ninja_Name, Course, and Duration.

CREATE TABLE Ninja (
    Ninja_ID NUMBER PRIMARY KEY,
    Ninja_name VARCHAR2(50) NOT NULL,
    Course VARCHAR2(50) NOT NULL,
    Duration NUMBER NOT NULL
);

Create Index

Here we are creating an index for the age column.

CREATE INDEX n_Course ON Ninja(Course);

Insert Values

Here, we insert values in our Ninja table.

INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (1, 'Ninja A', 'CN', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (2, 'Ninja B', 'OOPs', '12 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (3, 'Ninja C', 'DBMS', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (4, 'Ninja D', 'CN', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (5, 'Ninja E', 'DSA', '12 weeks');

 

We will get the following table after inserting the values:

SQL Table

Dropping Column and Index

For dropping the Index:

DROP INDEX n_Course;

 

Output:

Index dropped.

 

For deleting the Column Course:

ALTER TABLE Ninja
DROP COLUMN Course;

 

Output:

SQL Table

For dropping the complete Table:

DROP TABLE Ninja;

 

Output:

Table dropped.

What are TRUNCATE Statements?

Before discussing the difference between drop and truncate commands, let’s discuss the truncate statement first.

In the database, TRUNCATE removes the rows and records from a table. Use TRUNCATE TABLE to eliminate every row from a large table while maintaining the table's structure. 

The Syntax of the TRUNCATE statement is as follows:

TRUNCATE TABLE <table_name>

Example of TRUNCATE Statement

Here is an example of a TRUNCATE Statement. We will use the same table to understand the difference between drop and truncate statements.

Create a Table

First, we will create the Ninja table with columns NInja_ID, Ninja_Name, Course, and Duration.

CREATE TABLE Ninja (
    Ninja_ID NUMBER PRIMARY KEY,
    Ninja_name VARCHAR2(50) NOT NULL,
    Course VARCHAR2(50) NOT NULL,
    Duration NUMBER NOT NULL
);

Insert Values

Here we are inserting values in our Ninja table.


INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (1, 'Ninja A', 'CN', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (2, 'Ninja B', 'OOPs', '12 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (3, 'Ninja C', 'DBMS', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (4, 'Ninja D', 'CN', '8 weeks');
INSERT INTO Ninja (Ninja_ID, Ninja_name, Course, Duration) VALUES (5, 'Ninja E', 'DSA', '12 weeks');

 

We will get the following table after inserting the values:

Output after inserting values

Truncate table

Use the following command to truncate the table.

TRUNCATE TABLE Ninja;

 

Output:

Table truncated.

Difference between DROP and TRUNCATE in SQL

Here is the comparison table to understand the difference between DROP and TRUNCATE statements.

Key Points DROP TRUNCATE
Feature To delete the existing table definition and content in databases. It is used to delete all the table rows in the database.
Table View There is no table view for the DROP command. A table view exists for the TRUNCATE command.
Fast It is slower than the TRUNCATE command. It is faster than the DROP command.
Undo Tablespaces Undo Tablespaces cannot be used. Undo Tablespaces can be used.
Memory Table space is free from memory in this command. Table space is not free from memory in this command.
Integrity DROP command removes the integrity constraint. Truncate command maintains the integrity constraints.

Frequently Asked Questions

What is the use of DROP Statements?

The Drop statements are used to delete the existing table in the database. You can also delete an entire database using the DROP Statement. It deletes a table definition and any associated data, triggers, indexes, constraints, and access requirements.

What is the use of TRUNCATE Statements?

An existing table's contents can be deleted using the SQL TRUNCATE statement. You wouldn't need to create that table again if you wanted to store some data because it wouldn't remove the entire table structure from the database.

What are DDL Statements in SQL?

DDL stands for Data Definition Language. It describes the database's structure or schema. It is a set of SQL commands that can create, delete, and modify the tables and schemas of the databases.

Which is faster, DROP or TRUNCATE statements in SQL?

The truncate statements in SQL are faster than the drop statements. The main reason behind the fast working of the truncate command is that they delete all the records from the tables without any transaction log creation.

Conclusion

In this article, we explored the Difference between DROP and TRUNCATE in SQL. Both DROP and TRUNCATE statements are DDL commands used for different purposes.

We hope this article helps you. You can visit more articles related to SQL Commands.

Read More, sql functions

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Live masterclass