The following example deletes all rows from the "Employee" table in the "VENTERPRISE" database.
- BEGIN TRANSACTION
- SELECT * FROM Employee --to check the records
- DELETE FROM Employee
- WHERE ManagerID = 'M011'
b) Now, let's check our result.
c) Let's try to roll back it by executing the following statement(s).
d) Again, let's check our result. And yes, we have rolled back out delete transactions.
TRUNCATE Statement
TRUNCATE is a DDL (Data Definition Language) command that is used to delete all data from a table (relation) without removing the table structure. TRUNCATE is used only to remove data from the table, not to remove a table from the database. This is almost similar to the DELETE statement because it does not have a WHERE clause.
Key Points
- TRUNCATE is a DDL Command.
- WHERE Clause cannot be used with TRUNCATE.
- TRUNCATE resets auto-increment.
- To use the TRUNCATE, you need permission to use the TRUNCATE TABLE.
- TRUNCATE removes all records from a table by using a table lock.
- TRUNCATE keeps the minimum log space in the transaction log, so it can also be rolled back in SQL Server.
- In SQL Server, TRUNCATE operation can be rolled back.
- TRUNCATE does not return the number of deleted rows (records) from the table as a result.
- TRUNCATE is faster than DELETE.
Note
Users cannot use the TRUNCATE TABLE command on tables that are referenced by the FOREIGN KEY constraint. But, the user can truncate a table that has a foreign key that references itself.
Syntax
TRUNCATE TABLE <table_name>;
Examples
The examples in this section demonstrate the functionality of the TRUNCATE statement.
1) Execute the following TRUNCATE statement to remove all the data (truncate) from the "PRODUCT" table.
2) SQL Server TRUNCATE Transaction Example
a) Execute the following statement(s) to begin the transaction.
- BEGIN TRANSACTION
- SELECT * FROM PRODUCT --to check the records
- TRUNCATE TABLE PRODUCT
b) Now, let's check the result.
c) Let's try to roll back it by executing the following statement(s).
d) Again, let's check our result. And yes, we have rolled back our truncate statements.
DROP Statement
DROP TABLE is also a DDL (Data Definition Language) command. It is used to remove data stored in a table as well as a table structure from a database. The DROP TABLE statement removes the table's structure, data, indexes, constraints, and triggers. When a table is dropped, any constraints or triggers associated with it, are also dropped.
A table that is referenced by a FOREIGN KEY constraint cannot be dropped using the DROP TABLE statement. The referencing FOREIGN KEY constraint or the referencing table must be dropped first. The referencing table must be specified first, if both the referencing table and the primary key holding table are being dropped in the same DROP TABLE statement.
Key Points
- DROP Statement is also a DDL Command.
- In SQL Server, DROP table operation can be rolled back.
- The DROP statement frees up memory space (table space from the memory).
Syntax
DROP TABLE <table_name>;
Note
Multiple tables can be dropped in any database using a comma-separated list.
Syntax
DROP TABLE (table_name1, table_name2, ...);
Examples
The examples in this section demonstrate the functionality of the DROP statement. Let's see.
1) Execute the following DROP statement to drop the table from the database.
- DROP TABLE CLIENT
- Print 'Hey, you have dropped your table...'
2) SQL Server DROP Transaction Example
a) Execute the following statement(s) to begin the transaction.
- BEGIN TRANSACTION
- SELECT * FROM CLIENT --to check the records
- DROP TABLE CLIENT
b) Now, let's check the result.
c) Let's try to roll back it by executing the following statement(s).
d) Again, let's check our result. And yes, we have rolled back our delete statement.
Difference between DELETE and TRUNCATE Statements
Now, let's see the difference between DELETE and TRUNCATE Command.
S.No |
DELETE |
TRUNCATE |
1 |
DELETE is a DML Command. |
TRUNCATE is a DDL Command. |
2 |
DELETE is used to delete the records based on a condition. |
TRUNCATE is used to remove all the records from a table. |
3 |
WHERE Clause can be used with Delete to filter the records. |
WHERE Clause can not be used with Truncate. |
4 |
The DELETE statement deletes rows one by one and records an entry for each deleted row in the transaction log. |
TRUNCATE keeps the minimum log space in the transaction log. |
5 |
DELETE uses a row lock. |
TRUNCATE uses a table lock. |
6 |
DELETE does not reset auto-increment. |
TRUNCATE resets auto increment. |
7 |
In SQL Server, DELETE can be rolled back. |
In SQL Server, TRUNCATE can also be rolled back. |
8 |
DELETE is slower than TRUNCATE. |
TRUNCATE is faster than DELETE. |
Difference between DELETE, TRUNCATE, and DROP Statements
Let's look at the quick difference between DELETE TRUNCATE, and DROP Statements.
S.No. |
Key Points |
DELETE |
TRUNCATE |
DROP |
1 |
Classification |
DML (Data Manipulation Language) |
DDL (Data Definition Language) |
DDL (Data Definition Language) |
2 |
Use to delete |
one or more rows |
all the rows |
all the data stored in a table along with its structure |
3 |
WHERE Condition? |
Yes |
No |
No |
4 |
Uses a lock |
Row Lock |
Table Lock |
Table Lock |
5 |
Write Transaction log |
for each row |
for the whole table |
for the whole table |
6 |
Should be Rollback? |
Yes |
Yes |
Yes |
7 |
Does it reset the auto-increment? |
No |
Yes |
--- |
8 |
Speed |
Slower than TRUNCATE |
Faster than DROP |
Quick to perform (faster) |
In this article, we have discussed the concept of delete, truncate, and drop statements of SQL Server with various examples.
I hope you enjoyed this article. Follow C# Corner to learn more new and amazing things about SQL Server.
Thanks for reading.