.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

What is the difference between truncate and delete in sql server

Posted By :Ashutosh Jha     Posted Date :December 27, 2013    Points :40   Category :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.

You can also find related Interview Question to What is the difference between truncate and delete in sql server  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...)

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

Difference between Delete and Truncate command?

  
Delete Command:
Delete a row based on where Condition.
Truncate command:
Delete all the Rows from table.

(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 Server. Transfer and Response. Redirect?

  
The Transfer method allows you to transfer from inside one ASP page to another ASP page. All of the state information that has been created for the first (calling) ASP page will be transferred to the second (called) ASP page. This transferred information includes all objects and variables that have been given a value in an Application or Session scope, and all items in the Request collections. For example, the second ASP page will have the same SessionID as the first ASP page.

When the second (called) ASP page completes its tasks, you do not return to the first (calling) ASP page. All these happen on the server side browser is not aware of this.
The redirect message issue HTTP 304 to the browser and causes browser to got the specified page. Hence there is round trip between client and server. Unlike transfer, redirect doesn't pass context information to the called page.


Shashi Ray (More...)

Difference between Server.Transfer & Response.Redierect ?

  
Server.Transfer-

1)Server.Transfer transfers page processing from one page directly to the next page without making a round-trip back to the client's browser. This provides a faster response with a little less overhead on the server.

2) Server.Transfer does not update the clients url history list or current url.

Response.Redirect-

1)Response.Redirect is used to redirect the user's browser to another page or site. This performas a trip back to the client where the client's browser is redirected to the new page.

2) The user's browser history list is updated to reflect the new address. (More...)

What is the difference between SharePoint Portal Server and Windows SharePoint Services?

  
SharePoint Portal Server is the global portal offering features like global navigation and searching.Windows SharePoint Services is more content management based with document libraries and lists. You apply information to certain areas within your portal from windows share point Services or directly to portal areas. (More...)

What is the difference between Microsoft Office SharePoint Server 2007 for Internet sites and Microsoft Office SharePoint Server 2007?

  
Microsoft Office SharePoint Server 2007 for Internet sites and Microsoft Office SharePoint Server2007 have identical feature functionality. While the feature functionality is similar, the usage rights are different.If you are creating an Internet, or Extranet, facing website, it is recommended that you use Microsoft Office SharePoint Server 2007 for Internet sites which does not require the purchase client access licenses. Websites hosted using an "Internet sites" edition can only be used for Internet facing websites and all content, information, and applications must be accessible to non employees.Websites hosted using an "Internet sites" edition cannot be accessed by employees creating, sharing, or collaborating on content which is solely for internal use only, such as an Intranet Portal scenario. (More...)

Difference between Server-Side AJAX framework and Client-side AJAX framework?

  
ASP.NET AJAX contains both a server-side Ajax framework and a client-side Ajax framework. The server-side framework provides developers with an easy way to implement Ajax functionality, without having to possess much knowledge of JavaScript. The framework includes server controls and components and the drag and drop functionality. This framework is usually preferred when you need to quickly ajaxify an asp.net application. The disadvantage is that you still need a round trip to the server to perform a client-side action.
The Client-Side Framework allows you to build web applications with rich user-interactivity as that of a desktop application. It contains a set of JavaScript libraries, which is independent from ASP.NET. The library is getting rich in functionality with every new build released. (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