Question №34
Remaining:
Explain the difference between DELETE, TRUNCATE, and DROP
Sample Answer
Show Answer by Default
DELETE
- Removes selected records from a table.
- A WHERE condition can be used to delete specific records.
- The operation is logged in the transaction log row by row.
- ON DELETE triggers are activated.
Syntax
MySQL 8.1DELETE FROM table_name [WHERE condition];
TRUNCATE
- Removes all data from the table without the possibility of recovery.
- WHERE cannot be used.
- Faster because it does not log the deletion of each row.
- Resets auto-increment identifiers.
- Triggers are not activated.
Syntax
MySQL 8.1TRUNCATE TABLE table_name;
DROP
- Deletes the entire table along with its data, structure, indexes, and constraints.
- The action is irreversible.
Syntax
MySQL 8.1DROP TABLE table_name;
Choosing between commands
- Use DELETE when you need to remove specific records.
- Use TRUNCATE to quickly remove all data from the table while keeping its structure.
- Use DROP to completely remove the table from the database.