Assignment#3
Assignment#3
Assignment #3
Database Systems
SUBMITTED BY:
HAFIZ MUHAMMAD SAAD
SAP ID:
53180
SUBMITTED TO:
MS. TUBA MANSOOR
What is the difference between the TRUNCATE, DROP, and DELETE commands in SQL?
1) Definition
TRUNCATE DROP DELETE
Removes all rows from a Completely removes a table Deletes specific rows from a
table quickly without logging or database from the table based on a condition
individual row deletions. It schema, along with all its (or all rows if no condition is
resets the table to its initial data and structure. specified). This command
empty state but retains the logs each deletion
table structure. individually.
2) Functionality
TRUNCATE DELETE
Cannot delete specific rows; it removes all Can delete selected rows using a WHERE
rows from the table. clause (e.g., DELETE FROM employees
WHERE department = 'Sales';).
TRUNCATE DROP
Only removes the data but retains the table Removes the entire table, including its data,
structure and constraints (e.g., indexes). structure, constraints, and indexes.
DELETE:
• Rollback: Can be rolled back if executed within a transaction (BEGIN TRANSACTION
... ROLLBACK).
Example:
BEGIN TRANSACTION;
DELETE FROM employees WHERE department = 'Sales';
ROLLBACK; -- This restores the deleted rows.
5) Performance
• TRUNCATE is faster than DELETE because it does not log individual row deletions or
trigger associated constraints and triggers.
• DROP is also fast, as it just removes the object from the schema without scanning
individual rows.
6) Usage Scenarios and Examples
TRUNCATE Use Case:
When you need to empty a table quickly without removing its structure.
Example: Resetting a log table after a reporting cycle.
TRUNCATE TABLE logs;
DROP Use Case:
When you want to permanently remove a table or database.
Example: Deleting an obsolete table.
DROP TABLE old_customers;
Delete Use Case:
When you need to remove specific rows based on a condition.
Example: Deleting employees from the "Sales" department.
DELETE FROM employees WHERE department = 'Sales';
Summary Table
Structure Transaction
Command Action Performance Usage
Retained? Rollback