Data Manipulation: INSERT, UPDATE, DELETE & ON DUPLICATE KEY UPDATE
Overview & Learning Objectives
Data analysts must maintain transactional tables through Data Manipulation Language (DML). Modifying data requires extreme care: executing an `UPDATE` or `DELETE` without a `WHERE` clause mutates or obliterates every row in the table. MySQL also features the powerful `ON DUPLICATE KEY UPDATE` construct (Upsert), which inserts new records or updates existing records when primary key conflicts arise.
- Insert single and multiple rows efficiently using multi-value `INSERT INTO ... VALUES (...)`.
- Perform safe, conditional record updates using `UPDATE ... SET ... WHERE ...`.
- Distinguish between `DELETE FROM` (row-by-row with rollback support) and `TRUNCATE TABLE` (DDL instant reset).
Industry Standard
Standard: Analytics pipelines at Uber and Zomato use MySQL `INSERT ... ON DUPLICATE KEY UPDATE` to achieve idempotent data ingestion that safely handles job restarts.
Real-World Example: ScoopCraft Real-Time Inventory Updates
ScoopCraft updates stock counts when dark-store deliveries arrive or customer sales are finalized.
| Operation | SQL Command | Safety Check | Business Outcome |
|---|---|---|---|
| Batch Ingest | INSERT INTO menu_items (flavor, price) VALUES (...) | Enforces unique constraints | 3 new flavors added to app catalog |
| Price Promotion | UPDATE menu_items SET price = price * 0.9 WHERE ... | Always verify with SELECT first | 10% discount applied to sorbet category |
| Inventory Depletion | DELETE FROM inventory WHERE stock_units = 0 AND ... | WHERE prevents full table wipe | Discontinued items cleanly archived |
Syntax & Core SQL Implementation
-- Multi-row insert
INSERT INTO menu_items (flavor_name, category, retail_price, stock_units)
VALUES
('Belgian Dark Chocolate', 'Tubs', 350.00, 45),
('Alphonso Mango Sorbet', 'Sorbets', 280.00, 30),
('Roasted Almond Crunch', 'Tubs', 320.00, 20);
-- Safe conditional update
UPDATE menu_items
SET retail_price = retail_price * 1.05
WHERE category = 'Tubs';
-- MySQL Upsert (Insert or Update on Key Conflict)
INSERT INTO product_inventory (product_id, stock_qty)
VALUES (101, 50)
ON DUPLICATE KEY UPDATE stock_qty = stock_qty + 50;
Parameters & Arguments Breakdown
INSERT INTO: Specifies destination table and optional explicit column list.UPDATE ... SET: Modifies column values for rows matching the WHERE filter.ON DUPLICATE KEY UPDATE: MySQL-exclusive upsert syntax updating existing rows when a primary/unique key matches.
Sample Walkthrough (Q&A)
Question: What is the critical difference between DELETE FROM table_name and TRUNCATE TABLE table_name in MySQL?
SQL Pattern: DELETE vs TRUNCATE
Result: DELETE is DML (row-by-row, logged in transaction log, fires triggers); TRUNCATE is DDL (drops and recreates table instantly, resets AUTO_INCREMENT)
Explanation: TRUNCATE is vastly faster for emptying large tables but cannot be safely rolled back in transaction blocks.
Common Mistakes & Limitations
- Executing `UPDATE` or `DELETE` without a `WHERE` clause, accidentally altering or wiping out every single record in the table.
- Relying on MySQL Workbench "Safe Updates" mode without understanding why `SET SQL_SAFE_UPDATES = 0` is sometimes needed for batch operations.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Toggle Safe Updates in Session | SET SQL_SAFE_UPDATES = 0; | SET SQL_SAFE_UPDATES = 0; |
Pro Tips & Modern Best Practices
- Always run your `WHERE` condition in a `SELECT` statement first to verify which rows will be affected before executing `UPDATE` or `DELETE`.
- Batch multiple rows into a single `INSERT INTO ... VALUES (...), (...), (...)` statement to reduce network round-trips and maximize InnoDB commit throughput.