DATABASTEKNIK - DL6 Spring 004 An introductury course on dtse systems http://user.it.uu.se/~udl/dt-vt004/ Kjell Orsorn Uppsl Dtse Lortory Deprtment of Informtion Technology, Uppsl University, Uppsl, Sweden Kjell Orsorn 3//04
Introduction to Reltionl Alger Elmsri/Nvthe ch 6 Kjell Orsorn Deprtment of Informtion Technology Uppsl University, Uppsl, Sweden Kjell Orsorn 3//04
3 Query lnguges Lnguges where users cn express wht informtion to retrieve from the dtse. Ctegories of query lnguges: Procedurl Non-procedurl (declrtive) Forml ( pure ) lnguges: Reltionl lger Reltionl clculus Tuple-reltionl clculus Domin-reltionl clculus Forml lnguges form underlying sis of query lnguges tht people use. Kjell Orsorn 3//04
4 Reltionl lger Reltionl lger is procedurl lngue Opertions in reltionl lger tkes two or more reltions s rguments nd return new reltion. Reltionl lgeric opertions: Opertions from set theory: Union, Intersection, Difference, Crtesin product Opertions specificlly introduced for the reltionl dt model: Select, Project, Join It hve een shown tht the select, project, union, difference, nd crtesin product opertions form complete set. Tht is ny other reltionl lger opertion cn e expressed in these. Kjell Orsorn 3//04
Opertions from set theory Reltions re required to e union comptile to e le to tke prt in the union, intersection nd difference opertions. Two reltions R nd R is sid to e union-comptile if: R Õ D xd x...xd n nd R Õ D xd x...xd n i.e. if they hve the sme degree nd the sme domins. Kjell Orsorn 3//04
6 Union opertion The union of two union-comptile reltions R nd S is the set of ll tuples tht either occur in R, S, or in oth. Nottion: R» S Defined s: R» S = {t t Œ R or t Œ S} For exmple: R S A B» A B = A B 3 3 Kjell Orsorn 3//04
7 Difference opertion The difference etween two union-comptile sets R nd S is the set of ll tuples tht occur in R ut not in S. Nottion: R - S Defined s: R - S = {t t Œ R nd t œ S} For exmple: R S A B - A B = A B 3 Kjell Orsorn 3//04
8 Intersection The intersection of two union-comptile sets R nd S, is the set of ll tuples tht occur in oth R nd S. Nottion: R «S Defined s: R «S = {t t Œ R nd t Œ S} For exmple: R S A B «A B = A B 3 Kjell Orsorn 3//04
9 Crtesin product Let R nd S e reltions with k nd k rities resp. The crtesin product of R nd S is the set of ll possile k +k tuples where the first k components constitute tuple in R nd the lst k components tuple in S. Nottion: R S Defined s: R S = {t q t Œ R nd q Œ S} Assume tht ttriutes of r(r) nd s(s) re disjoint. (i.e. R «S = ). If ttriutes of r(r) nd s(s) re not disjoint, then renming must e used. X = Kjell Orsorn 3//04
Kjell Orsorn 3//04 0 UU - IT - UDBL Crtesin product exmple A B C D c 6 = A B C D c c 6 6
Selection opertion The selection opertor, s, selects specific set of tuples from reltion ccording to selection condition (or selection predicte) P. Nottion: s p (R) Defined s: s p (R) = {t t Œ R nd P(t) } (i.e. the set of tuples t in R tht fulfills the condition P) Where P is logicl expression (*) consisting of terms connected y: Ÿ (nd), (or), ÿ (not) nd ech term is one of: <ttriute> op <ttriute> or <constnt> where op is one of: =,, >,. <. Exmple: s SALARY>30000 (EMPLOYEE) (*) formul in propositionl clculus Kjell Orsorn 3//04
Kjell Orsorn 3//04 UU - IT - UDBL Selection exmple = A B C D 4 7 7 3 9 R s A=B Ÿ D > (R) = A B C D 4 7 9
3 Projection opertion The projection opertor, P, picks out (or projects) listed columns from reltion nd cretes new reltion consisting of these columns. Nottion: P A,A,...,Ak (R) where A, A re ttriute nmes nd R is reltion nme. The result is new reltion of k columns. Duplicte rows removed from result, since reltions re sets. Exmple: P LNAME,FNAME,SALARY (EMPLOYEE) Kjell Orsorn 3//04
Kjell Orsorn 3//04 4 UU - IT - UDBL Projection exmple = A B 3 4 C R P A,C (R) = A C = A C
Join opertor The join opertor, ƒ (lmost), cretes new reltion y joining relted tuples from two reltions. Nottion: R ƒ C S C is the join condition which hs the form A r q A s, where q is one of {=, <, >,,, }. Severl terms cn e connected s C ŸC Ÿ...C k. A join opertion with this kind of generl join condition is clled Thet join. Kjell Orsorn 3//04
Kjell Orsorn 3//04 6 UU - IT - UDBL Exmple Thet join ƒ A D = A B 6 9 7 7 C B 3 3 3 8 8 7 7 7 7 A B 6 9 7 7 C 3 8 8 B C 7 7 3 3 8 D 4 9 R ƒ A D S C 3 3 8 8 8 D 4 9 9 9 R S
7 Equijoin The sme s join ut it is required tht ttriute A r nd ttriute A s should hve the sme vlue. Nottion: R ƒ C S C is the join condition which hs the form A r = A s. Severl terms cn e connected s C ŸC Ÿ...C k. Kjell Orsorn 3//04
Kjell Orsorn 3//04 8 UU - IT - UDBL Exmple Equijoin ƒ B=C = A B 4 C D 4 d d A B 4 C D 4 9 d d d E e e e R ƒ B=C S E e e R S
9 Nturl join Nturl join is equivlent with the ppliction of join to R nd S with the equlity condition A r = A s (i.e. n equijoin) nd then removing the redundnt column A s in the result. Nottion: R * Ar,As S A r,a s re ttriute pirs tht should fulfil the join condition which hs the form A r = A s. Severl terms cn e connected s C ŸC Ÿ...C k. Kjell Orsorn 3//04
Kjell Orsorn 3//04 0 UU - IT - UDBL Exmple Nturl join ƒ B=C = A B 4 D d d A B 4 C D 4 9 d d d E e e e R * B=C S E e e R S
Kjell Orsorn 3//04 UU - IT - UDBL Composition of opertions Expressions cn e uilt y composing multiple opertions Exmple: s A=C (R S) s A=C (R S) = A B C D c 6 = A B C D c c 6 6 A B C D 6 R S =
Additionl reltionl opertions Assignment nd Renme Division Outer join nd outer union Aggregte functions (presented together with SQL) Updte opertions (presented together with SQL) (not prt of pure query lnguge) Kjell Orsorn 3//04
3 Assignment opertion The ssignment opertion ( ) mkes it possile to ssign the result of n expression to temporry reltion vrile. Exmple: temp s dno = (EMPLOYEE) result fnme,lnme,slry (temp) The result to the right of the is ssigned to the reltion vrile on the left of the. The vrile my use vrile in susequent expressions. Kjell Orsorn 3//04
4 Renming reltions nd ttriute The ssignment opertion cn lso e used to renme reltions nd ttriutes. Exmple: NEWEMP s dno = (EMPLOYEE) R(FIRSTNAME,LASTNAME,SALARY) fnme,lnme,slry (NEWEMP) Kjell Orsorn 3//04
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 = (A,...,A m,b,...,b n ) S = (B,...,B n ) The result of R S is reltion on schem R - S = (A,...,A m ) R S = {t t Œ P R-S (R) Ÿ " uœs (tu ŒR)} Kjell Orsorn 3//04
6 Exmple Division opertion R S R S A c d d d d e e B 3 3 4 6 B = A e Kjell Orsorn 3//04
7 Outer join/union opertion Extensions of the join/union opertions tht void loss of informtion. Computes the join/union nd then dds tuples from one reltion tht do not mtch tuples in the other reltion to the result of the join. Fills out with null vlues: null signifies tht the vlue is unknown or does not exist. All comprisons involving null re flse y definition. Kjell Orsorn 3//04
8 Reltion lon Exmple Outer join rnch-nme Downtown Redwood Perryridge Reltion orrower customer-nme Jones Smith Hyes lon-numer -70 L-30 L-60 lon-numer -70 L-30 L- mount 3000 4000 700 Kjell Orsorn 3//04
9 Exmple Outer join cont... lon * orrower (nturl join) rnch-nme lon-numer mount customer-nme Downtown Redwood -70 L-30 3000 4000 Jones Smith lon ƒ left orrower (left outer join) rnch-nme lon-numer mount customer-nme lon-numer Downtown Redwood Perryridge -70 L-30 L-60 3000 4000 700 Jones Smith null -70 L-30 null Kjell Orsorn 3//04
30 Exmple Outer join cont... lon ƒ right orrower (nturl right outer join) rnch-nme lon-numer mount customer-nme Downtown Redwood null L-70 L-30 L- 3000 4000 null Jones Smith Hyes lon ƒ full orrower (nturl full outer join) rnch-nme lon-numer mount customer-nme Downtown Redwood Perryridge null L-70 L-30 L-60 L- 3000 4000 700 null Jones Smith null Hyes Kjell Orsorn 3//04
3 Aggregtion opertions Presented together with SQL lter Exmples of ggregtion opertions vg min mx sum count Kjell Orsorn 3//04
3 Updte opertions Presented together with SQL lter Opertions for dtse updtes re normlly prt of the DML insert (of new tuples) updte (of ttriute vlues) delete (of tuples) Cn e expressed y mens of the ssignment opertor Kjell Orsorn 3//04
33 Exmple DB schem In the following exmple we will use dtse with the following reltion schems: emps(enme, slry, dept) depts(dnme, dept#, mgr) suppliers(snme, ddr) items(inme, item#, dept) orders(o#, dte, cust) customers(cnme, ddr, lnce) supplies(snme, inme, price) includes(o#, item, quntity) Kjell Orsorn 3//04
34 Reltion lger s query lnguge Reltionl schem: supplies(snme, inme, price) Wht is the nmes of the suppliers tht supply cheese? p snme (s inme='cheese' (SUPPLIES)) Wht is the nme nd price of the items tht cost less thn $ nd tht re supplied y WALMART p inme,price (s snme='walmart' Ÿ price < (SUPPLIES)) Kjell Orsorn 3//04