Você está na página 1de 91

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

"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 ;<=*===1
!%O : 9

SA0AR& > <=*===

"E#$0O&EE'

"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'

"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.e t operation removes any duplicate tuples

#athemati al sets do not allow dupli ate elements7

"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

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

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

Single e+pression versus se)uence of relational operations (%+a,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'

"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'

3f )e )rite1
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 ,type ompatible- "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'

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'

Relational Algebra Operations fro, Set 'heor#$ - '%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 ,type ompatible-

Relational Algebra Operations fro, Set 'heor#$ S%' D-..%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 ,type ompatible-

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

So,e properties of " -O / - '%RS%C'/ and D-..%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

Relational Algebra Operations fro, 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, 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

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 operation7 !E$TE#5R !E$ART#E%T
#5RSS%:SS%

E#$0O&EE

#5RSS%:SS% is the .oin ondition


Combines ea h department re ord )ith the employee )ho manages the department The .oin ondition an also be spe ified as !E$ART#E%T7#5RSS%: E#$0O&EE7SS%

%+a,ple of appl#ing the *O-

operation

!E$TE#5R !E$ART#E%T

#5RSS%:SS%

E#$0O&EE

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.

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.S Nen e* if R has nR tuples* and S has nS tuples* then the .oin result )ill generally ha(e less than nR P nS tuples7 Only related tuples "based on the .oin ondition' )ill appear in the result

So,e properties of *O

The general ase of 2O3% operation is alled a ThetaJ.oin1 R S


theta

The .oin ondition is alled theta Theta an be any general boolean expression on the attributes of R and SI for example1

R7Ai?S7B. A%! "R7A+:S7Bl OR R7Ap?S7B/'

#ost .oin onditions in(ol(e one or more e/uality onditions ,A%!-ed togetherI for example1

R7Ai:S7B. A%! R7A+:S7Bl A%! R7Ap:S7B/

0inar# Relational Operations$ %1"-*O

EQU32O3% Operation The most ommon use of .oin in(ol(es .oin onditions )ith equality comparisons only Su h a .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$ 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 re/uires that the t)o .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

0inar# Relational Operations *O- (contd()

A'"RA&

Example1 To apply a natural .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 .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 ondition in ludes each pair of attributes )ith the same name* ,A%!-ed together1

R7C:S7C A%! R7!7S7! Q"A*B*C*!*E'

Result +eeps only one attribute of ea h su h pair1

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

operation

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 ?.oin ondition>S : ?.oin ondition> "R 6 S'

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

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

Recap of Relational Algebra Operations

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.e tion* .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)

Example of Query Tree

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 ,retrie(e the super(isees of V2ames BorgB at all le(els- )ithout utiliUing a looping me hanism7

The SQ0< standard in ludes syntax for re ursi(e losure7

Additional Relational Operations (cont()

Additional Relational Operations (cont()

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()

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

Additional Relational Operations (cont()

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

Aggregate .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

"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

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

-llustrating aggregate functions and grouping

%+a,ples of 1ueries in Relational Algebra $ Procedural .or,

Q1: Retrieve the name and address of all employees who work for the Research department. 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.


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

%+a,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.

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'

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

'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 , 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 (Contd()

Example1 To find the first and last names of all employees )hose salary is abo(e ;D=*===* )e an )rite the follo)ing tuple al ulus expression1 +t./#)0"( t.1#)0" , "021%3""(t) )#! t.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 ,for all- /uantifier be ause e(ery tuple in ,the uni(erse of- tuples must ma+e F true to ma+e the /uantified formula true7 is alled the existential or ,there exists/uantifier be ause any tuple that exists in ,the uni(erse of- tuples may ma+e F true to ma+e the /uantified formula true7

%+a,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./#)0"( t.1#)0"( t.)!!&"SS , "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,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.1#)0"( e./#)0" , "021%3""(e) and ( ( x)(not(2&%:"CT(x)) or not(x.!# 065) %& ( ( ;)(<%&=S$%#(;) and ;."SS#6e.SS# and x.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.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 ( ( ;)(<%&=S$%#(;) and ;."SS#6e.SS# and x.2# 09"&6;.2#%)

&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

'he Do,ain Relational Calculus (Contd()

An expression of the domain al ulus is of the form + x1( x?( . . .( xn ,

C%#!(x1( x?( . . .( xn( xn@1( xn@?( . . .( 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

%+a,ple 1uer# "sing Do,ain Calculus


Retrie(e the birthdate and address of the employee )hose name is V2ohn B7 SmithB7 Query 1 +uv , ( q) ( r) ( s) ( t) ( ;) ( x) ( y) ( A) ("021%3""(qrstuv;xyA) and q68:ohn8 and r6898 and s68Smith8)

)bbreviated notation "021%3""(qrstuv;xyA) uses the (ariables )ithout the separating ommas1 "021%3""(q(r(s(t(u(v(;(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

10%$ A 1uer# &anguage 0ased on Do,ain Calculus (Appendi+ C)

This language is based on the idea of gi(ing an example of a /uery using ,example elements- )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 ,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

10%$ A 1uer# &anguage 0ased on Do,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,ples

QBE initially presents a relational s hema as a ,blan+ s hema- in )hi h the user fills in the /uery as an example1

Example S hema as a QBE Query 3nterfa e

10% %+a,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) ( ;) ( x) ( y) ( A) ("021%3""(qrstuv;xyA) and q67:ohn8 and r6798 and s67Smith8)

Four Su

essi(e )ays to spe ify a QBE Query

10% %+a,ples

Spe ifying omplex onditions in QBE1 A te hni/ue alled the , ondition box- 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.e t @ or A* )hereas the /uery in C79"b' gi(es those )ho )or+ on both the pro.e ts7

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

Nandling A%! onditions in a QBE Query

*O

in 10% $ %+a,ples

The .oin is simply a omplished by using the same example element "(ariable )ith unders ore' in the olumns being .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 \7D7\'

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'

Você também pode gostar