Download presentation
Presentation is loading. Please wait.
Published byTerry Napper Modified over 10 years ago
1
Isti upit iskazan na različite načine 2 - Zamena NOT IN (SELECT...) izraza www.baze-podataka.net
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 ); www.baze-podataka.net
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'); www.baze-podataka.net
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'); www.baze-podataka.net
5
2.3 - Upit Pitanje: Treba prikazati partnere koji nemaju adresu. Rezultat: sifra_partnera ime_partnera -------------- ------------ 3 MELANIJA www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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 www.baze-podataka.net
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). www.baze-podataka.net
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.