DELETE, TRUNCATE & DROP
Three ways to remove data — each with very different consequences. Know which one to use and when.
DELETE removes specific rows. TRUNCATE clears the whole table fast. DROP deletes the table itself. Choose carefully.
EXERCISE
1DELETE — Removing Specific Rows
DELETE removes rows that match a condition. Without WHERE it removes everything. Always verify what you are deleting before you run it.
Basic DELETE:
DELETE FROM employees WHERE id = 42;
-- Removes exactly one row (assuming id is the primary key)
DELETE with conditions:
-- Remove all cancelled orders older than 1 year:
DELETE FROM orders
WHERE status = 'cancelled'
AND created_at < DATE_SUB(CURDATE(), INTERVAL 1 YEAR);
-- Remove duplicate emails, keeping the lowest id:
DELETE FROM customers
WHERE id NOT IN (
SELECT MIN(id)
FROM customers
GROUP BY email
);
The catastrophic version — no WHERE:
-- ❌ Deletes every single row in the table:
DELETE FROM employees;
-- The table structure remains, but all data is gone
-- This CAN be rolled back if inside a transaction
Safe DELETE workflow:
-- Step 1: SELECT with the same WHERE to preview:
SELECT id, name FROM employees WHERE department_id = 99;
-- Verify these are the rows you intend to delete
-- Step 2: DELETE with the same WHERE:
DELETE FROM employees WHERE department_id = 99;
DELETE with LIMIT — safety cap:
-- Never delete more than 500 rows in one operation:
DELETE FROM logs
WHERE created_at < '2023-01-01'
LIMIT 500;
-- Run repeatedly until all old logs are gone
DELETE with JOIN (MySQL-specific syntax):
-- Delete orders from inactive customers:
DELETE o
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE c.is_active = FALSE;
Pro Tip: The golden rule: run
SELECT COUNT(*)with yourWHEREclause beforeDELETE. If the count is what you expect — proceed. If it is 50,000 when you expected 5 — stop and investigate.
EXERCISE
2TRUNCATE vs DELETE vs DROP — Three Ways to Remove Data
These three commands remove data in very different ways with very different consequences — choosing the wrong one is a career-defining mistake.
Side-by-side comparison:
-- DELETE — removes specific rows, can be rolled back:
DELETE FROM logs WHERE created_at < '2023-01-01';
-- Removes matching rows. Table structure intact. Slow on large tables.
-- CAN be rolled back inside a transaction.
-- TRUNCATE — removes ALL rows, faster, cannot be rolled back:
TRUNCATE TABLE logs;
-- Empties the table completely. Structure stays. AUTO_INCREMENT resets to 1.
-- CANNOT be rolled back in MySQL.
-- DROP — removes the entire table (data + structure):
DROP TABLE logs;
-- Table is completely gone. Cannot SELECT from it. Cannot be rolled back.
Speed comparison:
-- Table with 10 million rows:
-- DELETE (no WHERE) — very slow:
DELETE FROM big_table;
-- Logs every row deletion individually. Takes minutes.
-- TRUNCATE — very fast:
TRUNCATE TABLE big_table;
-- Deallocates pages in bulk. Takes seconds.
When to use each:
| Command | Use when... |
|---|---|
DELETE WHERE ... | Removing specific rows with a condition |
DELETE (no WHERE) | Rare — usually use TRUNCATE instead |
TRUNCATE | Clearing a table completely for a fresh start |
DROP | Permanently removing a table you no longer need |
The AUTO_INCREMENT difference:
-- DELETE leaves AUTO_INCREMENT where it is:
DELETE FROM customers;
INSERT INTO customers (name) VALUES ('Ada');
-- New id continues from where it left off (e.g., 47)
-- TRUNCATE resets AUTO_INCREMENT to 1:
TRUNCATE TABLE customers;
INSERT INTO customers (name) VALUES ('Ada');
-- New id = 1
Pro Tip: In production — think three times before TRUNCATE, four times before DROP. Both are irreversible (without a backup). DELETE inside a transaction is the safest option when you need to undo.
EXERCISE
3DDL Commands — ALTER and Managing Table Structure
After a table is created, requirements change. ALTER TABLE lets you add, remove, and modify columns without recreating the table from scratch.
Adding a column:
-- Add a phone column to existing employees table:
ALTER TABLE employees
ADD COLUMN phone VARCHAR(15);
-- New column added, existing rows get NULL for phone
-- Add with a default value:
ALTER TABLE employees
ADD COLUMN is_active BOOLEAN DEFAULT TRUE;
-- Existing rows immediately get is_active = TRUE
Modifying a column:
-- Change VARCHAR(50) to VARCHAR(100):
ALTER TABLE employees
MODIFY COLUMN name VARCHAR(100) NOT NULL;
-- Rename a column (MySQL 8.0+):
ALTER TABLE employees
RENAME COLUMN dept TO department;
Dropping a column:
-- Remove a column permanently:
ALTER TABLE employees
DROP COLUMN old_column;
-- ❌ Cannot be undone easily — back up first
Adding constraints after creation:
-- Add a UNIQUE constraint to an existing column:
ALTER TABLE employees
ADD CONSTRAINT unique_email UNIQUE (email);
-- Add a foreign key to an existing column:
ALTER TABLE employees
ADD CONSTRAINT fk_dept
FOREIGN KEY (department_id) REFERENCES departments(id);
-- Drop a constraint:
ALTER TABLE employees
DROP FOREIGN KEY fk_dept;
Renaming a table:
ALTER TABLE old_table_name RENAME TO new_table_name;
-- Or:
RENAME TABLE old_name TO new_name;
Pro Tip: On large production tables,
ALTER TABLEcan lock the table for minutes or hours while it rebuilds. This is why schema changes require careful planning and off-peak execution in real systems.
