Skynet Posted December 4, 2006 Share Posted December 4, 2006 I am learning SQL and am trying to figure out the following:What is the difference(s) or advantages/disadvantages between either of these following two statments? TRUNCATE TABLE table_name or DELETE FROM table_name Link to comment Share on other sites More sharing options...
murfitUK Posted December 4, 2006 Share Posted December 4, 2006 DELETE FROM tablename WHERE ...... deletes individual rowsTRUNCATE tablename deletes all rows in the table. In some version of sql it also resets any auto increment back to zero. Its equivalent of DROPping the table (ie deleting it completely) and then recreating it. There are difference depending on version of sql/mysql and also some differences depending on the type of database (innodb, myisam). Link to comment Share on other sites More sharing options...
justsomeguy Posted December 4, 2006 Share Posted December 4, 2006 To add to that, as you add and remove rows to a table, you can imagine that the table has a "high-water mark", which would be the maximum size that the table has reached. Even as you delete rows, the high-water mark stays where it is, and that much space is still being used by the table. A delete will just delete the rows, and leave the high-water mark (and table size) where it is. A truncate will delete the row and then rebuild the table to set the high-water mark lower to the current size of the table. I believe only Oracle supports truncate, but I may be wrong. And like murfitUK said, it is equivalent to dropping the table and then adding all of the rows back to it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now