We use cookies to enhance your experience on the site
CodeWorlds

Bridge of Knowledge - why join shelves

Welcome @name to the Bridge of Knowledge - a stone walkway that connects two wings of the Great Library. On one side stands the shelf of books, on the other the shelf of author descriptions. A book scroll alone will not tell you who its creator was - you must cross the bridge of cross-references.

Data split across shelves

In our Library we do not keep everything on one scroll. An author's name is not repeated next to each of their books - that would be chaos and a waste of papyrus. Instead we have a separate

autorzy
shelf, and each book carries only a cross-reference to it.

| ksiazki.id | tytul | autor_id | |------------|-------|----------| | 1 | Iliada | 3 | | 2 | Odyseja | 3 |

| autorzy.id | imie | nazwisko | |------------|------|----------| | 3 | Homer | z Jonii |

Foreign key - a cross-reference between shelves

The

ksiazki.autor_id
column is a foreign key. It points to a row in another shelf through its primary key:
autor_id
in the
ksiazki
table points to
id
in the
autorzy
table.

This is exactly how a whole relational database works - shelves linked by cross-references:

  • ksiazki.autor_id
    autorzy.id
  • ksiazki.kategoria_id
    kategorie.id
  • wypozyczenia.ksiazka_id
    ksiazki.id
  • wypozyczenia.czytelnik_id
    czytelnicy.id

To see a book's title and the author's surname in one result, we must learn to walk across these cross-references. We call this art JOIN - joining tables. Let us step onto the bridge, @name!

Go to CodeWorlds