c2cedge
LibrarySQL & Databases › Ch 14
SQL & Databases · E — Data Definition & Management

DML: INSERT, UPDATE, DELETE

Data Manipulation Language changes the rows. The single most important habit here: never UPDATE or DELETE without a WHERE.

Test weight: HighSkill: Modify data safelyDifficulty: Medium

Data Manipulation Language (DML) changes the contents of tables: INSERT adds rows, UPDATE changes existing rows, and DELETE removes rows. These are the statements that can do real damage, so the discipline of always scoping them with WHERE is essential.

Add, change, remove

INSERT adds one or many rows; UPDATE ... SET ... WHERE changes the matching rows; DELETE ... WHERE removes the matching rows. The WHERE clause decides which rows are affected — omit it and you hit every row.

Insert rows
INSERT INTO departments (dept_id, dept_name, location)
VALUES (40, 'Finance', 'Delhi');

-- multiple rows at once
INSERT INTO departments (dept_id, dept_name, location) VALUES
  (50, 'Legal', 'Chennai'),
  (60, 'Ops',   'Hyderabad');
Update and delete (always with WHERE)
UPDATE employees
SET    salary = salary * 1.10     -- 10% raise
WHERE  dept_id = 10;             -- only department 10

DELETE FROM employees
WHERE  status = 'inactive';
⚡ The edge
  • Always include a WHERE on UPDATE and DELETE. Without it, UPDATE employees SET salary = 0 zeroes everyone, and DELETE FROM employees empties the table.
  • An UPSERT (INSERT ... ON CONFLICT / ON DUPLICATE KEY UPDATE) inserts a row or updates it if the key already exists — handy for 'insert or refresh' logic.
Worked example
Give every employee in department 10 a 10% raise.
  1. This changes existing rows, so it's an UPDATE.
  2. Set salary to salary * 1.10.
  3. Scope it to department 10 with WHERE dept_id = 10 — never omit the WHERE.
Worked example
What happens if you run UPDATE employees SET salary = 50000; with no WHERE?
  1. UPDATE with no WHERE matches every row in the table.
  2. So every employee's salary is set to 50000, wiping out the real values.
  3. This is the classic costly mistake — always add a WHERE (and test with SELECT first).
⚠ Watch out
  • Missing WHERE on UPDATE/DELETE changes/removes every row — the most dangerous SQL mistake.
  • Test the WHERE with a SELECT first to see exactly which rows you'll affect.
  • Inserting a row that violates a constraint (FK, NOT NULL, UNIQUE) is rejected — by design.
Practice this — take a timed mock →
1,300+ questions, scored, with a weak-area report.
Know who's ready. Not who finished.
HomeLibraryPrivacyTerms