Download presentation
Presentation is loading. Please wait.
1
Isti upit iskazan na različite načine
1 - Zamena IN (SELECT ...) izraza
2
1.1 - Struktura CREATE TABLE partneri (
sifra_partnera INTEGER NOT NULL, ime_partnera VARCHAR(50) NOT NULL, CONSTRAINT pk_par PRIMARY KEY (sifra_partnera) ); CREATE TABLE adrese ( opis_adrese VARCHAR(20) NOT NULL, CONSTRAINT pk_tra PRIMARY KEY (sifra_partnera, opis_adrese), CONSTRAINT fk_tra_par FOREIGN KEY (sifra_partnera) REFERENCES partneri ON DELETE CASCADE ON UPDATE CASCADE
3
1.2 - Podaci INSERT INTO partneri (sifra_partnera, ime_partnera)
VALUES (1, 'Mika str'); VALUES (2, 'Pera doo'); VALUES (3, 'MELANIJA'); VALUES (4, 'Joca doo'); VALUES (5, 'Doo ZIKA i SONS');
4
1.2 - Podaci INSERT INTO adrese (sifra_partnera, opis_adrese)
VALUES (1, 'prodavnica'); VALUES (2, 'prodavnica br 1'); VALUES (2, 'prodavnica br 2'); VALUES (2, 'Kafana kod Pere'); VALUES (4, 'uprava'); VALUES (4, 'skladiste'); VALUES (4, 'prodavnica'); INSERT INTO adrese (sifra_partnera, ime_partnera) VALUES (5, 'Kafana Sinovi');
5
1.3 - Upit Pitanje: Treba prikazati partnere koji imaju barem dve adrese. Rezultat: sifra_partnera ime_partnera 2 Pera doo 4 Joca doo
6
1.4 - Rešenje sa IN SELECT p.sifra_partnera, p.ime_partnera
FROM partneri AS p WHERE p.sifra_partnera IN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
7
1.5 - Pretvaranje IN u EXISTS
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE p.sifra_partnera IN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
8
1.5 - Pretvaranje IN u EXISTS
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE p.sifra_partnera IN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
9
1.5 - Pretvaranje IN u EXISTS
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
10
1.6 - Rešenje sa EXISTS SELECT p.sifra_partnera, p.ime_partnera
FROM partneri AS p WHERE EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
11
1.7 - Pretvaranje EXISTS u JOIN
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
12
1.7 - Pretvaranje EXISTS u JOIN
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) ORDER BY p.sifra_partnera
13
1.7 - Pretvaranje EXISTS u JOIN
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p INNER JOIN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) AS pa ON p.sifra_partnera = pa.sifra_partnera ORDER BY p.sifra_partnera
14
1.7 - Pretvaranje EXISTS u JOIN
SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p INNER JOIN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) AS pa ON p.sifra_partnera = pa.sifra_partnera ORDER BY p.sifra_partnera
15
1.8 - Rešenje sa JOIN SELECT p.sifra_partnera, p.ime_partnera
FROM partneri AS p INNER JOIN (SELECT a.sifra_partnera FROM adrese AS a GROUP BY a.sifra_partnera HAVING COUNT(a.sifra_partnera) > 1 ) AS pa ON p.sifra_partnera = pa.sifra_partnera ORDER BY p.sifra_partnera
16
1.9 - Zaključak Prikazana su tri načina da se iskaže jedan isti upit.
Brzina izvršavanja prikazanih verzija zavisi od: - konkretnog RDBMS-a, - njegovog SQL optimizatora i - okolnosti pod kojima se upit izvršava (količina podataka, indeksi, dodatna ograničenja) Svaku od verzija treba isprobati, a koristiti onu koja se pokaže kao najbolja (najbrža).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.