SQL
The DELETE statement is used to remove specific records from a table based on a given condition. The DROP TABLE statement is used to delete an entire table, including its structure and data. Where as the TRUNCATE TABLE statement removes all records from a table but retains the table structure.
Please read detailed articles, DELETE table in SQL
Database Structure:
StudentID | FirstName | LastName | Age | Course |
---|---|---|---|---|
1 | John | Doe | 21 | Computer Science |
2 | Jane | Smith | 20 | Data Science |
3 | Bob | Johnson | 22 | Web Development |
4 | Alice | White | 23 | Cybersecurity |
DROP TABLE Statement:
-- Example: Dropping the Students table
DROP TABLE Students;
Output: The Students table is completely removed.
TRUNCATE TABLE Statement:
-- Example: Truncating the Students table
TRUNCATE TABLE Students;
Output:
StudentID | FirstName | LastName | Age | Course |
---|---|---|---|---|
0 | 0 |
DELETE Statement:
-- Example: Deleting a student record
DELETE FROM Students WHERE StudentID = 2;
Output:
StudentID | FirstName | LastName | Age | Course |
---|---|---|---|---|
1 | John | Doe | 21 | Computer Science |
3 | Bob | Johnson | 22 | Web Development |
4 | Alice | White | 23 | Cybersecurity |
Aspect | DROP TABLE | DELETE |
---|---|---|
Purpose | Remove an entire table, including structure and data. | Remove specific records from a table. |
Granularity | Operates at the table level. | Operates at the row level. |
Irreversibility | Irreversible. Once executed, the table is gone. | Reversible. Removes specific rows but retains the table structure. |
Performance | May take longer, especially for large tables. | Can be slower for large datasets, especially when removing a significant portion of records. |
Syntax | DROP TABLE table_name; | DELETE FROM table_name WHERE condition; |
Aspect | DELETE | TRUNCATE TABLE |
---|---|---|
Purpose | Remove specific records from a table based on a condition. | Remove all records from a table, but retain the table structure. |
Granularity | Operates at the row level, allowing targeted removal. | Operates at the table level, removing all records. |
Transaction Log | Generates individual row deletion entries in the transaction log. | Doesn’t log individual row deletions, resulting in less transaction log activity. |
Reset Identity Columns | Doesn’t automatically reset identity columns to their seed value. | Automatically resets identity columns to their seed value, effectively resetting auto-incrementing primary key values. |
Performance | Can be slower, especially for large datasets, due to logging. | Generally faster, especially for bulk data removal, as it doesn’t log individual row deletions. |
Syntax | DELETE FROM table_name WHERE condition; | TRUNCATE TABLE table_name; |
Aspect | DROP TABLE | TRUNCATE TABLE |
---|---|---|
Purpose | Completely removes an entire table and its structure. | Removes all records from a table but retains the table structure for future use. |
Granularity | Operates at the table level, deleting the entire entity. | Operates at the table level, removing all records. |
Irreversibility | Irreversible action. Once executed, the table is permanently deleted. | Reversible. The table structure remains, and the operation can be rolled back. |
Transaction Log | No impact on the transaction log since the entire table is deleted at once. | Less transaction log activity compared to DELETE. |
Reset Identity Columns | Doesn’t apply, as the entire table, including identity columns, is removed. | Automatically resets identity columns to their seed value. |
Performance | May take longer, especially for large tables. | Generally faster, especially for bulk data removal, as it doesn’t log individual row deletions. |
Syntax | DROP TABLE table_name; | TRUNCATE TABLE table_name; |