List the differences between the following MySql commands.
- drop table tablename;
- truncate table tablename;
- delete from tablename where 1;
Also from your experiences please tell me typical usage scenario for each.
List the differences between the following MySql commands.
Also from your experiences please tell me typical usage scenario for each.
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'
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.
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.
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.
Delete
Truncate
Drop