BİL 354 Veritnı Sistemleri Reltionl lger (İlişkisel Ceir)
Reltionl Queries Query lnguges: llow mnipultion nd retrievl of dt from dtse. Reltionl model supports simple, powerful QLs: Strong forml foundtion sed on logic. llows for much optimiztion. Query Lnguges!= progrmming lnguges! QLs not intended to e used for complex clcultions. QLs support esy, efficient ccess to lrge dt sets.
Forml Reltionl Query Lnguges Two mthemticl Query Lnguges form the sis for rel lnguges (e.g. SQL), nd for implementtion: Reltionl lger: More opertionl, very useful for representing execution plns. Reltionl Clculus: Lets users descrie wht they wnt, rther thn how to compute it. (Nonopertionl,declrtive.) 3
Wht is Reltionl lger? n lger whose opernds re reltions or vriles tht represent reltions. Opertors re designed to do the most common things tht we need to do with reltions in dtse. The result is n lger tht cn e used s query lnguge for reltions. 4
Reltionl lger Procedurl lnguge Six sic opertors select project union set difference Crtesin product renme The opertors tke one or more reltions s inputs nd give new reltion s result. Dtse System Concepts 3.5 Silerschtz, Korth nd Sudrshn
Select Opertion Exmple Reltion r B C D 7 5 7 3 3 0 =B ^ D > 5 (r) B C D 7 3 0 Dtse System Concepts 3.6 Silerschtz, Korth nd Sudrshn
Select Opertion Nottion: p (r) p is clled the selection predicte Defined s: p (r) = {t t r nd p(t)} Where p is formul in propositionl clculus consisting of terms connected y : (nd), (or), (not) Ech term is one of: <ttriute> op <ttriute> or <constnt> where op is one of: =,, >,. <. Exmple of selection: rnch-nme= Perryridge (ccount) Dtse System Concepts 3.7 Silerschtz, Korth nd Sudrshn
Project Opertion Exmple Reltion r: B C 0 0 30 40,C (r) C C = Dtse System Concepts 3.8 Silerschtz, Korth nd Sudrshn
Project Opertion Nottion:,,, k (r) where, re ttriute nmes nd r is reltion nme. The result is defined s the reltion of k columns otined y ersing the columns tht re not listed Duplicte rows removed from result, since reltions re sets E.g. To eliminte the rnch-nme ttriute of ccount ccount-numer, lnce (ccount) Dtse System Concepts 3.9 Silerschtz, Korth nd Sudrshn
Union Opertion Exmple Reltions r, s: B B 3 r s r s: B 3 Dtse System Concepts 3.0 Silerschtz, Korth nd Sudrshn
Union Opertion Nottion: r s Defined s: r s = {t t r or t s} For r s to e vlid.. r, s must hve the sme rity (sme numer of ttriutes). The ttriute domins must e comptile (e.g., nd column of r dels with the sme type of vlues s does the nd column of s) E.g. to find ll customers with either n ccount or lon customer-nme (depositor) customer-nme (orrower) Dtse System Concepts 3. Silerschtz, Korth nd Sudrshn
Set Difference Opertion Exmple Reltions r, s: B B 3 r s r s: B Dtse System Concepts 3. Silerschtz, Korth nd Sudrshn
Set Difference Opertion Nottion r s Defined s: r s = {t t r nd t s} Set differences must e tken etween comptile reltions. r nd s must hve the sme rity ttriute domins of r nd s must e comptile Dtse System Concepts 3.3 Silerschtz, Korth nd Sudrshn
Crtesin-Product Opertion-Exmple Reltions r, s: B C D E r 0 0 0 0 r x s: s B C D E 0 0 0 0 0 0 0 0 Dtse System Concepts 3.4 Silerschtz, Korth nd Sudrshn
Nottion r x s Defined s: Crtesin-Product Opertion r x s = {t q t r nd q s} ssume tht ttriutes of r(r) nd s(s) re disjoint. (Tht is, R S = ). If ttriutes of r(r) nd s(s) re not disjoint, then renming must e used. Dtse System Concepts 3.5 Silerschtz, Korth nd Sudrshn
Composition of Opertions Cn uild expressions using multiple opertions Exmple: =C (r x s) r x s B C D E =C (r x s) 0 0 0 0 0 0 0 0 B C D E 0 0 0 Dtse System Concepts 3.6 Silerschtz, Korth nd Sudrshn
Renme Opertion llows us to nme, nd therefore to refer to, the results of reltionl-lger expressions. llows us to refer to reltion y more thn one nme. Exmple: x (E) returns the expression E under the nme X If reltionl-lger expression E hs rity n, then x (,,, n) (E) returns the result of expression E under the nme X, nd with the ttriutes renmed to,,., n. Dtse System Concepts 3.7 Silerschtz, Korth nd Sudrshn
Bnking Exmple rnch (rnch-nme, rnch-city, ssets) customer (customer-nme, customer-street, customer-only) ccount (ccount-numer, rnch-nme, lnce) lon (lon-numer, rnch-nme, mount) depositor (customer-nme, ccount-numer) orrower (customer-nme, lon-numer) Dtse System Concepts 3.8 Silerschtz, Korth nd Sudrshn
Exmple Queries Find ll lons of over $00 mount > 00 (lon) Find the lon numer for ech lon of n mount greter thn $00 lon-numer ( mount > 00 (lon)) Dtse System Concepts 3.9 Silerschtz, Korth nd Sudrshn
Exmple Queries Find the nmes of ll customers who hve lon, n ccount, or oth, from the nk customer-nme (orrower) customer-nme (depositor) Find the nmes of ll customers who hve lon nd n ccount t nk. customer-nme (orrower) customer-nme (depositor) Dtse System Concepts 3.0 Silerschtz, Korth nd Sudrshn
Exmple Queries Find the nmes of ll customers who hve lon t the Perryridge rnch. customer-nme ( rnch-nme= Perryridge ( orrower.lon-numer = lon.lon-numer (orrower x lon))) Find the nmes of ll customers who hve lon t the Perryridge rnch ut do not hve n ccount t ny rnch of the nk. customer-nme ( rnch-nme = Perryridge ( orrower.lon-numer = lon.lon-numer (orrower x lon))) customer-nme (depositor) Dtse System Concepts 3. Silerschtz, Korth nd Sudrshn
Exmple Queries Find the nmes of ll customers who hve lon t the Perryridge rnch. Query customer-nme ( rnch-nme = Perryridge ( orrower.lon-numer = lon.lon-numer (orrower x lon))) Query customer-nme ( lon.lon-numer = orrower.lon-numer ( ( rnch-nme = Perryridge (lon)) x orrower)) Dtse System Concepts 3. Silerschtz, Korth nd Sudrshn
Find the lrgest ccount lnce Renme ccount reltion s d The query is: Exmple Queries lnce (ccount) - ccount.lnce ( ccount.lnce < d.lnce (ccount x d (ccount))) Dtse System Concepts 3.3 Silerschtz, Korth nd Sudrshn
Forml Definition sic expression in the reltionl lger consists of either one of the following: reltion in the dtse constnt reltion Let E nd E e reltionl-lger expressions; the following re ll reltionl-lger expressions: E E E - E E x E p (E ), P is predicte on ttriutes in E s (E ), S is list consisting of some of the ttriutes in E x (E ), x is the new nme for the result of E Dtse System Concepts 3.4 Silerschtz, Korth nd Sudrshn
dditionl Opertions We define dditionl opertions tht do not dd ny power to the reltionl lger, ut tht simplify common queries. Set intersection Nturl join Division ssignment Dtse System Concepts 3.5 Silerschtz, Korth nd Sudrshn
Set-Intersection Opertion Nottion: r s Defined s: r s ={ t t r nd t s } ssume: r, s hve the sme rity ttriutes of r nd s re comptile Note: r s = r - (r - s) Dtse System Concepts 3.6 Silerschtz, Korth nd Sudrshn
Set-Intersection Opertion - Exmple Reltion r, s: B B 3 r s r s B Dtse System Concepts 3.7 Silerschtz, Korth nd Sudrshn
Nottion: r s Nturl-Join Opertion Let r nd s e reltions on schems R nd S respectively. Then, r s is reltion on schem R S otined s follows: Consider ech pir of tuples t r from r nd t s from s. If t r nd t s hve the sme vlue on ech of the ttriutes in R S, dd tuple t to the result, where Exmple: t hs the sme vlue s t r on r t hs the sme vlue s t s on s R = (, B, C, D) S = (E, B, D) Result schem = (, B, C, D, E) r s is defined s: r., r.b, r.c, r.d, s.e ( r.b = s.b r.d = s.d (r x s)) Dtse System Concepts 3.8 Silerschtz, Korth nd Sudrshn
Silerschtz, Korth nd Sudrshn 3.9 Dtse System Concepts Nturl Join Opertion Exmple Reltions r, s: B 4 C D B 3 3 D E r B C D E s r s
Division Opertion Suited to queries tht include the phrse for ll. Let r nd s e reltions on schems R nd S respectively where R = (,, m, B,, B n ) S = (B,, B n ) The result of r s is reltion on schem R S = (,, m ) r s r s = { t t R-S (r) u s ( tu r ) } Dtse System Concepts 3.30 Silerschtz, Korth nd Sudrshn
Silerschtz, Korth nd Sudrshn 3.3 Dtse System Concepts Division Opertion Exmple Reltions r, s: r s: B B 3 3 4 6 r s
Silerschtz, Korth nd Sudrshn 3.3 Dtse System Concepts nother Division Exmple B C D E 3 Reltions r, s: r s: D E B C r s
Division Opertion (Cont.) Property Let q r s Then q is the lrgest reltion stisfying q x s r Definition in terms of the sic lger opertion Let r(r) nd s(s) e reltions, nd let S R r s = R-S (r) R-S ( ( R-S (r) x s) R-S,S (r)) To see why R-S,S (r) simply reorders ttriutes of r R-S ( R-S (r) x s) R-S,S (r)) gives those tuples t in R-S (r) such tht for some tuple u s, tu r. Dtse System Concepts 3.33 Silerschtz, Korth nd Sudrshn
ssignment Opertion The ssignment opertion () provides convenient wy to express complex queries. Write query s sequentil progrm consisting of series of ssignments followed y n expression whose vlue is displyed s result of the query. ssignment must lwys e mde to temporry reltion vrile. Exmple: Write r s s temp R-S (r) temp R-S ((temp x s) R-S,S (r)) result = temp temp The result to the right of the is ssigned to the reltion vrile on the left of the. My use vrile in susequent expressions. Dtse System Concepts 3.34 Silerschtz, Korth nd Sudrshn
Exmple Queries Find ll customers who hve n ccount from t lest the Downtown nd the Uptown rnches. Query CN ( BN= Downtown (depositor ccount)) CN ( BN= Uptown (depositor ccount)) where CN denotes customer-nme nd BN denotes rnch-nme. Query customer-nme, rnch-nme (depositor ccount) temp(rnch-nme) ({( Downtown ), ( Uptown )}) Dtse System Concepts 3.35 Silerschtz, Korth nd Sudrshn
Exmple Queries Find ll customers who hve n ccount t ll rnches locted in Brooklyn city. customer-nme, rnch-nme (depositor ccount) rnch-nme ( rnch-city = Brooklyn (rnch)) Dtse System Concepts 3.36 Silerschtz, Korth nd Sudrshn