0% found this document useful (0 votes)
7 views

Assignment#3

Uploaded by

53180
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment#3

Uploaded by

53180
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Difference Between TRUNCATE, DROP, and DELETE

Assignment #3

Database Systems

SUBMITTED BY:
HAFIZ MUHAMMAD SAAD

SAP ID:
53180

SUBMITTED TO:
MS. TUBA MANSOOR

DEPARTMENT OF COMPUTER SCIENCE

RIPHAH INTERNATIONAL UNIVERSITY


Hafiz Muhammad Saad BSSE-3B Sap ID: 53180

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.

3) Impact on Data and Table Structure


TRUNCATE:
• Data: All data is deleted.
• Structure: The table structure remains intact, and the identity column is reset to its initial
value (if any).
DROP:
• Data and Structure: Both the data and the table structure are completely removed. The
table can no longer be accessed unless recreated.
DELETE:
• Data: Deletes specific rows, and the rest of the data remains.
• Structure: The table structure remains unchanged.
4) Transaction Control
TRUNCATE:
• Rollback: Cannot be rolled back in most databases once executed, as it is a DDL
(Data Definition Language) operation.
DROP:
• Rollback: Cannot be rolled back since it is also a DDL operation.

Riphah International University


Hafiz Muhammad Saad BSSE-3B Sap ID: 53180

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

Removes all Reset table without


TRUNCATE Yes No Fast
rows deleting structure

Removes Remove obsolete


DROP No No Fast
table tables or databases

Deletes Remove specific data


DELETE Yes Yes Slower
specific rows based on conditions

Riphah International University

You might also like