We use cookies to enhance your experience on the site
CodeWorlds

UPDATE - correcting a scroll

Sometimes data in the Library must change: a book's price rises, a reader changes city, a book returns from a loan. For this we use UPDATE.

Syntax

1UPDATE ksiazki
2SET cena = 59.90
3WHERE id = 5;

The three parts of this command:

  • UPDATE ksiazki - which shelf (table) we are correcting.
  • SET cena = 59.90 - which column and to what value we change it.
  • WHERE id = 5 - which row (or rows) is to be changed.

Changing several columns at once

In a single

SET
we can change several columns, separating them with commas:

1UPDATE czytelnicy
2SET miasto = 'Aleksandria', email = 'hypatia@biblioteka.aleksandria'
3WHERE id = 7;

MOST IMPORTANT: always WHERE!

Look at this command WITHOUT the

WHERE
clause:

1UPDATE ksiazki
2SET cena = 59.90;

This will change the price of ALL books in the Library to 59.90! Every scroll gets the same price, and the original data is lost forever.

WHERE
is the scribe's sight. Without it
UPDATE
strikes the entire shelf. Before you run an UPDATE, always ask yourself: which exact rows do I want to change? - and write the matching
WHERE
.

Go to CodeWorlds