Introduction
The UPDATE command changes data in a table. It needs caution because wrong changes can cause data loss or errors.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
UPDATE employees SET salary = 5000 WHERE id = 3;
UPDATE products SET price = price * 0.9 WHERE category = 'Books';
UPDATE customers SET city = 'New York';
CREATE TABLE users (id INT, name VARCHAR(20), age INT); INSERT INTO users VALUES (1, 'Alice', 25), (2, 'Bob', 30), (3, 'Carol', 22); UPDATE users SET age = age + 1 WHERE name = 'Bob'; SELECT * FROM users ORDER BY id;
UPDATE statement without a WHERE clause?WHERE in UPDATEWHERE clause limits which rows get updated. Without it, all rows are affected.WHEREWHERE, every row in the table changes, which can cause data loss or errors.WHERE updates all rows [OK]UPDATE table SET column = value WHERE condition;.products with rows:{id:1, price:100}, {id:2, price:200}, {id:3, price:300}UPDATE products SET price = price + 50 WHERE id > 1;id > 1 affects rows with id 2 and 3 only.UPDATE employees SET salary = salary + 1000 WHERE;WHERE clause syntaxWHERE clause must have a condition after it. Here it is empty, causing error.SET keyword and table name are correct; salary can be updated.WHERE clause is incomplete and causes a syntax error. -> Option AWHERE price < 100 is needed.Pass the test and this pattern moves unknown