Thursday, December 19, 2019

Difference between Delete, Drop and Truncate in SQL Server


Delete is used to delete the one row or more than one rows or all rows from data table in database.

Syntax:
DELETE FROM TABLE table_Name;

After execute this query all rows are removed from table.

DELETE FROM TABLE table_Name WHERE column_Name=?

After execute this query particular row removed from table.

Drop is used to delete whole database or just a table.
The Drop statement destroys the objects like an existing database,table,index,or view.

Syntax:
DROP object object_Name

Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.

DROP DATABASE database_name;
database_name: Name of the database to be deleted.

Truncate statement is a Data Definition Language(DDL) operation that is used to mark the extents of a table for deallocation (empty for reuse).

Truncate basically used to remove the whole data from table but remain the structure of table for future use.

Syntax:

TRUNCATE TABLE table_Name;

TRUNCATE Vs DROP
Truncate is normally ultra-fast and its 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 DROP statement cannot be rolled back, so it must be used wisely.

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...