×

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

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute 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 DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.