Showing posts with label Delete Statement. Show all posts
Showing posts with label Delete Statement. Show all posts

Sunday, 7 May 2017

Difference between SQL DELETE and TRUNCATE Statement

In this post, I will explain the Difference between SQL Delete and Truncate Statement.

In SQL, there are different ways to delete rows from a table. We can use Delete Statement or Truncate Statement to delete rows.

Here are some important differences between delete and truncate statement:
1.  Delete statement is used to delete a single or multiple rows from a table, we can use where clause with delete statement to delete selected row(s). Truncate statement is used to remove all rows from a table, it performs the same function as a Delete statement without a Where clause.
2. Where clause with delete statement is optional. Truncate statement is without where clause.
3. Delete operation on a table will not reset the identity column counter. Truncate operation on a table resets identity column counter.

Example -
Delete Statement
DELETE FROM tbl_Employee WHERE Id = 10
Truncate Statement
TRUNCATE TABLE tbl_Employee

SQL Delete Statement

In this post, I will explain the SQL Delete Statement.

DELETE statement is used to delete a single or multiple rows from a table. We can use where clause with delete statement to delete selected row(s).

Few things to Note about Delete Statement -
1. Where clause with delete statement is optional
2. If where clause in a delete statement is not included, then all row(s) from table is deleted
3. If table row is referenced by foreign key, then row cannot be deleted
4. Delete operation on a table will not reset identity column counter
5. Delete operation cannot be rolled back if it is committed

Example -
DELETE FROM tbl_Employee
WHERE Id = 10