This is a discussion on What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server within the Database Support forums, part of the Web Development category; What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server ?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Following are difference between them:- DELETE TABLE syntax logs the deletes thus make the delete operation slow. TRUNCATE table does not log any information but it logs information about deallocation of data page of the table so TRUNCATE table is faster as compared to delete table. DELETE table can have criteria while TRUNCATE can not. TRUNCATE table can not trigger. |
| |||
| Hi, The Difference between TRUNCATE and DELETE commands. TRUNCATE 1. Once u use TRUNCATE command then u cannot retrieve the data again from the table. 2. TRUNCATE removes all the rows from the Table. DELETE 1. If u use DELETE Command then the data deleted can be retrieved when you ROLLBACK . 2. U can delete selected no of records from table using DELETE command. For Ex: DELETE FROM [TABLE_NAME];, deletes all the records from the table. DELETE FROM [TABLE_NAME] WHERE [FieldName] > 10;, will delete records which have the higher values than 10 ..i.e works on the specified condition in the WHERE CLAUSE. Hope got answer !
__________________ -Murali.. |
| |||
| DELETE logs the data for each row affected by the statement in the transaction log and physically removes the row from the file, one row at a time. The recording of each affected row can cause your transaction log grow massively if you are deleting huge numbers of rows. However, when you run your databases in full recovery mode, detailed logging is necessary for SQL Server to be able to recover the database to the most recent state, should a problem arise. The fact that each row is logged explains why DELETE statements can be slow. TRUNCATE is faster than DELETE due to the way TRUNCATE "removes" rows. Actually, TRUNCATE does not remove data, but rather deallocates whole data pages and removes pointers to indexes. The data still exists until it is overwritten or the database is shrunk. This action does not require a lot of resources and is therefore very fast. It is a common mistake to think that TRUNCATE is not logged. This is wrong. The deallocation of the data pages is recorded in the log file. Therefore, BOL refers to TRUNCATE operations as "minimally logged" operations. You can use TRUNCATE within a transaction, and when this transaction is rolled-back, the data pages are reallocated again and the database is again in its original, consistent state. Some limitations do exist for using TRUNCATE. · You need to be db_owner, ddl_admin, or owner of the table to be able to fire a TRUNCATE statement. · TRUNCATE will not work on tables, which are referenced by one or more FOREIGN KEY constraints. |
| |||
| The Differences between Truncate and Delete TRUNCATE and DELETE remove the data not the structure Both commands remove rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement. Conditional based deletion of data Conditional based deletion of data means that not all rows are deleted. Let's suppose I have a table authors and from this table I want to delete the authors that are living in Australia. Let's examine what our options for doing this with each command. TRUNCATE - In case of the TRUNCATE command we can't perform the conditional based deletion because there is no WHERE clause allowed with this command. DELETE - The DELETE command provides the functionality of conditional based deletion of data from the table using the WHERE clause. TRUNCATE is a DDL command whereas DELETE is a DML command That is TRUNCATE is a DDL (data definition language) operation and DELETE is a DML (data manipulation language) operation. Yes according to SQL server it's true. But why it is so, why is TRUNCATE DDL and DELETE DML? When we run the TRUNCATE command it puts a "Schema modification (Sch-M)" lock on the table. What is "schema modification (Sch-M)"? The Database Engine uses schema modification (Sch-M) locks during a table data definition language (DDL) operation, such as adding a column or dropping a table. During the time that it is held, the Sch-M lock prevents concurrent access to the table. This means the Sch-M lock blocks all outside operations until the lock is released. Now you'll ask how it blocks any modification to the table when in the case of TRUNCATE we are performing modifications because we are deleting data? But deleting the data is the one side of coin only. Because TRUNCATE doesn't perform any data modification in the table that is why the DELETE TRIGGER is not called. I think we are not modifying the data of the table, BUT as you know TRUNCATE resets the Identity counter of the column in the table, which means the TRUNCATE is modifying the table definition or structure, which comes under the DDL operations. Also when you are truncating a table, you can't modify or add any data to the table. So, to become a DDL operation you have to fulfill some of the conditions written below: Modifying a table structure or definition comes under DDL operations, and When you are modifying the table structure, you can't access the table to do any data modification. Since TRUNCATE is doing all the activities above, that proves that TRUNCATE is a DDL operation. In case of the DELETE command I am not sure which lock is implemented, but as we know and you can read above that DELETE command deletes the rows one by one. It is modifying the data by deleting it from the table, and because DELETE performs data modifications that is why the DELETE TRIGGER is called. The DELETE command does not modify the table structure in any manner, such as like how TRUNCATE modifies the identity column by resetting its value. To become a DML operation you have to fulfill some of the conditions written below: Modifying the table data. When you are modifying the table data in the mean time you can't perform any table structure modification on the table. Here the DELETE command is modifying the data of the table and also when delete statement is running you can't modify the table structure. So we can say that DELETE is a DML operation. Behavior of Truncate and Delete for Triggers Triggers are important topic in SQL Server, and here I am talking about how both TRUNCATE and DELETE behave differently for Triggers. As you all know triggers fire whenever any data modification happens in the table. In case of TRUNCATE and DELETE, because they are deleting data from the table, the DELETE TRIGGER will fire if present and also the INSTEAD OF and AFTER triggers can be triggered if present. TRUNCATE - When we run the TRUNCATE command to delete all the rows of a table it actually doesn't remove any row rather it deallocates the data pages. So in the case of the TRUNCATE command, triggers will not be fired because here no modification takes place. As we know that TRUNCATE is a DDL command, and DDL commands doesn't modify your data; instead they modify your table structure and definition. DELETE - In case of DELETE the DELETE trigger will be fired if present and also if the INSTEAD OF and AFTER triggers for DELETE TRIGGER is present then they also will be fired. As we know that delete command is a DML command and it deletes the data on row-by-row basis. So that means delete is modifying the data by deleting it from the table. As we know delete is a DML command and trigger will be fired whenever any DML operation on the table takes place. Where we can use these statements? There are some restrictions on the use of both of these statements as follows: For Delete The DELETE statement may fail if it violates a trigger or tries to remove a row referenced by data in another table with a FOREIGN KEY constraint. If the DELETE removes multiple rows, and any one of the removed rows violates a trigger or constraint, the statement is canceled, an error is returned, and no rows are removed. For Truncate You cannot use TRUNCATE TABLE on tables that: Are referenced by a FOREIGN KEY constraint. Participate in an indexed view. Are published using transactional replication or merge replication. Permissions of performing TRUNCATE or DELETE operation For using both the statements you need some permissions on the server, which decides whether you can perform the Delete or Truncate action on the table or not. To truncate a table, you need at least ALTER permissions on the table (which is granted by default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles). To delete rows using the delete statement, you only need the DELETE permission. ThankQ KiruthikaSambandam |
| |||
| DELETE is a logged operation on a per row basis. This means that the deletion of each row gets logged and physically deleted. You can DELETE any row that will not violate a constraint, while leaving the foreign key or any other contraint in place. TRUNCATE is also a logged operation, but in a different way. TRUNCATE logs the deallocation of the data pages in which the data exists. The deallocation of data pages means that your data rows still actually exist in the data pages, but the extents have been marked as empty for reuse. This is what makes TRUNCATE a faster operation to perform over DELETE. You cannot TRUNCATE a table that has any foreign key constraints. You will have to remove the contraints, TRUNCATE the table, and reapply the contraints. TRUNCATE will reset any identity columns to the default seed value. This means if you have a table with an identity column and you have 264 rows with a seed value of 1, your last record will have the value 264 (assuming you started with value 1) in its identity columns. After TRUNCATEing your table, when you insert a new record into the empty table, the identity column will have a value of 1. DELETE will not do this. In the same scenario, if you DELETEd your rows, when inserting a new row into the empty table, the identity column will have a value of 265.
__________________ Shaalini.S ![]() Be the Best of Whatever you are... |
| |||
| Hi Techies, Oracle provides FLASHBACK technology that enable the user to retrieve the data ever after TRUNCATE. So we should stop thinking that truncate will permanently remove that data from table. Oracle9i introduced Flashback Query to provide a simple, powerful and completely non-disruptive mechanism for recovering from human errors. It allows users to view the state of data at a point in time in the past without requiring any structural changes to the database. This FLASHBACK technology has been structured well in the latest versions.
__________________ Keep smiling... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Delete records from multiple table at a time | Falcon | Database Support | 7 | 04-07-2008 01:12 AM |
| truncate table | itbarota | Database Support | 3 | 03-03-2008 01:31 AM |
| How to delete a row from parent table that has relationship with the child tables? | $enthil | Database Support | 5 | 08-14-2007 09:04 AM |
| How can we delete master table record | vadivelanvaidyanathan | Database Support | 2 | 08-08-2007 03:29 AM |
| What are the different index configurations a table can have in sql Server 2005? | Archer | Database Support | 1 | 07-25-2007 03:33 AM |