We use cookies to enhance your experience on the site
CodeWorlds

DELETE - taking a scroll off the shelf

When a book is destroyed or a reader withdraws from the Library, the scribe removes the matching row with the DELETE FROM command.

Syntax

1DELETE FROM czytelnicy
2WHERE id = 12;

Two parts:

  • DELETE FROM czytelnicy - which shelf we are removing from.
  • WHERE id = 12 - which row is to disappear.

Notice that with

DELETE
we do not provide any columns - we remove the whole row, not a single field. (To clear just one field, we use
UPDATE ... SET field = NULL
.)

Removing many rows

WHERE
may point to more than one row:

1DELETE FROM ksiazki
2WHERE dostepna = 0;

This will remove all books marked as unavailable.

SECOND GREAT WARNING: always WHERE!

This command looks harmless, yet it is the most dangerous of them all:

1DELETE FROM czytelnicy;

Without

WHERE
it removes ALL readers - the whole shelf is emptied! There is no trash bin, no undo. The scrolls are gone.

That is why an experienced scribe has an iron habit: write WHERE first, only then the rest of the command. Some even check the condition with

SELECT * FROM table WHERE ...
ended with a ";" to see which rows will disappear before they issue the
DELETE
.

Go to CodeWorlds