Presentation is loading. Please wait.

Presentation is loading. Please wait.

Isti upit iskazan na različite načine 2 - Zamena NOT IN (SELECT...) izraza www.baze-podataka.net.

Similar presentations


Presentation on theme: "Isti upit iskazan na različite načine 2 - Zamena NOT IN (SELECT...) izraza www.baze-podataka.net."— Presentation transcript:

1 Isti upit iskazan na različite načine 2 - Zamena NOT IN (SELECT...) izraza

2 2.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 ( sifra_partnera INTEGER NOT NULL, 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 2.2 - Podaci INSERT INTO partneri (sifra_partnera, ime_partnera) VALUES (1, 'Mika str'); INSERT INTO partneri (sifra_partnera, ime_partnera) VALUES (2, 'Pera doo'); INSERT INTO partneri (sifra_partnera, ime_partnera) VALUES (3, 'MELANIJA'); INSERT INTO partneri (sifra_partnera, ime_partnera) VALUES (4, 'Joca doo'); INSERT INTO partneri (sifra_partnera, ime_partnera) VALUES (5, 'Doo ZIKA i SONS');

4 2.2 - Podaci INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (1, 'prodavnica'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (2, 'prodavnica br 1'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (2, 'prodavnica br 2'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (2, 'Kafana kod Pere'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (4, 'uprava'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (4, 'skladiste'); INSERT INTO adrese (sifra_partnera, opis_adrese) VALUES (4, 'prodavnica'); INSERT INTO adrese (sifra_partnera, ime_partnera) VALUES (5, 'Kafana Sinovi');

5 2.3 - Upit Pitanje: Treba prikazati partnere koji nemaju adresu. Rezultat: sifra_partnera ime_partnera MELANIJA

6 2.4 - Rešenje sa NOT IN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE p.sifra_partnera NOT IN (SELECT a.sifra_partnera FROM adrese AS a ) ORDER BY p.sifra_partnera

7 2.5 - Pretvaranje IN u EXISTS SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE p.sifra_partnera NOT IN (SELECT a.sifra_partnera FROM adrese AS a ) ORDER BY p.sifra_partnera

8 2.5 - Pretvaranje IN u EXISTS SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE p.sifra_partnera NOT IN (SELECT a.sifra_partnera FROM adrese AS a ) ORDER BY p.sifra_partnera

9 2.5 - Pretvaranje IN u EXISTS SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE NOT EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera ) ORDER BY p.sifra_partnera

10 2.5 - Pretvaranje IN u EXISTS SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE NOT EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera ) ORDER BY p.sifra_partnera

11 2.6 - Rešenje sa NOT EXISTS SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE NOT EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera ) ORDER BY p.sifra_partnera

12 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE NOT EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera ) ORDER BY p.sifra_partnera

13 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p WHERE NOT EXISTS (SELECT a.sifra_partnera FROM adrese AS a WHERE p.sifra_partnera = a.sifra_partnera ) ORDER BY p.sifra_partnera

14 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p LEFT OUTER JOIN (SELECT a.sifra_partnera FROM adrese AS a ) AS pa ON p.sifra_partnera = pa.sifra_partnera ORDER BY p.sifra_partnera

15 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p LEFT OUTER JOIN (SELECT a.sifra_partnera FROM adrese AS a ) AS pa ON p.sifra_partnera = pa.sifra_partnera ORDER BY p.sifra_partnera

16 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p LEFT OUTER JOIN (SELECT a.sifra_partnera FROM adrese AS a ) AS pa ON p.sifra_partnera = pa.sifra_partnera WHERE pa.sifra_partnera IS NULL ORDER BY p.sifra_partnera

17 2.7 - Pretvaranje EXISTS u JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p LEFT OUTER JOIN (SELECT a.sifra_partnera FROM adrese AS a ) AS pa ON p.sifra_partnera = pa.sifra_partnera WHERE pa.sifra_partnera IS NULL ORDER BY p.sifra_partnera

18 2.8 - Rešenje sa JOIN SELECT p.sifra_partnera, p.ime_partnera FROM partneri AS p LEFT OUTER JOIN (SELECT a.sifra_partnera FROM adrese AS a ) AS pa ON p.sifra_partnera = pa.sifra_partnera WHERE pa.sifra_partnera IS NULL ORDER BY p.sifra_partnera

19 2.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).


Download ppt "Isti upit iskazan na različite načine 2 - Zamena NOT IN (SELECT...) izraza www.baze-podataka.net."

Similar presentations


Ads by Google