RELATIONAL MODEL Structure of Reltionl Dtbses Reltionl Algebr Tuple Reltionl Clculus Domin Reltionl Clculus Extended Reltionl-Algebr- Opertions Modifiction of the Dtbse Views
EXAMPLE OF A RELATION
BASIC STRUCTURE Formlly, given sets D, D,. D n reltion r is subset of D x D x x D n Thus reltion is set of n-tuples (,,, n ) where ech i D i Exmple: if customer-nme = {Jones, Smith, Curry, Lindsy} customer-street = {Min, North, Prk} customer-city = {Hrrison, Rye, Pittsfield} Then r = { (Jones, Min, Hrrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsy, Prk, Pittsfield)} is reltion over customer-nme x customer-street x customer-city
ATTRIBUTE TYPES Ech ttribute of reltion hs nme The set of llowed vlues for ech ttribute is clled the domin of the ttribute Attribute vlues re (normlly) required to be tomic, tht is, indivisible E.g. multivlued ttribute vlues re not tomic E.g. composite ttribute vlues re not tomic The specil vlue null is member of every domin The null vlue cuses complictions in the definition of mny opertions we shll ignore the effect of null vlues in our min presenttion nd consider their effect lter
RELATION SCHEMA A, A,, A n re ttributes R = (A, A,, A n ) is reltion schem E.g. Customer-schem = (customer-nme, customer-street, customer-city) r(r) is reltion on the reltion schem R E.g. customer (Customer-schem)
RELATION INSTANCE The current vlues (reltion instnce) of reltion re specified by tble An element t of r is tuple, represented by row in tble ttributes (or columns) customer-nme customer-street customer-city Jones Smith Curry Lindsy Min North North Prk Hrrison Rye Rye Pittsfield tuples (or rows) customer
RELATIONS ARE UNORDERED Order of tuples is irrelevnt (tuples my be stored in n rbitrry order) E.g. ccount reltion with unordered tuples
DATABASE A dtbse consists of multiple reltions Informtion bout n enterprise is broken up into prts, with ech reltion storing one prt of the informtion E.g.: ccount : stores informtion bout ccounts depositor : stores informtion bout which customer owns which ccount customer : stores informtion bout customers Storing ll informtion s single reltion such s bnk(ccount-number, blnce, customer-nme,..) results in repetition of informtion (e.g. two customers own n ccount) the need for null vlues (e.g. represent customer without n ccount) Normliztion theory dels with how to design reltionl schems
THE CUSTOMER RELATION
THE DEPOSITOR RELATION
E-R DIAGRAM FOR THE BANKING ENTERPRISE
SCHEMA DIAGRAM FOR THE BANKING ENTERPRISE
QUERY LANGUAGES Lnguge in which user requests informtion from the dtbse. Ctegories of lnguges procedurl non-procedurl Pure lnguges: Reltionl Algebr Tuple Reltionl Clculus Domin Reltionl Clculus Pure lnguges form underlying bsis of query lnguges tht people use.
RELATIONAL ALGEBRA Procedurl lnguge Six bsic opertors select project union set difference Crtesin product renme The opertors tke one or more reltions s inputs nd give new reltion s result.
SELECT OPERATION EXAMPLE Reltion r A B C D 7 5 7 3 3 0 A=B ^ D > 5 (r) A B C D 7 3 0
SELECT OPERATION 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 by : (nd), (or), (not) Ech term is one of: <ttribute> op <ttribute> or <constnt> where op is one of: =,, >,. <. Exmple of selection: brnch-nme= Perryridge (ccount)
PROJECT OPERATION EXAMPLE Reltion r: A B C 0 0 30 40 A,C (r) A C A C =
PROJECT OPERATION Nottion: A, A,, Ak (r) where A, A re ttribute nmes nd r is reltion nme. The result is defined s the reltion of k columns obtined by ersing the columns tht re not listed Duplicte rows removed from result, since reltions re sets E.g. To eliminte the brnch-nme ttribute of ccount ccount-number, blnce (ccount)
UNION OPERATION EXAMPLE Reltions r, s: A B A B 3 r s r s: A B 3
UNION OPERATION Nottion: r s Defined s: r s = {t t r or t s} For r s to be vlid.. r, s must hve the sme rity (sme number of ttributes). The ttribute domins must be comptible (e.g., nd column of r dels with the sme type of vlues s does the nd column of s)
SET DIFFERENCE OPERATION EXAMPLE Reltions r, s: A B A B 3 r s r s: A B
SET DIFFERENCE OPERATION Nottion r s Defined s: r s = {t t r nd t s} Set differences must be tken between comptible reltions. r nd s must hve the sme rity ttribute domins of r nd s must be comptible
CARTESIAN-PRODUCT OPERATION-EXAMPLE Reltions r, s: r x s: A B A B C D 0 0 0 0 0 0 0 0 E b b b b C D 0 0 0 0 E b b r s
CARTESIAN-PRODUCT OPERATION Nottion r x s Defined s: r x s = {t q t r nd q s} Assume tht ttributes of r(r) nd s(s) re disjoint. (Tht is, R S = ). If ttributes of r(r) nd s(s) re not disjoint, then renming must be used.
COMPOSITION OF OPERATIONS Cn build expressions using multiple opertions Exmple: A=C (r x s) r x s A B C D 0 0 0 0 0 0 0 0 E b b b b A B C D E A=C (r x s) 0 0 0 b
RENAME OPERATION Allows us to nme, nd therefore to refer to, the results of reltionl-lgebr expressions. Allows us to refer to reltion by more thn one nme. Exmple: x (E) returns the expression E under the nme X If reltionl-lgebr expression E hs rity n, then x (A, A,, An) (E) returns the result of expression E under the nme X, nd with the ttributes renmed to A, A,., An.
BANKING EXAMPLE brnch (brnch-nme, brnch-city, ssets) customer (customer-nme, customer-street, customer-only) ccount (ccount-number, brnch-nme, blnce) lon (lon-number, brnch-nme, mount) depositor (customer-nme, ccountnumber) borrower (customer-nme, lon-number)
EXAMPLE QUERIES Find ll lons of over $00 mount > 00 (lon) Find the lon number for ech lon of n mount greter thn $00 lon-number ( mount > 00 (lon))
EXAMPLE QUERIES Find the nmes of ll customers who hve lon, n ccount, or both, from the bnk customer-nme (borrower) customer-nme (depositor) Find the nmes of ll customers who hve lon nd n ccount t bnk. customer-nme (borrower) customer-nme (depositor)
EXAMPLE QUERIES Find the nmes of ll customers who hve lon t the Perryridge brnch. customer-nme ( brnch-nme= Perryridge ( borrower.lon-number = lon.lon-number (borrower x lon))) Find the nmes of ll customers who hve lon t the Perryridge brnch but do not hve n ccount t ny brnch of the bnk. customer-nme ( brnch-nme = Perryridge ( borrower.lon-number = lon.lon-number (borrower x lon))) customer-nme (depositor)
EXAMPLE QUERIES Find the nmes of ll customers who hve lon t the Perryridge brnch. Query customer-nme ( brnch-nme = Perryridge ( borrower.lon-number = lon.lon-number (borrower x lon))) Query customer-nme ( lon.lon-number = borrower.lon-number ( ( brnch-nme = Perryridge (lon)) x borrower))
EXAMPLE QUERIES Find the lrgest ccount blnce Renme ccount reltion s d The query is: blnce (ccount) - ccount.blnce ( ccount.blnce < d.blnce (ccount x d (ccount)))
FORMAL DEFINITION A bsic expression in the reltionl lgebr consists of either one of the following: A reltion in the dtbse A constnt reltion Let E nd E be reltionl-lgebr expressions; the following re ll reltionl-lgebr expressions: E E E - E E x E p (E ), P is predicte on ttributes in E s (E ), S is list consisting of some of the ttributes in E x (E ), x is the new nme for the result of E
ADDITIONAL OPERATIONS We define dditionl opertions tht do not dd ny power to the reltionl lgebr, but tht simplify common queries. Set intersection Nturl join Division Assignment
SET-INTERSECTION OPERATION Nottion: r s Defined s: r s ={ t t r nd t s } Assume: r, s hve the sme rity ttributes of r nd s re comptible Note: r s = r - (r - s)
SET-INTERSECTION OPERATION - Reltion r, s: A B EXAMPLE A B 3 r s A B r s
NATURAL-JOIN OPERATION Nottion: r s Let r nd s be reltions on schems R nd S respectively. Then, r s is reltion on schem R S obtined 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 ttributes in R S, dd tuple t to the result, where t hs the sme vlue s t r on r t hs the sme vlue s t s on s Exmple: R = (A, B, C, D) S = (E, B, D) Result schem = (A, B, C, D, E) r s is defined s:
NATURAL JOIN OPERATION EXAMPLE Reltions r, s: A B 4 C D b b B 3 3 D b b E r A B C D b E s r s
DIVISION OPERATION r s Suited to queries tht include the phrse for ll. Let r nd s be 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 R-S (r) u s ( tu r ) }
DIVISION OPERATION EXAMPLE Reltions r, s: A B B r s: A r 3 3 4 6 s
ANOTHER DIVISION EXAMPLE A B C D b b b b E 3 Reltions r, s: r s: D b E A B C r s
DIVISION OPERATION (CONT.) Property Let q r s Then q is the lrgest reltion stisfying q x s r Definition in terms of the bsic lgebr opertion Let r(r) nd s(s) be 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 ttributes 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.
ASSIGNMENT OPERATION The ssignment opertion () provides convenient wy to express complex queries. Write query s sequentil progrm consisting of series of ssignments followed by n expression whose vlue is displyed s result of the query. Assignment must lwys be mde to temporry reltion vrible. 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 vrible on the left of the. My use vrible in subsequent expressions.
EXAMPLE QUERIES Find ll customers who hve n ccount from t lest the Downtown nd the Uptown brnches. Query CN ( BN= Downtown (depositor ccount)) CN ( BN= Uptown (depositor ccount)) where CN denotes customer-nme nd BN denotes brnch-nme. Query customer-nme, brnch-nme (depositor ccount) temp(brnch-nme) ({( Downtown ), ( Uptown )})
EXAMPLE QUERIES Find ll customers who hve n ccount t ll brnches locted in Brooklyn city. customer-nme, brnch-nme (depositor ccount) brnch-nme ( brnch-city = Brooklyn (brnch))
VIEWS In some cses, it is not desirble for ll users to see the entire logicl model (i.e., ll the ctul reltions stored in the dtbse.) Consider person who needs to know customer s lon number but hs no need to see the lon mount. This person should see reltion described, in the reltionl lgebr, by customer-nme, lon-number (borrower lon) Any reltion tht is not of the conceptul model but is mde visible to user s virtul reltion is clled view.
VIEW DEFINITION A view is defined using the crete view sttement which hs the form crete view v s <query expression where <query expression> is ny legl reltionl lgebr query expression. The view nme is represented by v. Once view is defined, the view nme cn be used to refer to the virtul reltion tht the view genertes. View definition is not the sme s creting new reltion by evluting the query expression Rther, view definition cuses the sving of n expression;
VIEW EXAMPLES Consider the view (nmed ll-customer) consisting of brnches nd their customers. crete view ll-customer s brnch-nme, customer-nme (depositor ccount) brnch-nme, customer-nme (borrower lon) We cn find ll customers of the Perryridge brnch by writing: customer-nme ( brnch-nme = Perryridge (ll-customer))
UPDATES THROUGH VIEW Dtbse modifictions expressed s views must be trnslted to modifictions of the ctul reltions in the dtbse. Consider the person who needs to see ll lon dt in the lon reltion except mount. The view given to the person, brnch-lon, is defined s: crete view brnch-lon s brnch-nme, lon-number (lon) Since we llow view nme to pper wherever reltion nme is llowed, the person my write: brnch-lon brnch-lon {( Perryridge, L-
TUPLE RELATIONAL CALCULUS A nonprocedurl query lnguge, where ech query is of the form {t P (t) } It is the set of ll tuples t such tht predicte P is true for t t is tuple vrible, t[a] denotes the vlue of tuple t on ttribute A t r denotes tht tuple t is in reltion r P is formul similr to tht of the predicte clculus
PREDICATE CALCULUS FORMULA. Set of ttributes nd constnts. Set of comprison opertors: (e.g.,,,,,, ) 3. Set of connectives: nd (), or (v) not () 4. Impliction (): x y, if x if true, then y is true x y x v y 5. Set of quntifiers: t r (Q(t)) there exists tuple in t in reltion r such tht predicte Q(t) is true t r (Q(t)) Q is true for ll tuples t in reltion r
BANKING EXAMPLE brnch (brnch-nme, brnch-city, ssets) customer (customer-nme, customer-street, customer-city) ccount (ccount-number, brnch-nme, blnce) lon (lon-number, brnch-nme, mount) depositor (customer-nme, ccount-number) borrower (customer-nme, lon-number)
EXAMPLE QUERIES Find the lon-number, brnch-nme, nd mount for lons of over $00 {t t lon t [mount] 00} Find the lon number for ech lon of n mount greter thn $00 {t s lon (t[lon-number] = s[lon-number] s [mount] 00)} Notice tht reltion on schem [lon-number] is implicitly defined by the query