INSERT, UPDATE, DELETE Statements: Mistakes
Module: SQL Fundamentals
UPDATE employees SET salary = 100000;
UPDATE employees SET salary = 100000 WHERE employee_id = 123;
Missing WHERE clause in UPDATE affects ALL rows. Always use WHERE to target specific rows. This is one of the most dangerous SQL mistakes - can corrupt entire tables.
Always test with SELECT before UPDATE/DELETE. Use transactions for safety.
Critical
Updates ALL employees to same salary!
DELETE FROM orders WHERE customer_id = 123;
BEGIN; DELETE FROM order_items WHERE order_id IN (SELECT order_id FROM orders WHERE customer_id = 123); DELETE FROM orders WHERE customer_id = 123; COMMIT;
Foreign key constraint violation if order_items reference orders. Must delete child records (order_items) before parent (orders). Use transactions to ensure atomicity.
Check foreign key constraints before DELETE. Delete child records first.
High
Foreign key constraint violation if order_items reference orders
INSERT INTO users (name, email) VALUES ('John', NULL);
INSERT INTO users (name, email) VALUES ('John', 'john@example.com');
Attempting to insert NULL into NOT NULL column causes constraint violation. Check table schema and provide required values for all NOT NULL columns.
Always check table constraints before INSERT. Use DESCRIBE or \d to see column requirements.
Medium
Cannot insert NULL into NOT NULL column