×

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands.

What is Drop Command?

Drop is a Data Definition Language command in the Structured Query Language. The Drop command in SQL is used to delete or drop the table definition, the database, indexes, view, triggers, and data constraints from the database tables.

 In the Structured Query Language, the drop command is used to drop the elements from the relational database management system (RDBMS).

The Delete command can roll back data once it is performed, but the Drop commands cannot roll back data once it is performed. The Drop command is used to free memory where table space is stored because drop commands delete the table and its contents permanently.

The Drop command is fast as compared to the truncate command in the Structured Query Language. But, the execution timing of the drop command is slow because it has many complications. 

To delete one or more columns from the table, we use the drop command with the ALTER TABLE command.

The syntax for DROP command to remove the database is as follow:

DROP DATABASE Database_Name;

In the above syntax, we have to specify the database name that we want to delete from the system.

Note: - Be careful before using the DROP command for deleting the database because the drop commands remove all the objects from the database like tables, indexes, views, triggers created in the database.

The syntax for removing the table is as follow:

DROP TABLE Table_Name;

In the above syntax, we have to specify the table name that we want to delete from the database.

The syntax for removing the multiple tables from the same database in a single statement is as follow:

DROP TABLE table1, table2, table3;

We can delete multiple tables from the same database using the above syntax.

The syntax for removing the index is as follow:

DROP INDEX Index_Name;

We have to specify the index name in the above syntax just after the Index Keyword.

The syntax for removing the view is as follow:

DROP VIEW View_Name;

We have to specify the view name in the above syntax just after the View Keyword.

The syntax for removing the constraint from the table using the ALTER TABLE is as follow:

ALTER TABLE Table_Name DROP CONSTRAINT Constraint_Name;

Using the ALTER TABLE command in the SQL, we can delete the constraint from the table.

Example of DROP command:

We will take a few examples using the DROP command.

Step 1: Create a database or use an existing database. We will use two databases for the DROP command.

Step 2: Create a new table or use an existing table, index, view.

Example 1: Write a query to drop table students_information from the school database.

We will first select the School database using the USE Keyword followed by the database name.

USE School;

We will write the drop command to delete the students_information table from the selected database.

Consider the table Students_information with the following records:

Student_IdStudent_NameStudent_CourseStudent_Marks
1AnjaliBCOM85
2PranavBCA80
3YogeshB.E88
4BhushanMBA95
5PoonamMCOM97
6BhaveshB.E90
7KhushiBSC94
8PiyushBCOM75
9NikitaBA88
10AishwariyaBSC70
DROP TABLE Students_Information;

We have dropped the Students_information table with all the records present inside the table from the above query.

To cross check whether the query was executed successfully or not, we will execute the SELECT query.

SELECT * FROM Students_Information;
Drop Vs Truncate In SQL

As we execute the select query on the Students_information table, the table doesn't exist message is displayed, which means the Drop command query on the Students_information table is executed successfully.

Example 2: Write a query to drop manager_view1 view from the company database.

Consider the manager_view1 view with the following records:

MANAGERIDMANAGER_NAMEMANAGER_DEPARTMENT
1Snehdeep KaurORACLE
3Abhishek ManishJAVA

We will write the drop command to delete the manager_view1 view from the selected database.

DROP VIEW Manager_view1;

We have dropped the manager_view1 view from the above query with all the records present inside the view.

To cross_check whether the query was executed successfully or not, we will execute the SELECT query.

SELECT * FROM Manager_view1;
Drop Vs Truncate In SQL

As we execute the select query on the manager_view1 view, the view doesn't exist message is displayed, which means the Drop command query on the manager_view1 view is executed successfully.

Example 3: Write a query to drop the Cricket database from the system.

Before executing the drop command for the database, we first execute the show databases command to verify at the end of the drop command that we successfully dropped the Cricket database from the system.

SHOW DATABASES;
Database
Chk
Company
Cricket
Employee
Information_schema
Mysql
Performance_schema
Phpmyadmin
School
Test
WordPress
Drop Vs Truncate In SQL

Above are the databases that already exist in the system; we will drop the Cricket database from the system. Dropping the database means deleting all the objects present inside the database table, view, index, triggers, etc.

Now, we will execute a query for dropping the Cricket database.

DROP DATABASE Cricket;

We have executed the drop query on the Cricket database. To check if the database is dropped successfully or not, we will execute the SHOW DATABASES query.

SHOW DATABASES;
Database
Chk
Company
Employee
Information_schema
Mysql
Performance_schema
Phpmyadmin
School
Test
WordPress
Drop Vs Truncate In SQL

As you can see, the cricket database is dropped from the system successfully.

Example 4: Write a query to delete the last name column from the employee table.

ALTER TABLE employee DROP Last_Name;

We have deleted the Last_Name column from the employee table using the DROP command with the ALTER command in the above query.

FieldTypeNullKeyDefaultExtra
EMPLOYEEIDint(11)NOPRINULL
FIRST_NAMEvarchar(20)YESNULL
SALARYint(11)YESNULL
CITYvarchar(20)YESNULL
DEPARTMENTvarchar(20)YESNULL
MANAGERIDint(11)YESNULL
Drop Vs Truncate In SQL

The above output shows that Last_Name is deleted from the employee table.

What is Truncate Command?

Truncate is another Data Definition Language command in the Structured Query Language. The Truncate command is used to delete all the records from the table. Like the DROP command, but the TRUNCATE command does not contain a WHERE clause. The Truncate command is faster than both the DROP and the DELETE command. We cannot roll back the records like the DROP command after using the TRUNCATE command.

Syntax of TRUNCATE command in SQL:

TRUNCATE TABLE tablename;

In the above syntax, we have to mention the table name whose data we want to delete from the table.

We will take a few examples using the TRUNCATE command.

Example of TRUNCATE command:

Step 1: Create a database or use an existing database.

Step 2: Create a new table or use an existing table.

Consider the existing table with the following records:

Table: Employee:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Example: Write a query to delete all the records from the employee table using the truncate command.

TRUNCATE TABLE Employee;

We have deleted all the records from the above query from the employee table.

To cross check whether all the records from the employee table are deleted or not successfully, we will execute the SELECT query on the employee table.

SELECT * FROM Employee;
EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENT  MANAGERID
Drop Vs Truncate In SQL

As we execute the select query on the employee table, the empty set message is displayed, which means the truncate command on the employee table is executed successfully.

The below points show the difference between the Drop command and the Truncate command in the Structured Query Language:

The DROP CommandThe TRUNCATE Command
The DROP command deletes the table structure and table records.The TRUNCATE command drops all records from the table.
The DROP command is a Data Definition Language command.The TRUNCATE command is also a Data Definition Language command.
In the DROP command, table space is free from memoryThe TRUNCATE command does not free the table space from the memory.
The table view does not exist in the DROP commandThe table view exists in the TRUNCATE command
In the DROP command, we cannot use delete space.In the TRUNCATE command, we can use delete space but less than compared to the DELETE statement.
In the DROP command, the integrity constraints will be removed automatically from the table.  The integrity constraints will not be removed from the table in the TRUNCATE command.
The DROP command deletes the records fast, but there are many complications.The TRUNCATE command is faster than the DROP command.

Related Topics

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.