Middle
What is the difference between DROP and TRUNCATE?
sobes.tech AI
Answer from AI
DROP and TRUNCATE are SQL commands for deleting data, but they differ in behavior and application:
- DROP removes the entire table from the database along with its structure and data. After DROP, the table no longer exists.
- TRUNCATE deletes all data from the table, but the table's structure and definitions remain, making the table available for further use.
Differences:
- DROP is more radical and is used if the table is no longer needed.
- TRUNCATE deletes all rows faster because it does not log the deletion of each row separately.
Example:
-- Completely delete the table
DROP TABLE users;
-- Clear the table, leaving the structure
TRUNCATE TABLE users;