Here’s a comparison of DROP, DELETE, and TRUNCATE in SQL:
| Aspect | DROP | DELETE | TRUNCATE |
|---|---|---|---|
| Purpose | Removes an entire table or database. | Removes specific rows from a table. | Removes all rows from a table. |
| DML/DDL | DDL (Data Definition Language). | DML (Data Manipulation Language). | DDL (Data Definition Language). |
| Deletes Data Only? | Deletes both structure and data. | Deletes data, keeps table structure. | Deletes data, keeps table structure. |
| WHERE Clause | Not applicable. | Can use WHERE to filter rows. |
Cannot use WHERE. |
| Performance | Faster for dropping structure. | Slower for large data (logged). | Faster (minimal logging). |
| Rollback Support | Cannot be rolled back. | Can be rolled back if in a transaction. | Cannot be rolled back. |
| Resets Identity? | Yes, as table is removed. | No, retains identity sequence. | Yes, resets identity sequence. |
Examples:
-
DROP:
DROP TABLE employees; -
DELETE:
DELETE FROM employees WHERE department_id = 101; -
TRUNCATE:
TRUNCATE TABLE employees;
No comments:
Post a Comment