The Wayback Machine - https://web.archive.org/web/20230614235323/https://www.geeksforgeeks.org/sql-drop-truncate/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

SQL | DROP, TRUNCATE

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

SQL commands are broadly classified into two types DDL, DML here we will be learning about DDL commands, and in DDL we will be learning about DROP and TRUNCATE in this article.

DROP

DROP is used to delete a whole database or just a table.

In this article, we will be learning about the DROP statement which destroys objects like an existing database, table, index, or view. A DROP statement in SQL removes a component from a relational database management system (RDBMS). 

Syntax:

DROP object object_name

Examples 1:

To Drop a table

DROP TABLE table_name;

table_name: Name of the table to be deleted.

Examples 2:

To Drop a database

DROP DATABASE database_name;

database_name: Name of the database to be deleted.

TRUNCATE

The major difference between TRUNCATE and DROP is that truncate is used to delete the data inside the table not the whole table.

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 mytable statement is logically (though not physically) equivalent to the DELETE FROM mytable statement (without a WHERE clause).

Syntax:

TRUNCATE TABLE  table_name;

table_name: Name of the table to be truncated.

DATABASE name – student_data

DROP vs TRUNCATE

  • Truncate is normally ultra-fast and it’s ideal for deleting data from a temporary table.
  • Truncate preserves the structure of the table for future use, unlike drop table where the table is deleted with its full structure.
  • Table or Database deletion using a DROP statement cannot be rolled back, so it must be used wisely.

 Difference between DROP and TRUNCATE

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.

let’s consider the given database: 

 

 To delete the whole database

Query:

DROP DATABASE student_data; 

After running the above query whole database will be deleted.

To 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.

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Last Updated : 12 Jun, 2023
Like Article
Save Article
Similar Reads