DATABASDESIGN FÖR INGENJÖRER - 06F Sommr 00 En introuktionskurs i tssystem http://user.it.uu.se/~ul/t-sommr0/ lt. http://www.it.uu.se/eu/course/homepge/esign/st0/ Kjell Orsorn (Rusln Fomkin) Uppsl Dtse Lortory Deprtment of Informtion Technology, Uppsl University, Uppsl, Sween Kjell Orsorn 00-06-
Introuction to Reltionl Alger Elmsri/Nvthe ch 6 Kjell Orsorn & Rusln Fomkin Deprtment of Informtion Technology Uppsl University, Uppsl, Sween Kjell Orsorn 00-06-
Query lnguges Lnguges where users cn express wht informtion to retrieve from the tse. tegories of query lnguges: Proceurl Non-proceurl (eclrtive) Forml ( pure ) lnguges: Reltionl lger Reltionl clculus Tuple-reltionl clculus Domin-reltionl clculus Forml lnguges form unerlying sis of query lnguges tht people use. Kjell Orsorn 00-06-
Reltionl lger Reltionl lger is proceurl lngue Opertions in reltionl lger tkes one or two reltions s rguments n return new reltion. Reltionl lgeric opertions: Opertions from set theory: Union, Intersection, Difference, rtesin prouct Opertions specificlly introuce for the reltionl t moel: Select, Project, Join It hve een shown tht the select, project, union, ifference, n crtesin prouct opertions form complete set. Tht is ny other reltionl lger opertion cn e expresse in these. Kjell Orsorn 00-06-
Opertions from set theory Reltions re require to e union comptile to e le to tke prt in the union, intersection n ifference opertions. Two reltions R n R is si to e union-comptile if: R D x D x... x D n n R D x D x... x D n i.e. if they hve the sme egree n the sme omins. Kjell Orsorn 00-06-
6 Union opertion The union of two union-comptile reltions R n S is the set of ll tuples tht either occur in R, S, or in oth. Nottion: R S Define s: R S = {t t R or t S} For exmple: R S A B A B = A B Kjell Orsorn 00-06-
Difference opertion The ifference etween two union-comptile sets R n S is the set of ll tuples tht occur in R ut not in S. Nottion: R S Define s: R S = {t t R n t S} For exmple: R S A B A B = A B Kjell Orsorn 00-06-
8 Intersection The intersection of two union-comptile sets R n S, is the set of ll tuples tht occur in oth R n S. Nottion: R S Define s: R S = {t t R n t S} For exmple: R S A B A B = A B Kjell Orsorn 00-06-
9 rtesin prouct Let R n S e reltions with k n k rities resp. The crtesin prouct of R n S is the set of ll possile k +k tuples where the first k components constitute tuple in R n the lst k components tuple in S. Nottion: R x S Define s: R x S = {t q t R n q S} Assume tht ttriutes of r(r) n s(s) re isjoint (i.e. R S = ). If ttriutes of r(r) n s(s) re not isjoint, then renming must e use. X = Kjell Orsorn 00-06-
Kjell Orsorn 00-06- 0 UU - IT - UDBL rtesin prouct exmple A B x D c 6 = A B D c c 6 6
Selection opertion The selection opertor, σ, selects specific set of tuples from reltion ccoring to selection conition (or selection preicte) P. Nottion: σ p (R) Define s: σ p (R) = {t t R AND P(t) } (i.e. the set of tuples t in R tht fulfills the conition P) Where P is logicl expression (*) consisting of terms connecte y: (n), (or), (not) n ech term is one of: <ttriute> op <ttriute> or <constnt> where op is one from the set {=, <,,, >, } Exmple: σ SALARY>0000 (EMPLOYEE) (*) formul in propositionl clculus Kjell Orsorn 00-06-
Kjell Orsorn 00-06- UU - IT - UDBL Selection exmple = A B D 9 R σ A=B, D> (R) = A B D 9
Projection opertion The projection opertor, π, picks out (or projects) liste columns from reltion n cretes new reltion consisting of these columns. Nottion: π A,A,...,A k (R) where A, A re ttriute nmes n R is reltion nme. The result is new reltion of k columns. Duplicte rows remove from result, since reltions re sets. Exmple: π LNAME,FNAME,SALARY (EMPLOYEE) Kjell Orsorn 00-06-
Projection exmple R = A B π A, (R) = A Kjell Orsorn 00-06-
Join opertor The join opertor cretes new reltion y joining relte tuples from two reltions. Nottion: R S is the join conition which hs the form A r Θ A s, where Θ is one of {=, <, >,,, }. Severl terms cn e connecte s... k. A join opertion with this kin of generl join conition is clle Thet join. Kjell Orsorn 00-06-
Kjell Orsorn 00-06- 6 UU - IT - UDBL Exmple Thet join A D = A B 6 9 B 8 8 A B 6 9 8 8 B 8 D 9 R A D S 8 8 8 D 9 9 9 R S
Equijoin The sme s join ut it is require tht ttriute A r n ttriute A s shoul hve the sme vlue. Nottion: R S is the join conition which hs the form A r = A s. Severl terms cn e connecte s... k. Kjell Orsorn 00-06-
Kjell Orsorn 00-06- 8 UU - IT - UDBL Exmple Equijoin B= = A B D A B D 9 E e e e R B= S E e e R S
9 Nturl join Nturl join is equivlent with the ppliction of join to R n S with the equlity conition A r = A s (i.e. n equijoin) n then removing the reunnt column A s in the result. Nottion: R * Ar,As S A r,a s re ttriute pirs tht shoul fulfil the join conition which hs the form A r = A s. Severl terms cn e connecte s... k. Kjell Orsorn 00-06-
Kjell Orsorn 00-06- 0 UU - IT - UDBL Exmple Nturl join B, = A B D A B D 9 E e e e R* B, S E e e R S
Kjell Orsorn 00-06- UU - IT - UDBL omposition of opertions Expressions cn e uilt y composing multiple opertions Exmple: σ A= (R x S) σ A= (R x S) = A B x D c 6 = A B D c c 6 6 A B D 6 R x S =
Assignment opertion The ssignment opertion (P) mkes it possile to ssign the result of n expression to temporry reltion vrile. Exmple: temp P σ no = (EMPLOYEE) result P π fnme,lnme,slry (temp) The result to the right of the P is ssigne to the reltion vrile on the left of the P. The vrile my use vrile in susequent expressions. Kjell Orsorn 00-06-
Renming reltions n ttriute The ssignment opertion cn lso e use to renme reltions n ttriutes. Exmple: NEWEMP P σ no = (EMPLOYEE) ρ (FIRSTNAME,LASTNAME,SALARY) π fnme,lnme,slry (NEWEMP) Kjell Orsorn 00-06-
Division opertion Suite to queries tht inclue the phrse for ll. Let R n S e reltions on schems R n 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 π R-S (R) u S tu R} Kjell Orsorn 00-06-
Exmple Division opertion R S R S A c e e B 6 B = A e Kjell Orsorn 00-06-
6 Reltion lger s query lnguge Reltionl schem: supplies(snme, inme, price) Wht is the nmes of the suppliers tht supply cheese? π snme (σ inme='heese' (SUPPLIES)) Wht is the nme n price of the items tht cost less thn $ n tht re supplie y WALMART π inme,price (σ snme='walmart' price < (SUPPLIES)) Kjell Orsorn 00-06-