# Chapter 6

The Relational Algebra and Calculus

Chapter Outline

Relational Algebra
    

Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra Tuple Relational Cal ulus !omain Relational Cal ulus

Relational Cal ulus
 

➲ ➲

Example !atabase Appli ation "CO#\$A%&' O(er(ie) of the QBE language "appendix !'

Relational Algebra Overview

Relational algebra is the basi set of operations for the relational model
These operations enable a user to spe ify basic retrieval requests "or queries'

The result of an operation is a new relation* )hi h may ha(e been formed from one or more input relations

This property ma+es the algebra , losed- "all ob.e ts in relational algebra are relations'

Relational Algebra Overview (continued) ➲ The algebra operations thus produ e ne) relations  These an be further manipulated using operations of the same algebra ➲ A se/uen e of relational algebra operations forms a relational algebra expression  The result of a relational algebra expression is also a relation that represents the result of a database /uery "or retrie(al re/uest' .

Relational Algebra Overview ➲ Relational Algebra onsists of se(eral groups of operations  Unary Relational Operations    SE0ECT "symbol1 σ "sigma'' \$RO2ECT "symbol1 π "pi'' RE%A#E "symbol1 ρ "rho''  Relational Algebra Operations From Set Theory   U%3O% " ∪ '* 3%TERSECT3O% " ∩ '* !3FFERE%CE "or #3%US* – ' CARTES3A% \$RO!UCT " x ' 2O3% "se(eral (ariations of 2O3% exist' !343S3O% OUTER 2O3%S* OUTER U%3O% A55RE5ATE FU%CT3O%S "These ompute summary of information1 for example* SU#* COU%T* A45* #3%* #A6'  Binary Relational Operations    Additional Relational Operations   .

Database State for COMPA ! ➲ All examples dis ussed belo) refer to the CO#\$A%& database sho)n here7 .

<=*===1 !%O : 9 σ σ SA0AR& > <=*=== "E#\$0O&EE' ."nar# Relational Operations\$ S%&%C' ➲ The SE0ECT operation "denoted by σ "sigma'' is used to sele t a subset of the tuples from a relation based on a selection condition7    The sele tion ondition a ts as a filter 8eeps only those tuples that satisfy the /ualifying ondition Tuples satisfying the ondition are selected )hereas the other tuples are dis arded "filtered out' ➲ Examples1  Sele t the E#\$0O&EE tuples )hose department number is 91  "E#\$0O&EE' Sele t the employee tuples )hose salary is greater than .

"nar# Relational Operations\$ S%&%C'  3n general* the select operation is denoted by σ ?sele tion ondition>"R' )here    the symbol σ "sigma' is used to denote the select operator the sele tion ondition is a Boolean " onditional' expression spe ified on the attributes of relation R tuples that ma+e the ondition true are sele ted  appear in the result of the operation dis arded from the result of the operation  tuples that ma+e the ondition false are filtered out  .

"nar# Relational Operations\$ S%&%C' (contd() ➲ SE0ECT Operation \$roperties     The SE0ECT operation σ ?sele tion ondition>"R' produ es a relation S that has the same s hema "same attributes' as R SE0ECT σ is ommutati(e1  σ ? ondition@>"σ ? onditionA> "R'' : σ ? onditionA> "σ ? ondition@> "R'' Be ause of ommutati(ity property* a as ade "se/uen e' of SE0ECT operations may be applied in any order1  σ? ond@>"σ? ondA> "σ? ond<> "R'' : σ? ondA> "σ? ond<> "σ? ond@> " R''' A as ade of SE0ECT operations may be repla ed by a single sele tion )ith a on.un tion of all the onditions1  σ? ond@>"σ? ondA> "σ? ond<>"R'' : σ ? ond@> A%! ? ondA> A%! ? ond<>"R'''  The number of tuples in the result of a SE0ECT is less than "or e/ual to' the number of tuples in the input relation R .

'he following )uer# results refer to this database state .

"nar# Relational Operations\$ PRO*%C' ➲ ➲ \$RO2ECT Operation is denoted by π "pi' This operation +eeps ertain columns "attributes' from a relation and dis ards the other olumns7  \$RO2ECT reates a (erti al partitioning   The list of spe ified olumns "attributes' is +ept in ea h tuple The other attributes in ea h tuple are dis arded ➲ Example1 To list ea h employeeBs first and last name and salary* the follo)ing is used1 π0%A#E* F%A#E*SA0AR&"E#\$0O&EE' .

e t operation removes any duplicate tuples  #athemati al sets do not allow dupli ate elements7 ."nar# Relational Operations\$ PRO*%C' (cont() ➲ The general form of the project operation is1 π?attribute list>"R'   π "pi' is the symbol used to represent the project operation ?attribute list> is the desired list of attributes from relation R7 This is be ause the result of the project operation must be a set of tuples  ➲ The pro.

"nar# Relational Operations\$ PRO*%C' (contd() ➲ \$RO2ECT Operation \$roperties  The number of tuples in the result of pro.e tion π?list>"R' is al)ays less or e/ual to the number of tuples in R  3f the list of attributes in ludes a key of R* then the number of tuples in the result of \$RO2ECT is equal to the number of tuples in R π ?list@> "π ?listA> "R' ' : π ?list@> "R' as long as ?listA> ontains the attributes in ?list@>  \$RO2ECT is not ommutati(e  .

ples of appl#ing S%&%C' and PRO*%C' operations .%+a.

Relational Algebra %+pressions ➲ Ce may )ant to apply se(eral relational algebra operations one after the other   Either )e an )rite the operations as a single relational algebra expression by nesting the operations* or Ce an apply one operation at a time and reate intermediate result relations7 ➲ 3n the latter ase* )e must gi(e names to the relations that hold the intermediate results7 .

ple) To retrie(e the first name* last name* and salary of all employees )ho )or+ in department number D* )e must apply a sele t and a pro.e t operation ➲ Ce an )rite a single relational algebra expression as follo)s1  π F%A#E* 0%A#E* SA0AR&"σ !%O:D"E#\$0O&EE'' ➲ ➲ OR Ce an expli itly sho) the sequence of operations* gi(ing a name to ea h intermediate relation1  !E\$DEE#\$S ← σ !%O:D"E#\$0O&EE'  RESU0T ← π F%A#E* 0%A#E* SA0AR& "!E\$DEE#\$S' .Single e+pression versus se)uence of relational operations (%+a.

"nar# Relational Operations\$ R% AM% The RE%A#E operator is denoted by ρ "rho' ➲ 3n some ases* )e may )ant to rename the attributes of a relation or the relation name or both  Useful )hen a /uery re/uires multiple operations  %e essary in some ases "see 2O3% operation later' ➲ .

"nar# Relational Operations\$ R% AM% (contd() ➲ The general RE%A#E operation ρ an be expressed by any of the follo)ing forms1  ρS "B@* BA* F* Bn '"R' hanges both1   the relation name to S* and the olumn "attribute' names to B@* B@* F77Bn the relation name only to S the column (attribute) names only to B@* B@* F77Bn  ρS"R' hanges1   ρ"B@* BA* F* Bn '"R' hanges1  .

"nar# Relational Operations\$ R% AM% (contd() ➲ For on(enien e* )e also use a shorthand for renaming attributes in an intermediate relation1  3f )e )rite1 G RESU0T ← π F%A#E* 0%A#E* SA0AR& "!E\$DEE#\$S' G RESU0T )ill ha(e the same attribute names as !E\$DEE#\$S "same attributes as E#\$0O&EE' G 3f )e )rite1 G G RESU0T "F* #* 0* S* B* A* S6* SA0* SU* !%O'← ρ RESU0T "F7#707S7B*A*S6*SA0*SU* !%O'"!E\$DEE#\$S' The @= attributes of !E\$DEE#\$S are renamed to F* #* 0* S* B* A* S6* SA0* SU* !%O* respe ti(ely .

%+a.ple of appl#ing .ultiple operations and R% AM% .

Relational Algebra Operations fro. Set 'heor#\$ " -O ➲ U%3O% Operation     Binary operation* denoted by ∪ The result of R ∪ S* is a relation that in ludes all tuples that are either in R or in S or in both R and S !upli ate tuples are eliminated The t)o operand relations R and S must be ."or U%3O% ompatible'   R and S must ha(e same number of attributes Ea h pair of orresponding attributes must be type ompatible "ha(e same or ompatible domains' .type ompatible.

Relational Algebra Operations fro. Set 'heor#\$ " -O ➲ Example1   To retrie(e the so ial se urity numbers of all employees )ho either work in department 5 "RESU0T@ belo)' or directly supervise an employee who works in department 5 "RESU0TA belo)' Ce an use the U%3O% operation as follo)s1  The union operation produ es the tuples that are in either RESU0T@ or RESU0TA or both !E\$DEE#\$S ← σ!%O:D "E#\$0O&EE' RESU0T@ ← π SS%"!E\$DEE#\$S' RESU0TA"SS%' ← πSU\$ERSS%"!E\$DEE#\$S' RESU0T ← RESU0T@ ∪ RESU0TA .

%+a.ple of the result of a " -O ➲ operation U%3O% Example .

Relational Algebra Operations fro. Set 'heor# ➲ ➲ Type Compatibility of operands is re/uired for the binary set operation U%3O% ∪* "also for 3%TERSECT3O% ∩* and SET !3FFERE%CE H* see next slides' R@"A@* AA* 777* An' and RA"B@* BA* 777* Bn' are type ompatible if1   they ha(e the same number of attributes* and the domains of orresponding attributes are type ompatible "i7e7 dom"Ai':dom"Bi' for i:@* A* 777* n'7 ➲ The resulting relation for R@∪RA "also for R@∩RA* or R@HRA* see next slides' has the same attribute names as the first operand relation R@ "by on(ention' .

'%RS%C'-O ➲ ➲ 3%TERSECT3O% is denoted by ∩ The result of the operation R ∩ S* is a relation that in ludes all tuples that are in both R and S  The attribute names in the result )ill be the same as the attribute names in R ➲ The t)o operand relations R and S must be .Relational Algebra Operations fro.type ompatible- . Set 'heor#\$ .

Relational Algebra Operations fro.. Set 'heor#\$ S%' D-.type ompatible- .%R% C% (cont() SET !3FFERE%CE "also alled #3%US or E6CE\$T' is denoted by H ➲ The result of R H S* is a relation that in ludes all tuples that are in R but not in S  The attribute names in the result )ill be the same as the attribute names in R ➲ ➲ The t)o operand relations R and S must be .

'%RS%C'/ and D-.%R% C% .ple to illustrate the result of " -O / ..%+a.

'%RS%C'/ and D-.e properties of " -O / ..%R% C% ➲ %oti e that both union and interse tion are commutative operationsI that is  R ∪ S : S ∪ R* and R ∩ S : S ∩ R ➲ Both union and interse tion an be treated as nJary operations appli able to any number of relations as both are associative operationsI that is   R ∪ "S ∪ T' : "R ∪ S' ∪ T "R ∩ S' ∩ T : R ∩ "S ∩ T' ➲ The minus operation is not ommutati(eI that is* in general  RHSKSHR .So.

Set 'heor#\$ CAR'%S-A PROD"C' ➲ CARTES3A% "or CROSS' \$RO!UCT Operation    This operation is used to ombine tuples from t)o relations in a ombinatorial fashion7 !enoted by R"A@* AA* 7 7 7* An' x S"B@* BA* 7 7 7* Bm' Result is a relation Q )ith degree n L m attributes1  Q"A@* AA* 7 7 7* An* B@* BA* 7 7 7* Bm'* in that order7   The resulting relation state has one tuple for ea h ombination of tuplesMone from R and one from S7 Nen e* if R has nR tuples "denoted as ORO : nR '* and S has nS tuples* then R x S )ill ha(e nR P nS tuples7 The t)o operands do %OT ha(e to be Qtype ompatible-  .Relational Algebra Operations fro.

Set 'heor#\$ CAR'%S-A PROD"C' (cont() ➲ 5enerally* CROSS \$RO!UCT is not a meaningful operation  Can be ome meaningful )hen follo)ed by other operations FE#A0EEE#\$S ← σ SE6:BFB"E#\$0O&EE' E#\$%A#ES ← π F%A#E* 0%A#E* SS% "FE#A0EEE#\$S' E#\$E!E\$E%!E%TS ← E#\$%A#ES x !E\$E%!E%T ➲ Example "not meaningful'1    ➲ E#\$E!E\$E%!E%TS )ill ontain e(ery ombination of E#\$%A#ES and !E\$E%!E%T  )hether or not they are a tually related .Relational Algebra Operations fro.

Relational Algebra Operations fro. Set 'heor#\$ CAR'%S-A PROD"C' (cont() ➲ ➲ To +eep only ombinations )here the !E\$E%!E%T is related to the E#\$0O&EE* )e add a SE0ECT operation as follo)s Example "meaningful'1      FE#A0EEE#\$S ← σ SE6:BFB"E#\$0O&EE' E#\$%A#ES ← π F%A#E* 0%A#E* SS% "FE#A0EEE#\$S' E#\$E!E\$E%!E%TS ← E#\$%A#ES x !E\$E%!E%T ACTUA0E!E\$S ← σ SS%:ESS%"E#\$E!E\$E%!E%TS' RESU0T ← π F%A#E* 0%A#E* !E\$E%!E%TE%A#E "ACTUA0E!E\$S' ➲ RESU0T )ill no) ontain the name of female employees and their dependents .

%+a.ple of appl#ing CAR'%S-A PROD"C' .

0inar# Relational Operations\$ *O➲ 2O3% Operation "denoted by  '     The se/uen e of CARTES3A% \$RO!ECT follo)ed by SE0ECT is used /uite ommonly to identify and sele t related tuples from t)o relations A spe ial operation* alled 2O3% ombines this se/uen e into a single operation This operation is (ery important for any relational database )ith more than a single relation* be ause it allo)s us combine related tuples from (arious relations The general form of a .oin operation on t)o relations R"A@* AA* 7 7 7* An' and S"B@* BA* 7 7 7* Bm' is1 R ?.oin ondition>S )here R and S an be any relations that result from general relational algebra expressions 7 .

oin ondition   Combines ea h department re ord )ith the employee )ho manages the department The .0inar# Relational Operations\$ *O➲ (cont() Example1 Suppose that )e )ant to retrie(e the name of the manager of ea h department7    To get the managerBs name* )e need to ombine ea h !E\$ART#E%T tuple )ith the E#\$0O&EE tuple )hose SS% (alue mat hes the #5RSS% (alue in the department tuple7 Ce do this by using the .oin ondition an also be spe ified as !E\$ART#E%T7#5RSS%: E#\$0O&EE7SS% .oin operation7 !E\$TE#5R ← !E\$ART#E%T #5RSS%:SS% E#\$0O&EE ➲ #5RSS%:SS% is the .

ple of appl#ing the *O- operation !E\$TE#5R ← !E\$ART#E%T #5RSS%:SS% E#\$0O&EE .%+a.

oin ondition' )ill appear in the result  .  Result is a relation Q )ith degree n L m attributes1  Q"A@* AA* 7 7 7* An* B@* BA* 7 7 7* Bm'* in that order7   The resulting relation state has one tuple for ea h ombination of tuplesMr from R and s from S* but only if they satisfy the join condition rRAiS:sRB.oin result )ill generally ha(e less than nR P nS tuples7 Only related tuples "based on the .S Nen e* if R has nR tuples* and S has nS tuples* then the .So.e properties of *O➲ Consider the follo)ing 2O3% operation1  R"A@* AA* 7 7 7* An' S"B@* BA* 7 7 7* Bm' R7Ai:S7B.

A%!-ed togetherI for example1  R7Ai:S7B.e properties of *O➲ The general ase of 2O3% operation is alled a ThetaJ.oin ondition is alled theta Theta an be any general boolean expression on the attributes of R and SI for example1  R7Ai?S7B.oin1 R S theta ➲ ➲ The . A%! "R7A+:S7Bl OR R7Ap?S7B/' ➲ #ost .oin onditions in(ol(e one or more e/uality onditions . A%! R7A+:S7Bl A%! R7Ap:S7B/ .So.

oin in(ol(es .oin* )here the only omparison operator used is :* is alled an EQU32O3%7   3n the result of an EQU32O3% )e al)ays ha(e one or more pairs of attributes ")hose names need not be identi al' that ha(e identi al (alues in e(ery tuple7 The 2O3% seen in the pre(ious example )as an EQU32O3%7 .0inar# Relational Operations\$ %1"-*O➲ ➲ ➲ EQU32O3% Operation The most ommon use of .oin onditions )ith equality comparisons only Su h a .

oin re/uires that the t)o .0inar# Relational Operations\$ A'"RA& *O.Operation ➲ %ATURA0 2O3% Operation  Another (ariation of 2O3% alled %ATURA0 2O3% M denoted by P M )as reated to get rid of the se ond "superfluous' attribute in an EQU32O3% ondition7  be ause one of ea h pair of attributes )ith identi al (alues is superfluous   The standard definition of natural .oin attributes* or ea h pair of orresponding .oin attributes* have the same name in both relations 3f this is not the ase* a renaming operation is applied first7 .

oin ondition is reated based on this attribute1 !E\$ART#E%T7!%U#BER:!E\$TE0OCAT3O%S7!%U#BER Another example1 Q ← R"A*B*C*!' P S"C*!*E'  The impli it .oin on the !%U#BER attributes of !E\$ART#E%T and !E\$TE0OCAT3O%S* it is suffi ient to )rite1  !E\$TE0OCS ← !E\$ART#E%T P !E\$TE0OCAT3O%S ➲ ➲ Only attribute )ith the same name is !%U#BER An impli it .(contd() ➲ A'"RA& Example1 To apply a natural .oin ondition in ludes each pair of attributes )ith the same name* .0inar# Relational Operations *O.A%!-ed together1  ➲ R7C:S7C A%! R7!7S7! Q"A*B*C*!*E'  Result +eeps only one attribute of ea h su h pair1  .

ple of A'"RA& *O- operation .%+a.

oin ondition> "R 6 S' .oin ondition>S : σ ?.Co.plete Set of Relational Operations ➲ ➲ The set of operations in luding SE0ECT σ* \$RO2ECT π * U%3O% ∪* !3FFERE%CE − * RE%A#E ρ* and CARTES3A% \$RO!UCT 6 is alled a complete set be ause any other relational algebra expression an be expressed by a ombination of these fi(e operations7 For example1   R ∩ S : "R ∪ S ' H ""R − S' ∪ "S − R'' R ?.

0inar# Relational Operations\$ D-2-S-O ➲ !343S3O% Operation   The di(ision operation is applied to t)o relations R"T' ÷ S"6'* )here 6 subset T7 0et & : T J 6 "and hen e T : 6 ∪ &'I that is* let & be the set of attributes of R that are not attributes of S7  For a tuple t to appear in the result T of the !343S3O%* the (alues in t must appear in R in ombination )ith every tuple in S7 .

ple of D-2-S-O .%+a.

Recap of Relational Algebra Operations .

oin* renaming* di(ision* F7  0eaf nodes represent base relations  A tree gi(es a good (isual feel of the omplexity of the /uery and the operations in(ol(ed  Algebrai Query OptimiUation onsists of re)riting the /uery or modifying the /uery tree into an e/ui(alent tree7 (see Chapter 15)  .e tion* .1uer# 'ree otation ➲ Query Tree An internal data stru ture to represent a /uery  Standard te hni/ue for estimating the )or+ in(ol(ed in exe uting the /uery* the generation of intermediate results* and the optimiUation of exe ution  %odes stand for operations li+e sele tion* pro.

Example of Query Tree .

retrie(e the super(isees of V2ames BorgB at all le(els.Additional Relational Operations (cont() ➲ Although it is possible to retrie(e employees at ea h le(el and then ta+e their union* )e annot* in general* spe ify a /uery su h as .)ithout utiliUing a looping me hanism7  The SQ0< standard in ludes syntax for re ursi(e losure7 .

The OUTER 2O3% Operation

3n %ATURA0 2O3% and EQU32O3%* tuples )ithout a matching "or related' tuple are eliminated from the .oin result
 

Tuples )ith null in the .oin attributes are also eliminated This amounts to loss of information7

A set of operations* alled OUTER .oins* an be used )hen )e )ant to +eep all the tuples in R* or all those in S* or all those in both relations in the result of the .oin* regardless of )hether or not they ha(e mat hing tuples in the other relation7

➲ ➲

Additional Relational Operations (cont() The left outer .oin operation +eeps e(ery tuple in the first or left relation R in R SI if no mat hing tuple is found in S* then the attributes of S in the .oin result are filled or ,padded- )ith null (alues7 A similar operation* right outer .oin* +eeps e(ery tuple in the se ond or right relation S in the result of R S7 A third operation* full outer .oin* denoted by +eeps all tuples in both the left and the right relations )hen no mat hing tuples are found* padding them )ith null (alues as needed7

Additional Relational Operations (cont() ➲ OUTER U%3O% Operations    The outer union operation )as de(eloped to ta+e the union of tuples from t)o relations if the relations are not type compatible7 This operation )ill ta+e the union of tuples in t)o relations R"6* &' and S"6* T' that are partially compatible* meaning that only some of their attributes* say 6* are type ompatible7 The attributes that are type ompatible are represented only on e in the result* and those attributes that are not type ompatible from either relation are also +ept in the result relation T"6* &* T'7 .

Example1 An outer union an be applied to t)o relations )hose s hemas are STU!E%T"%ame* SS%* !epartment* Ad(isor' and 3%STRUCTOR"%ame* SS%* !epartment* Ran+'7

Tuples from the t)o relations are mat hed based on ha(ing the same ombination of (alues of the shared attributesM %ame* SS%* !epartment7 3f a student is also an instru tor* both Ad(isor and Ran+ )ill ha(e a (alueI other)ise* one of these t)o attributes )ill be null7 The result relation STU!E%TEORE3%STRUCTOR )ill ha(e the follo)ing attributes1

ST !"#T\$%&\$'#ST& CT%& (#ame( SS#( !epartment( )dvisor( &an*)

%+a,ple of Outer "nion

Rel R A @ < B A 9 B 9 W

Rel S C D X

Outer Union of R* S A @ < %U00 B A 9 W C %U00 D X

Additional Relational Operations\$ Aggregate .unctions and 3rouping

A type of re/uest that annot be expressed in the basi relational algebra is to spe ify mathemati al aggregate functions on olle tions of (alues from the database7 Examples of su h fun tions in lude retrie(ing the a(erage or total salary of all employees or the total number of employee tuples7

These fun tions are used in simple statisti al /ueries that summariUe information from the database tuples7 SU#* A4ERA5E* #A63#U#* and #3%3#U#7

Common fun tions applied to olle tions of numeri (alues in lude

The COU%T fun tion is used for ounting tuples or (alues7

unction Operation ➲ Use of the Aggregate Fun tional operation ℱ     ℱ#A6 Salary "E#\$0O&EE' retrie(es the maximum salary (alue from the E#\$0O&EE relation ℱ#3% Salary "E#\$0O&EE' retrie(es the minimum Salary (alue from the E#\$0O&EE relation ℱSU# Salary "E#\$0O&EE' retrie(es the sum of the Salary from the E#\$0O&EE relation ℱCOU%T SS%* A4ERA5E Salary "E#\$0O&EE' omputes the ount "number' of employees and their a(erage salary  %ote1 ount .ust ounts the number of ro)s* )ithout remo(ing dupli ates .Aggregate .

"sing 3rouping with Aggregation ➲ The pre(ious examples all summariUed one or more attributes for a set of tuples  #aximum Salary or Count "number of' Ssn ➲ ➲ ➲ 5rouping an be ombined )ith Aggregate Fun tions Example1 For ea h department* retrie(e the !%O* COU%T SS%* and A4ERA5E SA0AR& A (ariation of aggregate operation ℱ allo)s this1    5rouping attribute pla ed to left of symbol Aggregate fun tions to right of symbol !%O ℱCOU%T SS%* A4ERA5E Salary "E#\$0O&EE' ➲ Abo(e operation groups employees by !%O "department number' and omputes the ount of employees and a(erage salary per department .

ples of appl#ing aggregate functions and grouping .%+a.

-llustrating aggregate functions and grouping .

 Q1: Retrieve the name and address of all employees who work for the ‘Research’ department.%+a. RESEARCH_DEPT ← σ DNAME=’Research’ (DEPARTMENT RESEARCH_EMPS ← (RESEARCH_DEPT DN!M"ER= DN#EMP\$#%EE EMP\$#%EE RES!\$T ← π &NAME' \$NAME' ADDRESS (RESEARCH_EMPS  Q6: Retrieve the names of employees who have no dependents.ples of 1ueries in Relational Algebra \$ Procedural . A\$\$_EMPS ← π SSN(EMP\$#%EE EMPS_()TH_DEPS(SSN ← π ESSN(DEPENDENT EMPS_()TH#!T_DEPS ← (A\$\$_EMPS J EMPS_()TH_DEPS RES!\$T ← π \$NAME' &NAME (EMPS_()TH#!T_DEPS * EMP\$#%EE .or.

%+a. π Fname* 0name* Address "Y !name: VResear "!E\$ART#E%T  hB !number:!no"E#\$0O&EE'' Q6: Retrieve the names of employees who have no dependents. π 0name* Fname""π Ssn "E#\$0O&EE' Z [ Ssn "π Essn "!E\$E%!E%T''' ∗ E#\$0O&EE' .ples of 1ueries in Relational Algebra 4 Single e+pressions As a single expression* these /ueries be ome1  Q1: Retrieve the name and address of all employees who work for the ‘Research’ department.

Relational Calculus ➲ ➲ A relational calculus expression reates a ne) relation* )hi h is spe ified in terms of (ariables that range o(er ro)s of the stored database relations "in tuple calculus' or o(er olumns of the stored relations "in domain calculus'7 3n a al ulus expression* there is no order of operations to spe ify ho) to retrie(e the /uery resultMa al ulus expression spe ifies only )hat information the result should ontain7  This is the main distinguishing feature bet)een relational algebra and relational al ulus7 .

Relational Calculus (Contd() ➲ ➲ Relational al ulus is onsidered to be a nonprocedural or declarative language7 This differs from relational algebra* )here )e must )rite a sequence of operations to spe ify a retrie(al re/uestI hen e relational algebra an be onsidered as a procedural )ay of stating a /uery7 .

C%#!(t)  )here t is a tuple (ariable and CO%! "t' is a onditional expression in(ol(ing t7 The result of su h a /uery is the set of all tuples t that satisfy CO%! "t'7 .'uple Relational Calculus ➲ ➲ ➲ The tuple relational al ulus is based on spe ifying a number of tuple (ariables7 Ea h tuple (ariable usually ranges o(er a parti ular database relation* meaning that the (ariable may ta+e as its (alue any indi(idual tuple from that relation7 A simple tuple relational al ulus /uery is of the form +t .

D=*===* )e an )rite the follo)ing tuple al ulus expression1 +t.1#)0" . "021%3""(t) )#! t./#)0"( t.'uple Relational Calculus (Contd() ➲ ➲ ➲ Example1 To find the first and last names of all employees )hose salary is abo(e .S)1)&3455555The ondition E#\$0O&EE"t' spe ifies that the range relation of tuple (ariable t is E#\$0O&EE7 The first and last name "\$RO2ECT3O% π F%A#E* 0%A#E' of ea h E#\$0O&EE tuple t that satisfies the ondition t7SA0AR&>D==== "SE0ECT3O% σ SA0AR& >D====' )ill be retrie(ed7 .

'he %+istential and "niversal 1uantifiers ➲ ➲ ➲ T)o spe ial symbols alled /uantifiers an appear in formulasI these are the uni(ersal /uantifier (∀) and the existential /uantifier (∃). 3nformally* a tuple (ariable t is bound if it is /uantified* meaning that it appears in an (∀ t) or (∃ t) lauseI other)ise* it is free7 3f F is a formula* then so are (∃ t'"F' and (∀ t'"F'* )here t is a tuple (ariable7   The formula (∃t'"F' is true if the formula F e(aluates to true for some "at least one' tuple assigned to free o urren es of t in FI other)ise (∃ t'"F' is false7 The formula (∀ t'"F' is true if the formula F e(aluates to true for e(ery tuple "in the uni(erse' assigned to free o urren es of t in FI other)ise (∀ t'"F' is false7 .

'he %+istential and "niversal 1uantifiers (Contd() ➲ ➲ ∀ is alled the uni(ersal or ./uantifier be ause e(ery tuple in .there exists/uantifier be ause any tuple that exists in .the uni(erse of.the uni(erse of.tuples may ma+e F true to ma+e the /uantified formula true7 .tuples must ma+e F true to ma+e the /uantified formula true7 ∃ is alled the existential or .for all.

/#)0"( t.ple 1uer# "sing %+istential 1uantifier Retrie(e the name and address of all employees )ho )or+ for the VResear hB department7 The /uery an be expressed as 1 +t. "021%3""(t) and (∃ d) (!"2)&T0"#T(d) and d.!#)0"67&esearch8 and d.!# 09"&6t.!#%) ➲ The only free tuple variables in a relational al ulus expression should be those that appear to the left of the bar " O '7  3n abo(e /uery* t is the only free (ariableI it is then bound successively to ea h tuple7 ➲ 3f a tuple satisfies the conditions spe ified in the /uery* the attributes F%A#E* 0%A#E* and A!!RESS are retrie(ed for ea h su h tuple7  The onditions E#\$0O&EE "t' and !E\$ART#E%T"d' spe ify the range relations for t and d7  The ondition d7!%A#E : VResear hB is a sele tion ondition and orresponds to a SE0ECT operation in the relational algebra* )hereas the ondition d7!%U#BER : t7!%O is a 2O3% ondition7 ➲ .%+a.1#)0"( t.)!!&"SS .

)(<%&=S\$%#(.) and .)(<%&=S\$%#(."SS#6e./#)0" ."SS#6e..2# 09"&6.2#%))))➲ Ex lude from the uni(ersal /uantifi ation all tuples that )e are not interested in by ma+ing the ondition true for all such tuples7 ➲  The first tuples to ex lude "by ma+ing them e(aluate automati ally to true' are those that are not in the relation R of interest7 ➲ 3n /uery abo(e* using the expression not(2&%:"CT(x)) inside the uni(ersally /uantified formula e(aluates to true all tuples x that are not in the \$RO2ECT relation7  Then )e ex lude the tuples )e are not interested in from R itself7 The expression not"x7!%U#:D' e(aluates to true all tuples x that are in the pro.ple 1uer# "sing "niversal 1uantifier Find the names of employees )ho )or+ on all the pro.e ts ontrolled by department number D7 The /uery an be1 +e.SS# and x..2# 09"&6.1#)0"( e.!# 065) %& ( (∃ . "021%3""(e) and ( (∀ x)(not(2&%:"CT(x)) or not(x.2#%) .%+a.SS# and x.) and .e t relation but are not ontrolled by department D7 ➲ Finally* )e spe ify a ondition that must hold on all the remaining tuples in R7 ( (∃ ...

&anguages 0ased on 'uple Relational Calculus ➲ The language S>1 is based on tuple al ulus7 3t uses the basi blo + stru ture to express the /ueries in tuple al ulus1    SE0ECT ?list of attributes> FRO# ?list of relations> CNERE ? onditions> ➲ SE0ECT lause mentions the attributes being pro.e ted* the FRO# lause mentions the relations needed in the /uery* and the CNERE lause mentions the sele tion as )ell as the .oin onditions7  SQ0 syntax is expanded further to a operations7 "See Chapter

7 ommodate other .

&anguages 0ased on 'uple Relational Calculus (Contd() ➲ Another language )hi h is based on tuple al ulus is > "1 )hi h a tually uses the range (ariables as in tuple al ulus7 3ts syntax in ludes1  RA%5E OF ?(ariable name> 3S ?relation name> RETR3E4E ?list of attributes from range (ariables> CNERE ? onditions> ➲ Then it uses   ➲ This language )as proposed in the relational !B#S 3%5RES7 "system is urrently still supported by Computer Asso iates H but the QUE0 language is no longer there'7 .

'he Do.ain Relational Calculus ➲ ➲ ➲ ➲ Another (ariation of relational al ulus alled the domain relational al ulus* or simply* domain al ulus is e/ui(alent to tuple al ulus and to relational algebra7 The language alled QBE "QueryJByJExample' that is related to domain al ulus )as de(eloped almost on urrently to SQ0 at 3B# Resear h* &or+to)n Neights* %e) &or+7  !omain al ulus )as thought of as a )ay to explain )hat QBE does7 !omain al ulus differs from tuple al ulus in the type of (ariables used in formulas1  Rather than ha(ing (ariables range o(er tuples* the (ariables range o(er single (alues from domains of attributes7 To form a relation of degree n for a /uery result* )e must ha(e n of these domain (ariablesM one for ea h attribute7 .

.( xn@m)  )here x@* xA* 7 7 7* xn* xnL@* xnLA* 7 7 7* xnLm are domain (ariables that range o(er domains "of attributes' and CO%! is a ondition or formula of the domain relational al ulus7 . . ➲ C%#!(x1( x?( .'he Do. .( xn . .( xn( xn@1( xn@?( . . .ain Relational Calculus (Contd() An expression of the domain al ulus is of the form + x1( x?( .

(x(y(A) Ten (ariables for the employee relation are needed* one to range o(er the domain of ea h attribute in order7  Of the ten (ariables /* r* s* 7 7 7* U* only u and ( are free7 Spe ify the requested attributes* B!ATE and A!!RESS* by the free domain (ariables u for B!ATE and ( for A!!RESS7 Spe ify the ondition for sele ting a tuple follo)ing the bar " O 'M  namely* that the se/uen e of (alues assigned to the (ariables /rstu()xyU be a tuple of the employee relation and that the (alues for / "F%A#E'* r "#3%3T'* and s "0%A#E' be V2ohnB* VBB* and VSmithB* respe ti(ely7 ➲ ➲ ➲ .%+a.ple 1uer# "sing Do.xyA) uses the (ariables )ithout the separating ommas1 "021%3""(q(r(s(t(u(v(.ain Calculus Retrie(e the birthdate and address of the employee )hose name is V2ohn B7 SmithB7 ➲ Query 1 +uv .) (∃ x) (∃ y) (∃ A) ("021%3""(qrstuv.xyA) and q68:ohn8 and r6898 and s68Smith8)➲ )bbreviated notation "021%3""(qrstuv. (∃ q) (∃ r) (∃ s) (∃ t) (∃ .

ain Calculus (Appendi+ C) ➲ ➲ ➲ ➲ This language is based on the idea of gi(ing an example of a /uery using .print-' is pla ed in those olumns )hi h are re/uested for the result of the /uery7 A user may initially start gi(ing a tual (alues as examples* but later an get used to pro(iding a minimum number of (ariables as example elements7 .)hi h are nothing but domain (ariables7 %otation1 An example element stands for a domain (ariable and is spe ified as an example (alue pre eded by the unders ore hara ter7 \$7 " alled \$ dot' operator "for .10%\$ A 1uer# &anguage 0ased on Do.example elements.

ain Calculus (Appendi+ C) ➲ ➲ ➲ ➲ The language is (ery userJfriendly* be ause it uses minimal syntax7 QBE )as fully de(eloped further )ith fa ilities for grouping* aggregation* updating et 7 and is sho)n to be e/ui(alent to SQ07 The language is a(ailable under Q#F "Query #anagement Fa ility' of !BA of 3B# and has been used in (arious )ays by other produ ts li+e ACCESS of #i rosoft* and \$ARA!O67 For details* see Appendix C in the text7 .10%\$ A 1uer# &anguage 0ased on Do.

ples ➲ QBE initially presents a relational s hema as a .in )hi h the user fills in the /uery as an example1 .10% %+a.blan+ s hema.

Example S hema as a QBE Query 3nterfa e .

) (∃ x) (∃ y) (∃ A) ("021%3""(qrstuv.10% %+a.xyA) and q67:ohn8 and r6798 and s67Smith8)➲ .ples The follo)ing domain al ulus /uery an be su essi(ely minimiUed by the user as sho)n1 ➲ Query 1 +uv . (∃ q) (∃ r) (∃ s) (∃ t) (∃ .

Four Su essi(e )ays to spe ify a QBE Query .

e t @ or A* )hereas the /uery in C79"b' gi(es those )ho )or+ on both the pro.is used in QBE to state more in(ol(ed Boolean expressions as onditions7 The C79"a' gi(es employees )ho )or+ on either pro.ples ➲ ➲ ➲ Spe ifying omplex onditions in QBE1 A te hni/ue alled the .10% %+a.e ts7 . ondition box.

Complex Conditions )ith and )ithout a ondition box as a part of QBE Query .

Nandling A%! onditions in a QBE Query .

ples ➲ The .oin is simply a omplished by using the same example element "(ariable )ith unders ore' in the olumns being .*O➲ in 10% \$ %+a.oined from different "or same as in C7D "b'' relation7 %ote that the Result is set us as an independent table to sho) (ariables from multiple relations pla ed in the result7 .

\$erforming 2oin )ith ommon example elements and use of a RESU0T relation .

A33R%3A'-O ➲ in 10% ➲ ➲ Aggregation is a omplished by using 7C%T for ount*7#A6* 7#3%* 7A45 for the orresponding aggregation fun tions 5rouping is a omplished by 75 operator7 Condition Box may use onditions on groups "similar to NA43%5 lause in SQ0 H see Se tion D7

.

A55RE5AT3O% in QBE 1 Examples .

%E5AT3O% in QBE 1 Example .

U\$!AT3%5 in QBE 1 Examples .

Chapter Su.ar# ➲ Relational Algebra      Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra Tuple Relational Cal ulus !omain Relational Cal ulus ➲ Relational Cal ulus   ➲ O(er(ie) of the QBE language "appendix C' ..

7 ommodate other .

&anguages 0ased on 'uple Relational Calculus (Contd() ➲ Another language )hi h is based on tuple al ulus is > "1 )hi h a tually uses the range (ariables as in tuple al ulus7 3ts syntax in ludes1  RA%5E OF ?(ariable name> 3S ?relation name> RETR3E4E ?list of attributes from range (ariables> CNERE ? onditions> ➲ Then it uses   ➲ This language )as proposed in the relational !B#S 3%5RES7 "system is urrently still supported by Computer Asso iates H but the QUE0 language is no longer there'7 .

'he Do.ain Relational Calculus ➲ ➲ ➲ ➲ Another (ariation of relational al ulus alled the domain relational al ulus* or simply* domain al ulus is e/ui(alent to tuple al ulus and to relational algebra7 The language alled QBE "QueryJByJExample' that is related to domain al ulus )as de(eloped almost on urrently to SQ0 at 3B# Resear h* &or+to)n Neights* %e) &or+7  !omain al ulus )as thought of as a )ay to explain )hat QBE does7 !omain al ulus differs from tuple al ulus in the type of (ariables used in formulas1  Rather than ha(ing (ariables range o(er tuples* the (ariables range o(er single (alues from domains of attributes7 To form a relation of degree n for a /uery result* )e must ha(e n of these domain (ariablesM one for ea h attribute7 .

.( xn@m)  )here x@* xA* 7 7 7* xn* xnL@* xnLA* 7 7 7* xnLm are domain (ariables that range o(er domains "of attributes' and CO%! is a ondition or formula of the domain relational al ulus7 . . ➲ C%#!(x1( x?( .'he Do. .( xn . .( xn( xn@1( xn@?( . . .ain Relational Calculus (Contd() An expression of the domain al ulus is of the form + x1( x?( .

(x(y(A) Ten (ariables for the employee relation are needed* one to range o(er the domain of ea h attribute in order7  Of the ten (ariables /* r* s* 7 7 7* U* only u and ( are free7 Spe ify the requested attributes* B!ATE and A!!RESS* by the free domain (ariables u for B!ATE and ( for A!!RESS7 Spe ify the ondition for sele ting a tuple follo)ing the bar " O 'M  namely* that the se/uen e of (alues assigned to the (ariables /rstu()xyU be a tuple of the employee relation and that the (alues for / "F%A#E'* r "#3%3T'* and s "0%A#E' be V2ohnB* VBB* and VSmithB* respe ti(ely7 ➲ ➲ ➲ .%+a.ple 1uer# "sing Do.xyA) uses the (ariables )ithout the separating ommas1 "021%3""(q(r(s(t(u(v(.ain Calculus Retrie(e the birthdate and address of the employee )hose name is V2ohn B7 SmithB7 ➲ Query 1 +uv .) (∃ x) (∃ y) (∃ A) ("021%3""(qrstuv.xyA) and q68:ohn8 and r6898 and s68Smith8)➲ )bbreviated notation "021%3""(qrstuv. (∃ q) (∃ r) (∃ s) (∃ t) (∃ .

ain Calculus (Appendi+ C) ➲ ➲ ➲ ➲ This language is based on the idea of gi(ing an example of a /uery using .print-' is pla ed in those olumns )hi h are re/uested for the result of the /uery7 A user may initially start gi(ing a tual (alues as examples* but later an get used to pro(iding a minimum number of (ariables as example elements7 .)hi h are nothing but domain (ariables7 %otation1 An example element stands for a domain (ariable and is spe ified as an example (alue pre eded by the unders ore hara ter7 \$7 " alled \$ dot' operator "for .10%\$ A 1uer# &anguage 0ased on Do.example elements.

ain Calculus (Appendi+ C) ➲ ➲ ➲ ➲ The language is (ery userJfriendly* be ause it uses minimal syntax7 QBE )as fully de(eloped further )ith fa ilities for grouping* aggregation* updating et 7 and is sho)n to be e/ui(alent to SQ07 The language is a(ailable under Q#F "Query #anagement Fa ility' of !BA of 3B# and has been used in (arious )ays by other produ ts li+e ACCESS of #i rosoft* and \$ARA!O67 For details* see Appendix C in the text7 .10%\$ A 1uer# &anguage 0ased on Do.

ples ➲ QBE initially presents a relational s hema as a .in )hi h the user fills in the /uery as an example1 .10% %+a.blan+ s hema.

Example S hema as a QBE Query 3nterfa e .

) (∃ x) (∃ y) (∃ A) ("021%3""(qrstuv.10% %+a.xyA) and q67:ohn8 and r6798 and s67Smith8)➲ .ples The follo)ing domain al ulus /uery an be su essi(ely minimiUed by the user as sho)n1 ➲ Query 1 +uv . (∃ q) (∃ r) (∃ s) (∃ t) (∃ .

Four Su essi(e )ays to spe ify a QBE Query .

e t @ or A* )hereas the /uery in C79"b' gi(es those )ho )or+ on both the pro.is used in QBE to state more in(ol(ed Boolean expressions as onditions7 The C79"a' gi(es employees )ho )or+ on either pro.ples ➲ ➲ ➲ Spe ifying omplex onditions in QBE1 A te hni/ue alled the .10% %+a.e ts7 . ondition box.

Complex Conditions )ith and )ithout a ondition box as a part of QBE Query .

Nandling A%! onditions in a QBE Query .

ples ➲ The .oin is simply a omplished by using the same example element "(ariable )ith unders ore' in the olumns being .*O➲ in 10% \$ %+a.oined from different "or same as in C7D "b'' relation7 %ote that the Result is set us as an independent table to sho) (ariables from multiple relations pla ed in the result7 .

\$erforming 2oin )ith ommon example elements and use of a RESU0T relation .