Results 1 to 5 of 5

Thread: What is the difference between delete table & truncate table?

  1. #1
    Join Date
    Feb 2009
    Posts
    8

    What is the difference between delete table & truncate table?

    Hi,
    What is the difference between delete table & truncate table?

    Please let me know!

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: What is the difference between delete table & truncate table?

    The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.

    Code:
    SQL> SELECT COUNT(*) FROM emp;
    
      COUNT(*)
    ----------
            14
    
    SQL> DELETE FROM emp WHERE job = 'CLERK';
    
    4 rows deleted.
    
    SQL> COMMIT;
    
    Commit complete.
    
    SQL> SELECT COUNT(*) FROM emp;
    
      COUNT(*)
    TRUNCATE

    TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as a DELETE.

    Code:
    SQL> TRUNCATE TABLE emp;
    
    Table truncated.
    
    SQL> SELECT COUNT(*) FROM emp;
    
      COUNT(*)

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is the difference between delete table & truncate table?

    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 !

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is the difference between delete table & truncate table?

    TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server.
    DELETE is a DML command and can be rolled back.

    Both commands accomplish identical tasks (removing all data from a table), but TRUNCATE is much faster.

    TRUNCATE : You can't use WHERE clause
    DELETE : You can use WHERE clause

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is the difference between delete table & truncate table?

    DELETE and TRUNCATE differ in some more important and subtle ways than the auto increment counter.

    The most important difference is DELETE operations are transaction-safe and logged, which means DELETEs can be rolled back. TRUNCATE cannot be done inside a transaction and can’t be rolled back. Because TRUNCATE is not logged recovering a mistakenly TRUNCATEd table is a much bigger problem than recovering from a DELETE.

    DELETE will fail if foreign key constraints are broken; TRUNCATE may not honor foreign key constraints (it does for InnoDB tables). DELETE will fire any ON DELETE triggers; TRUNCATE will not.

    TRUNCATE is probably better thought of as a shortcut for DROP TABLE/CREATE TABLE rather than a quirky kind of DELETE.

    These differences are not specific to mySQL — PostgreSQL, MS SQL Server, and Oracle behave more or less the same way.

Similar Threads

  1. Replies: 5
    Last Post: 27-08-2011, 10:53 AM
  2. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 02:01 PM
  3. Replies: 4
    Last Post: 30-11-2010, 03:01 AM
  4. Difference between Delete and Truncate
    By Aileen_L in forum Software Development
    Replies: 3
    Last Post: 29-01-2010, 09:05 AM
  5. Replies: 2
    Last Post: 24-04-2009, 11:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,452,260.71855 seconds with 16 queries