.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Interview Question >> Sql Server >> Post New Question Subscribe to Interview Questions

Difference between Delete and Truncate command?

Posted By :Narayanan     Posted Date :July 22, 2011    Points :40   Category :Sql Server 
Delete Command:
Delete a row based on where Condition.
Truncate command:
Delete all the Rows from table.



You can also find related Interview Question to Difference between Delete and Truncate command?  below: 

Difference Between Delete and Truncate

  
.Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow.

. Truncate table also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back.

. Truncate table is functionally identical to delete statement with no "where clause" both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete.

. Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc., remains as it is.

. In truncate table the counter used by an identity column for new rows is reset to the seed for the column.

. If you want to retain the identity counter, use delete statement instead.

. If you want to remove table definition and its data, use the drop table statement.

. You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger.

. Truncate table may not be used on tables participating in an indexed view. (More...)

What is the difference between TRUNCATE and DELETE commands?

  
TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE operation can be rolled back, but TRUNCATE operation cannot be rolled back. WHERE clause can be used with DELETE and not with TRUNCATE. (More...)

Difference between Delete and Truncate?

  
Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow.

Truncate table also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back.

Truncate table is functionally identical to delete statement with no where clause both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete.

Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc. remains as it is.

In truncate table the counter used by an identity column for new rows is reset to the seed for the column.

If you want to retain the identity counter, use delete statement instead.

If you want to remove table definition and its data, use the drop table statement.

You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger.

Shashi Ray (More...)

Difference between DELETE and TRUNCATE commands ?

  
TRUNCATE TABLE is faster that DELETE TABLE and uses fewer system and transaction log resources.
Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. (More...)

What is the difference between TRUNCATE and DELETE commands?

  
1.TRUNCATE is a DDL command whereas DELETE is a DML command.
2.DELETE operation can be rolled back, but TRUNCATE operation cannot be rolled back.
3.WHERE can be used with DELETE and not with TRUNCATE. (More...)

Delete and Truncate Command:

  


DELETE is a DML command and can be rolled back
Delete can use where clause

TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server
Truncate cannot use where clause

(More...)

What is difference between DELETE & TRUNCATE commands?

  
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.

TRUNCATE

.TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.
.You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
.TRUNCATE cannot be rolled back.
.TRUNCATE is DDL Command.
.TRUNCATE Resets identity of the table

DELETE
.DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.

.DELETE Can be used with or without a WHERE clause
.DELETE Activates Triggers.
.DELETE can be rolled back.
.DELETE is DML Command.
.DELETE does not reset identity of the table. (More...)

Difference between truncate and delete

  
Truncate
Resets identity of the table
DDL Command
Cannot be rolled back
Faster (Uses Fewer systems)
Where can't be used
removes the data by de-allocating the data pages
TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.
Trigger can't be activated


Delete
does not reset identity of the table
DML Command
can be rolled back
Slower (Removes row one by one)
WHERE Condition can be used
records an entry in the transaction log for each deleted row
Retain Identity counter
Trigger can be activated
(More...)

What is difference between DELETE and TRUNCATE commands?

  
TRUNCATE cannot be rolled back.
TRUNCATE is DDL Command.
TRUNCATE Resets identity of the table
TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.

DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.
DELETE can be rolled back.
DELETE is DML Command.
DELETE does not reset identity of the table. (More...)

What is the difference between truncate and delete in sql server

  
Truncate is a quick way to empty a table. It removes everything without logging each row. Truncate will fail if there are foreign key relationships on the table. Conversely, the delete command removes rows from a table, while logging each deletion and triggering any delete triggers that may be present. (More...)

How To Delete Description Value for a Column in Table using SQL Command?

  
We can Delete Description from Column using sp_dropextendedproperty System Store Procedure.
Sample Command to Delete Description from Column in a Table:

EXEC sys.sp_dropextendedproperty @name=N'MS_Description', @value=N'My Description for Column Here' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'MyTableName', @level2type=N'COLUMN',@level2name=N'ColumnName' (More...)

Can we use Truncate command on a table which is referenced by FOREIGN KEY?

  
No. We cannot use Truncate command on a table with Foreign Key because of referential integrity (More...)

Can we use Truncate command on a table which is referenced by FOREIGN KEY?

  
No. We cannot use Truncate command on a table with Foreign Key because of referential integrity (More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

 
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.


Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend