UPDATE changes values in the rows that match a WHERE condition:
UPDATE product SET price = 0.95 WHERE code = 'P02';
SET lists the columns to change. You can change several at once, separated by commas: SET price = 0.95, in_stock = 150.
The WHERE decides which rows change. Leave it out and every row is updated:
UPDATE product SET price = 0; -- changes ALL products!
So nearly every UPDATE should have a WHERE. The checker runs your UPDATE, then reads the table back to confirm only the right row changed.
Question. Five notebooks were sold. Reduce the stock of the product titled Notebook by 5.
Answer. The new value can be worked out from the old one. in_stock - 5 is calculated separately for each row the WHERE picks:
UPDATE product SET in_stock = in_stock - 5 WHERE title = 'Notebook';
code | title | in_stock
P01 | Notebook | 35
P02 | Pen | 200
P03 | Backpack | 15
Only the Notebook row changed: 40 became 35.
ANDinstead of a comma inSET.SET price = 0.95 AND in_stock = 150is read as one true-or-false expression, so the price becomes0and the stock does not change. Separate the columns with a comma.- Text without quotes.
WHERE code = P02fails withno such column: P02. WriteWHERE code = 'P02'. UPDATEwithout aWHEREchanges every row — always add theWHERE.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
코드 'P02'인 제품의 가격을 0.95로 변경하십시오. 오직 해당 제품만 변경되도록 반드시 WHERE를 사용하십시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
배송이 도착했으므로 모든 제품의 재고가 10 증가했습니다. 표현식 — SET in_stock = in_stock + 10 — 을 사용하여 업데이트하되, WHERE 없이 처리하여 모든 행에 적용되게 하십시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
백팩(P03)은 이제 16.50이며 재고가 12개입니다. 두 값을 하나의 UPDATE에서 변경하시오. SET에서 쉼표로 분리하십시오.
출력을 보려면 '실행'을 클릭하세요.