We use cookies to enhance your experience on the site
CodeWorlds

Table aliases - short catalog numbers

Have you already noticed the shortcuts

k
and
a
in the queries? These are table aliases - temporary, short catalog numbers that we give the shelves for the duration of a single query.

Why aliases?

When we join tables, the

id
column exists in both. How is the Library to know whether we mean
ksiazki.id
or
autorzy.id
? We must prefix the column name with the table name. Full names, however, are long:

1SELECT ksiazki.tytul, autorzy.nazwisko
2FROM ksiazki
3INNER JOIN autorzy ON ksiazki.autor_id = autorzy.id;

Shorter with aliases

We give an alias right after the table name, writing a letter (or short shortcut) after a space:

1SELECT k.tytul, a.nazwisko
2FROM ksiazki k
3INNER JOIN autorzy a ON k.autor_id = a.id;
  • ksiazki k
    - from now on, in this query,
    k
    means
    ksiazki
    .
  • autorzy a
    -
    a
    means
    autorzy
    .

Thanks to this,

k.tytul
and
a.nazwisko
are short and unambiguous. An alias works only within one query - it does not change the table's name in the database.

Good practice

By convention we choose aliases so that they resemble the table name:

k
for
ksiazki
,
a
for
autorzy
,
c
for
czytelnicy
,
w
for
wypozyczenia
,
kat
for
kategorie
. When joining three shelves, aliases save your eyes!

Go to CodeWorlds