Reputation: 12013
List the differences between the following MySql commands.
Also from your experiences please tell me typical usage scenario for each.
Upvotes: 17
Views: 16256
Reputation: 431
Delete
Truncate
Drop
Upvotes: 2
Reputation: 4425
If you want to delete only rows, then you can use DELETE
If you want to delete table from database, then you can use DROP
Before delete data, think well whether data is useful or not. Because you can't get again that data.
Upvotes: 5
Reputation: 5761
In addition to the answers above, on Oracle RDBMS, "delete" is a transaction that can be rolled back if you didn't commit. "Truncate" cannot.
Upvotes: 2
Reputation: 546035
drop table tablename;
truncate table tablename;
DELETE
because it simply deletes all data. DELETE
will scan the table to generate a count of rows that were affected.delete from tablename;
WHERE
clause.DELETE FROM tablename WHERE username = 'joe'
Upvotes: 28
Reputation: 190945
Drop
is deleting the table. I would drop the table if I didn't need it anymoredelete
to do a delete (of specific rows) with a query.I rarely "delete" anything from my databases. I generally put a flag column such as deleted
as a boolean value. I check for that. If its true
, I don't present that data.
Upvotes: 5