← SQL and databases

Changing rows with UPDATE

Edit values, and always use WHERE.

Start this lesson

UPDATE table SET column = new_value WHERE condition; changes existing rows. The WHERE decides which rows change. Without WHERE, every row in the table changes, which is one of the most common and painful mistakes in SQL. You can also compute the new value from the old one: SET price = price * 1.1.

UPDATE products
SET price = 42.0
WHERE id = 3;

Changes the price of only the Desk Lamp (id 3). All other products keep their prices.

A safe habit: first run SELECT * FROM products WHERE id = 3; to see exactly which rows match. Then change SELECT * into UPDATE … SET … and keep the same WHERE.

To change two columns at once: SET price = 42.0, stock = 10.