Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between drop table and truncate table?

I have some tables that I build as a part of my report rollup. I don't need them afterwards at all. Someone mentioned to truncate them as it would be faster.

like image 811
Brian G Avatar asked Sep 25 '08 20:09

Brian G


People also ask

What is difference between truncate and DROP table?

In SQL, the DROP command is used to remove the whole database or table indexes, data, and more. Whereas the TRUNCATE command is used to remove all the rows from the table.

What is the difference between dropping a table and deleting a table?

Delete statement removes only the rows in the table and it preserves the table structure as same, and Drop command removes all the data in the table and the table structure.

What is the difference between DROP truncate and delete command?

Truncate is a DDL command, it removes all the rows from the table and also frees the space held. It takes a lock on the table while delete command takes a lock on rows of the table. Drop is a DDL command, it removes the complete data along with the table structure(unlike truncate command that removes only the rows).

What is the difference between truncate table and delete table in SQL?

Delete and truncate both commands can be used to delete data of the table. Delete is a DML command whereas truncate is DDL command. Truncate can be used to delete the entire data of the table without maintaining the integrity of the table. On the other hand , delete statement can be used for deleting the specific data.


4 Answers

Deleting records from a table logs every deletion and executes delete triggers for the records deleted. Truncate is a more powerful command that empties a table without logging each row. SQL Server prevents you from truncating a table with foreign keys referencing it, because of the need to check the foreign keys on each row.

Truncate is normally ultra-fast, ideal for cleaning out data from a temporary table. It does preserve the structure of the table for future use.

If you actually want to remove the table definitions as well as the data, simply drop the tables.

See this MSDN article for more info

like image 182
JoshL Avatar answered Oct 22 '22 16:10

JoshL


DROP TABLE deletes the table.

TRUNCATE TABLE empties it, but leaves its structure for future data.

like image 30
ceejayoz Avatar answered Oct 22 '22 18:10

ceejayoz


DROP and TRUNC do different things:

TRUNCATE TABLE

Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.

DROP TABLE

Removes one or more table definitions and all data, indexes, triggers, constraints, and permission specifications for those tables.

As far as speed is concerned the difference should be small. And anyway if you don't need the table structure at all, certainly use DROP.

like image 26
daremon Avatar answered Oct 22 '22 18:10

daremon


I think you means the difference between DELETE TABLE and TRUNCATE TABLE.

DROP TABLE

remove the table from the database.

DELETE TABLE

without a condition delete all rows. If there are trigger and references then this will process for every row. Also a index will be modify if there one.

TRUNCATE TABLE

set the row count zero and without logging each row. That it is many faster as the other both.

like image 20
Horcrux7 Avatar answered Oct 22 '22 17:10

Horcrux7