CodeBlanks Logo Icon CodeBlanks

Safe deletion

Learn ways to avoid accidental deletion.

DELETE vs TRUNCATE

TRUNCATE empties a table quickly. Unlike DELETE it does not go row by row; it drops whole pages at once.

TRUNCATE TABLE products;

This query empties the products table instantly.

DELETE vs TRUNCATE — details

TRUNCATE usually cannot be rolled back and resets auto-increment ids. DELETE is slower but can be rolled back inside a transaction.

Practice DELETE vs TRUNCATE

Delete the row with id=4 from products.

Starter code

query.sql
-- delete row
DELETE FROM products
WHERE

Sample solution

query.sql
DELETE FROM products
WHERE id = 4

Expected output

1 satır silindi

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Fast empty

Question
TRUNCATE ____ products;
Answer
TRUNCATE TABLE products;

Explanation: TRUNCATE TABLE deletes every row at once, faster than DELETE.

2 Safe deletion: match the terms

Term Meaning
TRUNCATE fast delete
DELETE row delete
DROP drop table
WHERE condition

3 Which empties a table faster?

  • DROP
  • TRUNCATE Correct answer
  • DELETE
  • ALTER

Explanation: TRUNCATE is faster than DELETE.

All lessons in Beginner