We use cookies to enhance your experience on the site
CodeWorlds

The Hall of Seals - transactions and views

In the finale you will learn two tools of a master: transactions that protect data, and views that bring order to queries.

Transaction - all or nothing

A transaction joins several operations into one indivisible whole. Either all of them run, or none. We begin it with

START TRANSACTION
, confirm with
COMMIT
, and in case of an error undo with
ROLLBACK
:

1START TRANSACTION;
2UPDATE ksiazki SET cena = 25.00 WHERE id = 3;
3COMMIT;

If something went wrong before

COMMIT
, a simple
ROLLBACK
returns the database to the state before the transaction. Thanks to this we never leave data in a half-finished, broken state.

View (VIEW) - a saved query

A view is a saved query that behaves like a virtual table. We create it once, then query it like an ordinary shelf:

1CREATE VIEW dostepne_ksiazki AS
2SELECT tytul, cena FROM ksiazki WHERE dostepna = 1;

From now on

SELECT * FROM dostepne_ksiazki;
will return only available books - without repeating the whole condition. Views bring order to frequently used questions, just as the Pinakes brought order to the scrolls.

These are your last new spells. The seal is pressed - the data is safe!

Go to CodeWorlds