The Wayback Machine - https://web.archive.org/web/20240917111734/https://www.geeksforgeeks.org/sql-drop-truncate/
Open In App

DROP and TRUNCATE in SQL

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

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 delete a whole database or a table.

DROP statement deletes objects like an existing database, table, index, or view.

Syntax

DROP object object_name ;

DROP Command Examples

Let’s look at some examples of the DROP statement in SQL.

DROP Table

DROP TABLE table_name;

table_name: Name of the table to be deleted.

DROP database

Syntax:

DROP DATABASE database_name;

database_name: Name of the database to be deleted.

What is TRUNCATE Command?

TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the extent of a table for deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing several integrity-enforcing mechanisms.

It was officially introduced in the SQL:2008 standard. The TRUNCATE TABLE statement is logically (though not physically) equivalent to the DELETE Statement without a WHERE clause.

Syntax

TRUNCATE TABLE  table_name;

Where,

  • table_name: Name of the table to be truncated.
  • DATABASE name: student_data

Differences Between DROP and TRUNCATE

The key differences between DROP and TRUNCATE statements are explained in the following table:

DROPTRUNCATE
In the drop table data and its definition is deleted with their full structure.It preserves the structure of the table for further use exist but deletes all the data.
Drop is used to eliminate existing complications and fewer complications in the whole database from the table.Truncate is used to eliminate the tuples from the table.
Integrity constraints get removed in the DROP command.Integrity constraint doesn’t get removed in the Truncate command.
 Since the structure does not exist, the View of the table does not exist in the Drop  command.Since the structure exists, the View of the table exists in the Truncate command.
 Drop query frees the table space complications from memory.This query does not free the table space from memory.
It is slow as there are so many complications compared to the TRUNCATE command.It is fast as compared to the DROP command as there are fewer complications.

Read More: Differences between DROP and TRUNCATE Commands

SQL DROP AND TRUNCATE Statement Examples

Let’s look at some examples of the DROP and TRUNCATE statements in SQL and understand their working:

let’s consider the given database Student_data: 

Student Table

Student Table

To create this table, write the following queries:

SQL
CREATE TABLE Student_details (
ROLL_NO INT,
NAME VARCHAR(25),
ADDRESS VARCHAR(25),
PHONE INT ,
AGE INT); --Inserting the data in Student Table
INSERT INTO Student_details(ROLL_NO,NAME,ADDRESS,PHONE,AGE) VALUES 
(1,'Ram','Delhi',9415536635,24),
(2,'Ramesh','Gurgaon',9414576635,21),
(3,'Sujit','Delhi',9815532635,20),
(4,'Suresh','Noida',9115536695,21),
(5,'Kajal','Gurgaon',8915536735,28),
(6,'Garima','Rohtak',7015535635,23);

We will perform the examples on this particular table.

DROP Database Example

In this example, we will drop the student_data database.

Query:

DROP DATABASE student_data; 

After running the above query whole database will be deleted.

DROP Table Example

In this example, we will drop the student_details table;

Query:

DROP TABLE student_details; 

After running the above query whole table from the database will be deleted.

TRUNCATE Table Example

In this example, we will truncate the Student_details table from the student_data database.

Query:

TRUNCATE TABLE Student_details;

After running the above query Student_details table will be truncated, i.e, the data will be deleted but the structure will remain in the memory for further operations.

Important Points About SQL DROP & TRUNCATE Statements

SQL DROP Statement

  • Completely removes a table or database from the database, including the data and structure.
  • Is a permanent operation and cannot be rolled back.
  • Removes integrity constraints and indexes associated with the table.
  • Is slower compared to the TRUNCATE statement.

SQL TRUNCATE Statement

  • Removes all the rows or data from a table, but preserves the table structure and columns.
  • Is a faster operation compared to the DROP statement.
  • Resets the identity column (if any) back to its seed value.
  • Does not remove integrity constraints associated with the table.

Previous Article
Next Article

Similar Reads

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
Difference between DELETE, DROP and TRUNCATE
1. DELETE : Basically, it is a Data Manipulation Language Command (DML). It is used to delete one or more tuples of a table. With the help of the “DELETE” command, we can either delete all the rows in one go or can delete rows one by one. i.e., we can use it as per the requirement or the condition using the Where clause. It is comparatively slower
3 min read
SQL Server TRUNCATE TABLE
The TRUNCATE TABLE command in SQL Server allows you to rapidly and effectively remove every record from a table while maintaining the table structure. The TRUNCATE TABLE command is a more lightweight operation that is usually faster for large datasets than the DELETE statement, which eliminates rows one at a time and creates a larger transaction lo
6 min read
SQL TRUNCATE TABLE
The TRUNCATE TABLE statement in SQL is a powerful command used to swiftly remove all rows from a table, leaving the table structure intact. This operation is often favored over the DELETE statement for its efficiency, especially when dealing with large datasets. In this article, We will learn about SQL TRUNCATE TABLE in detail with the help of vari
5 min read
Difference between DELETE and TRUNCATE
DELETE is a DML(Data Manipulation Language) command and is used when we specify the row (tuple) that we want to remove or delete from the table or relation. The DELETE command can contain a WHERE clause. If the WHERE clause is used with the DELETE command, then it removes or deletes only those rows (tuples) that satisfy the condition; otherwise, by
4 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
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
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
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
Truncate All Tables in MySQL
TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the extent of a table for deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing a number of integrity-enforcing mechanisms. In SQL, truncate is used to delete all data from the table but doesn't del
2 min read
Truncate Table in MariaDB
MariaDB is an open-source relational database management system. It offers faster query execution and data manipulation as compared to other RDBMS. Also, it handles large datasets very efficiently. MariaDB Provides plugins for various functionalities like backup, performance monitoring, security, and data integration. MariaDB can easily integrate w
4 min read
TRUNCATE() Function in MySQL
The TRUNCATE() function in MySQL is a valuable tool for manipulating numerical values by removing their decimal parts without rounding. It allows us to limit the precision of numbers to a specified number of decimal places or even truncate them to the nearest integer. In this article, We will learn about the TRUNCATE() Function in MySQL in detail b
6 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
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
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
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
SQL DROP TABLE
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
3 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
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
Dynamic SQL and Temporary Tables in SQL Server
In SQL Server, creating and using Temp Tables using dynamic SQL is a good feature when we need to temporarily create tables at run time and delete automatically all within a session. They can be very useful when we need to store temporary data in a structured format and do data manipulation using Data Manipulation Language in SQL. In this article l
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read