БЛОК 3. / (15:00 - 18:30) · 2020. 5. 23. · (knjige), even if there are no matches in the...
TRANSCRIPT
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
60
БЛОК 3. / (15:00 - 18:30)
SPAJANJE TABELA: JOIN
Joining Tables with JOIN Clauses
Here is a simple example of three basic JOIN types, which you can experiment
with in order to see what the different joins accomplish:
CREATE TABLE mi1 ( a INT );
CREATE TABLE mi2 ( b INT );
INSERT INTO mi1 VALUES (1), (2), (3);
INSERT INTO mi2 VALUES (2), (4);
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
61
SELECT * FROM mi1 INNER JOIN mi2 ON mi1.a = mi2.b;
(INNER) JOIN: Returns records that have matching values in both tables
SELECT * FROM mi1 CROSS JOIN mi2;
A CROSS JOIN produces a result set in which every row in each table is joined to
every row in the other table; this is also called a cartesian product.
In MariaDB the CROSS keyword can be omitted, as it does nothing. Any JOIN
without an ON clause is a CROSS JOIN.
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
62
SELECT * FROM mi1 LEFT JOIN mi2 ON mi1.a = mi2.b;
The LEFT JOIN is an outer join, which produces a result set with all rows from the
table on the "left" (t1); the values for the columns in the other table (t2) depend on
whether or not a match was found.
If no match is found, all columns from that table are set to NULL for that row.
SELECT * FROM mi2 LEFT JOIN mi1 ON mi1.a = mi2.b;
The RIGHT JOIN is similar to the LEFT JOIN, though its resultset contains all rows
from the right table, and the left table's columns will be filled with NULLs when
needed..
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
63
NAPOMENA: Ako ne postiji u tabeli knjige roman DOROTEJ (izdanje 2017.)
dodati zapis za izdanje romana Dorotej iz 2017.
MariaDB [biblioteka]> INSERT INTO knjige(naslovKnjige,serijaID,autorID,godIzd)
-> VALUE
-> ("Dorotej",2,3,"2017");
Query OK, 1 row affected (0.737 sec)
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
64
Tekuće stanje u tabelama autori i knjige:
NAPOMENA!!!: Ako tekuće stanje u tabelama nije kao u tabelama
iznad, ažurirati tabele knjige i autori po uzoru na sledeće naredbe.
INSERT INTO autori(imeAutora, prezimeAutora, porekloAutora)
-> VALUE
-> ("Dobrica","Cosic","Srbija");
ili
UPDATE knjige SET
-> izdavacID=1,godIzd=1987,opis=" Prag, ljubav, politika..."
-> WHERE knjigaID=1;
ili
MariaDB [biblioteka]> INSERT INTO knjige(naslovKnjige, godIzd)
-> VALUE
-> ("Kad su cvetale tikve",1987);
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
65
Da bi mogli navesti spisak knjiga (nasloveKnjiga) po autorima, mora tablica knjiga
SPOJITI sa tablicom autora. To se moze realizovati pomocu JOIN klauzule:
Notice that the primary table from which we're drawing data is given in
the FROM clause. The table to which we're joining is given in the JOIN clause along
with the commonly named column (i.e., autorID) that we're using for the join.
U prethodnom primeru prikazani su svi autori.
Ako je potrebno prikazati samo dela Milana Kundere onda je postupak sledeci:
This statement will list the titles of Kundera books stored in the database.
Notice that we've added the AS parameter next to the column name naslovKnjige to change the
column heading in the results set to Kunderine knjige. This is known as an alias.
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
66
Primer sa kompletnijim JOIN izrazom:
There are a few possible ways to construct a join.
This method works if you're using a newer version of MariaDB and if both tables
contain a column of the same name and value.
Otherwise you'll have to redo the JOIN clause to look something like this:
...
JOIN autori ON autorID = rowID
...
This excerpt is based on the assumption that the key field in the autori table is not
called autorID, but rowID instead.
There's much more that can be said about joins, but that would make for a much
longer text.
U izrazu WHERE često se, radi fleksibilnosti, koriste džoker znaci:
WHERE name_last LIKE 'Dostoevsk%'
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
67
PARAMETRI u naredbi SELECT
Kao što je dato u opštem prikazu, izrazi i ključna reč SELECT imaju veliki broj
parametara. Navode se neki, koji mogu biti od koristi:
DISTINCT
KLJUČNA reč DISTINCT, se koristi kada želimo da izbegnemo duplicate u prikazu.
Na primer, ako u bazi postoji više naslova SMESNE LUBAVI od Milana Kundere ali
sa razlicitim ID brojevima (jer su različite godine i/ili izdavači), a potrebno je da se
prikaze samo spisak naslova (nezavisno od izdavača i godine izdavanja onda se
može koristiti DISTINCT.
Ako je potrebno kreirati listing sa prikazima naslova D. Nenadica, onda je sintaksa
sledeca:
Ali ako je potrebno kreirati listing sa prikazima naslova D. Nenadica, ALI BEY
PONAVLJANJA onda je sintaksa sledeca:
SELECT DISTINCT naslovKnjige,
CONCAT(imeAutora,” “,prezimeAutora) AS pisac
FROM knjige
JOIN autori USING (autorID)
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
68
WHERE prezimeAutora = “Nenadic”
HIGH PRIORITY
Koristi se kod baza koje su veoma opterecene UPITIMA.
SELECT DISTINCT HIGH PRIORITY naslovKnjige,
CONCAT(imeAutora,” “,prezimeAutora) AS pisac
FROM knjige
JOIN autori USING (autorID)
WHERE prezimeAutora = “Nenadic”
ORDER BY naslovKnjige;
SQL_CALC_FOUND_ROWS
Prebrajanje broja zapisa koju su u rezultatu upita - primer
SELECT DISTINCT SQL_CALC_FOUND_ROWS naslovKnjige,
CONCAT(imeAutora,” “,prezimeAutora) AS pisac
FROM knjige
JOIN autori USING (autorID)
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
69
WHERE prezimeAutora = “Nenadic”
ORDER BY naslovKnjige;
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
70
Primer UPITA:
MariaDB [biblioteka]> SELECT knjigaID, naslovKnjige,imeAutora,
prezimeAutora
-> FROM knjige
-> RIGHT JOIN autori ON knjige.autorID = autori.autorID
-> ORDER BY autori.autorID;
The RIGHT JOIN keyword returns all records from the right
table (autori), even if there are no matches in the left table
(knjige).
(Pitanje: Zasto u rezultatu upita ne postoji knjiga „Kad su cvetale
tikve“?)
Primer UPITA ver1:
MariaDB [biblioteka]> SELECT knjigaID, naslovKnjige,imeAutora,
prezimeAutora
-> FROM knjige
-> LEFT JOIN autori ON knjige.autorID = autori.autorID
-> ORDER BY autori.autorID;
АКАДЕМИЈА СТРУКОВНИХ СТУДИЈА ЗАПАДНА СРБИЈА
Базе података, SQL, MariaDB др Милован Миливојевић
71
The LEFT JOIN keyword returns all records from the left table
(knjige), even if there are no matches in the right table (autori).
Primer UPITA ver2:
SELECT knjigaID, naslovKnjige,imeAutora, prezimeAutora
-> FROM autori
-> LEFT JOIN knjige ON knjige.autorID = autori.autorID
-> ORDER BY autori.autorID;
OBRATITI PAZNJU: LEVI I DESNI UPIT MOGU IMATI iste REZULTATE (u zavisnosti od
rasporeda tabela LEVO/DESNO u odnosu na kljucnu rec JOIN)!