We use cookies to enhance your experience on the site
CodeWorlds

Views (VIEW) - a saved look

Sometimes we repeat a certain query over and over - for example "show available books". Instead of writing it each time, we can save it under a name. That is a view (VIEW).

What is a view?

A view is a saved

SELECT
query that can be used as if it were an ordinary table. A view stores no data - each time it reaches into the real tables and shows the current result.

Creating a view

1CREATE VIEW dostepne_ksiazki AS
2SELECT * FROM ksiazki WHERE dostepna = 1;

From now on the Keeper can ask the Library briefly:

1SELECT * FROM dostepne_ksiazki;

and will always get a fresh list of books ready to be borrowed, without repeating the condition

WHERE dostepna = 1
.

Why do we need views?

  • Readability - a complex query is hidden behind a simple name.
  • Convenience - written once, used many times.
  • Security - we can show a reader only the view, hiding sensitive columns.

Dropping a view

1DROP VIEW dostepne_ksiazki;

A view is like a ready spyglass on the tower - once set, it always shows what is needed.

Go to CodeWorlds