Você está na página 1de 53

Chapter 6

The ReIationaI AIgebra and CaIcuIus


Copyright 2004 Ramez Elmasri and Shamkant Navathe
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-3
Chapter OutIine
Example Database Application (COMPANY)
Relational Algebra
&nary Relational Operations
Relational Algebra Operations From Set Theory
inary Relational Operations
Additional Relational Operations
Examples oI Queries in Relational Algebra
Relational Calculus
Tuple Relational Calculus
omain Relational Calculus
Overview of the QBE language (appendix D)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-4
Database State for COMPANY
All examples discussed below reIer to the COMPANY database shown here.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-5
ReIationaI AIgebra
The basic set oI operations Ior the relational model is known
as the relational algebra. These operations enable a user to
speciIy basic retrieval requests.
The result oI a retrieval is a new relation, which may have
been Iormed Irom one or more relations. The algebra
operations thus produce new relations, which can be Iurther
manipulated using operations oI the same algebra.
A sequence oI relational algebra operations Iorms a
relational algebra expression, whose result will also be a
relation that represents the result oI a database query (or
retrieval request).
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-6
&nary ReIationaI Operations
SELECT Operation
SELECT operation is used to select a subset oI the tuples Irom a relation that
satisIy a selection condition. It is a Iilter that keeps only those tuples that
satisIy a qualiIying condition those satisIying the condition are selected
while others are discarded.
Example: To select the EMPLOYEE tuples whose department number is
Iour or those whose salary is greater than $30,000 the Iollowing notation is
used:
9O 4 (EMPLOYEE)
9SALARY > 30,000 (EMPLOYEE)
In general, the select operation is denoted by
9 selection condition~
(R) where the
symbol 9 (sigma) is used to denote the select operator, and the selection
condition is a oolean expression speciIied on the attributes oI relation R
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-7
&nary ReIationaI Operations
SELECT Operation Properties
The SELECT operation 9
selection condition~
(R) produces a relation S that
has the same schema as R
The SELECT operation 9 is commutative; i.e.,
9
condition1~
(9
condition2~
( R)) 9
condition2~
(9
condition1~
( R))
A cascaded SELECT operation may be applied in any order; i.e.,
9
condition1~
(9
condition2~
(9
condition3~
( R))
9
condition2~
(9
condition3~
(9
condition1~
( R)))
A cascaded SELECT operation may be replaced by a single selection
with a conjunction oI all the conditions; i.e.,
9
condition1~
(9
condition2~
(9
condition3~
( R))
9
condition1~ AN condition2~ AN condition3~
( R)))
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-8
&nary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-9
&nary ReIationaI Operations (cont.)
PRO1ECT Operation
This operation selects certain .4umns Irom the table and discards the other
columns. The PROJECT creates a vertical partitioning one with the needed
columns (attributes) containing results oI the operation and other containing
the discarded Columns.
Example: To list each employee`s Iirst and last name and salary, the
Iollowing is used:
6
LAME, FAME,SALARY
(EMPLOYEE)
The general Iorm oI the project operation is 6attribute list~(R) where 6
(pi) is the symbol used to represent the project operation and attribute list~
is the desired list oI attributes Irom the attributes oI relation R.
The project operation 7em4;es any dup.ate tupes, so the result oI the
project operation is a set oI tuples and hence a valid relation.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-10
&nary ReIationaI Operations (cont.)
PRO1ECT Operation Properties
The number oI tuples in the result oI projection 6
list>
Ris always
less or equal to the number oI tuples in R.
II the list oI attributes includes a key oI R, then the number oI tuples is
equal to the number oI tuples in R.
6
list1>
6
list2>
R) 6
list1>
Ras long as list2~ contains
the attributes in list2~
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-11
&nary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-12
&nary ReIationaI Operations (cont.)
Rename Operation
e may want to apply several relational algebra operations one aIter the other.
Either we can write the operations as a single relational algebra expression
by nesting the operations, or we can apply one operation at a time and create
intermediate result relations. In the latter case, we must give names to the
relations that hold the intermediate results.
Example: To retrieve the Iirst name, last name, and salary oI all employees
who work in department number 5, we must apply a select and a project
operation. e can write a single relational algebra expression as Iollows:
6
FAME, LAME, SALARY
(9
O5
(EMPLOYEE))
OR e can explicitly show the sequence oI operations, giving a name to each
intermediate relation:
EP5_EMPS 9
O5
(EMPLOYEE)
RESULT 6
FAME, LAME, SALARY
(EP5_EMPS)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-13
&nary ReIationaI Operations (cont.)
Rename Operation (cont.)
The rename operator is 8
The general Rename operation can be expressed by any oI the
Iollowing Iorms:
8
S (
1
,
2
, .,
n
)
( R) is a renamed relation S based on R with column names
1
,
1
,
...
n

8
S
( R) is a renamed relation S based on R (which does not speciIy column names).
8
(
1
,
2
, .,
n
)
( R) is a renamed relation with column names
1
,
1
, ...
n
which
does not speciIy a new relation name.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-14
&nary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-15
ReIationaI AIgebra Operations From
Set Theory
UO Operation
The result oI this operation, denoted by R S, is a relation that includes all
tuples that are either in R or in S or in both R and S. uplicate tuples are
eliminated.
Example: To retrieve the social security numbers oI all employees who either
work in department 5 or directly supervise an employee who works in
department 5, we can use the union operation as Iollows:
EP5_EMPS 9
O5
(EMPLOYEE)
RESULT1 6
SS
(EP5_EMPS)
RESULT2(SS) 6
SUPERSS
(EP5_EMPS)
RESULT RESULT1 RESULT2
The union operation produces the tuples that are in either RES&LT1 or
RES&LT2 or both. The two operands must be 'type compatible.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-16
ReIationaI AIgebra Operations From
Set Theory
Type Compatibility
The operand relations R
1
(A
1
, A
2
, ..., A
n
) and R
2
(
1
,
2
, ...,
n
)
must have the same number oI attributes, and the domains oI
corresponding attributes must be compatible; that is,
dom(A
i
)dom(
i
) Ior i1, 2, ..., n.
The resulting relation Ior R
1
R
2
,R
1
Y R
2
, or R
1
-R
2
has the
same attribute names as the 17st operand relation R1 (by
convention).
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-17
ReIationaI AIgebra Operations From
Set Theory
UO Example
$%&DEN%N$%R&C%OR
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-18
ReIationaI AIgebra Operations From Set
Theory (cont.) - use Fig. 6.4
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-19
ReIationaI AIgebra Operations From Set
Theory (cont.)
TERSECTO OPERATO
The result oI this operation, denoted by R Y S, is a relation that includes all
tuples that are in both R and S. The two operands must be "type compatible"
Example: The result oI the intersection operation (Iigure below) includes only
those who are both students and instructors.
$%&DEN% YN$%R&C%OR
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-20
ReIationaI AIgebra Operations From Set
Theory (cont.)
Set ifference (or MUS) Operation
The result oI this operation, denoted by R - S, is a relation that includes all
tuples that are in R but not in S. The two operands must be "type compatible.
Example: The Iigure shows the names oI students who are not instructors, and
the names oI instructors who are not students.
ST&ENT-INSTR&CTOR
INSTR&CTOR-ST&ENT
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-21
ReIationaI AIgebra Operations From Set
Theory (cont.)
Notice that both union and intersection are .4mmutat;e
4pe7at4ns, that is
R S S R, and R Y S S Y R
oth union and intersection can be treated as n-ary operations
applicable to any number oI relations as both are ass4.at;e
4pe7at4ns, that is
R (S T) (R S) T, and (R Y S) Y T R Y (S Y T)
The minus operation is n4t .4mmutat;e, that is, in general
R - S / S - R
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-22
ReIationaI AIgebra Operations From Set
Theory (cont.)
CARTESA (or cross product) Operation
This operation is used to combine tuples Irom two relations in a
combinatorial Iashion. In general, the result oI R(A
1
, A
2
, . . ., A
n
) x S(
1
,

2
, . . .,
m
) is a relation Q with degree n m attributes Q(A
1
, A
2
, . . ., A
n
,

1
,
2
, . . .,
m
), in that order. The resulting relation Q has one tuple Ior
each combination oI tuplesone Irom R and one Irom S.
ence, iI R has n
R
tuples (denoted as ,R, n
R
), and S has n
S
tuples, then
, R x S , will have n
R
* n
S
tuples.
The two operands do NOT have to be "type compatible
Example:
FEMALE_EMPS 9
SEX`F`
(EMPLOYEE)
EMPAMES 6
FNAME, LNAME, SSN
(FEMALE_EMPS)
EMP_EPEETS EMPAMES x EPEET
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-23
ReIationaI AIgebra Operations From Set
Theory (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-24
inary ReIationaI Operations
1O Operation
The sequence oI cartesian product Iollowed by select is used
quite commonly to identiIy and select related tuples Irom two
relations, a special operation, called 1O. It is denoted by a
This operation is very important Ior any relational database
with more than a single relation, because it allows us to process
relationships among relations.
The general Iorm oI a join operation on two relations R(A
1
, A
2
,
. . ., A
n
) and S(
1
,
2
, . . .,
m
) is:
R
join condition~
S
where R and S can be any relations that result Irom general
7eat4na aeb7a exp7ess4ns.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-25
inary ReIationaI Operations (cont.)
Example: Suppose that we want to retrieve the name oI
the manager oI each department. To get the manager`s
name, we need to combine each EPARTMENT tuple
with the EMPLOYEE tuple whose SSN value matches
the MGRSSN value in the department tuple. e do this
by using the join operation.
EPT_MGR EPARTMET
MGRSSSS
EMPLOYEE
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-26
inary ReIationaI Operations (cont.)
EQU1O Operation
The most common use oI join involves join conditions with equality comparisons only.
Such a join, where the only comparison operator used is , is called an EQ&IJOIN. In
the result oI an EQ&IJOIN we always have one or more pairs oI attributes (whose
names need not be identical) that have dent.a ;aues in every tuple.
The JOIN seen in the previous example was EQ&IJOIN.
ATURAL 1O Operation
ecause one oI each pair oI attributes with identical values is superIluous, a new
operation called natural joindenoted by *was created to get rid oI the second
(superIluous) attribute in an EQ&IJOIN condition.
The standard deIinition oI natural join requires that the two join attributes, or each pair
oI corresponding join attributes, have the same name in both relations. II this is not the
case, a renaming operation is applied Iirst.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-27
inary ReIationaI Operations (cont.)
Example: To apply a natural join on the N&MER attributes oI
EPARTMENT and EPTLOCATIONS, it is suIIicient to write:
EPT_LOCS EPARTMET ` EPT_LOCATOS
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-28
CompIete Set of ReIationaI Operations
The set oI operations including select 9,
project 6 , union , set difference - , and
cartesian product X is called a complete set
because any other relational algebra expression
can be expressed by a combination oI these Iive
operations.
For example:
R Y S (R S ) - ((R S) (S R))
R
join condition~
S 9
join condition~
(R X S)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-29
inary ReIationaI Operations (cont.)
'SO Operation
The division operation is applied to two relations
R(Z) S(X), where X subset Z. Let Y Z - X (and hence Z
X Y); that is, let Y be the set oI attributes oI R that are
not attributes oI S.
The result oI IVISION is a relation T(Y) that includes a
tuple t iI tuples t
R
appear in R with t
R
|Y| t, and with
t
R
|X| t
s
147 e;e7y tupe t
s
in S.
For a tuple t to appear in the result T oI the IVISION, the
values in t must appear in R in combination with e;e7y tuple
in S.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-30
inary ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-31
Recap of ReIationaI AIgebra Operations
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-32
AdditionaI ReIationaI Operations
Aggregate Functions and Grouping
A type oI request that cannot be expressed in the basic relational algebra
is to speciIy mathematical aggregate functions on collections oI values
Irom the database.
Examples oI such Iunctions include retrieving the average or total salary
oI all employees or the total number oI employee tuples. These Iunctions
are used in simple statistical queries that summarize inIormation Irom
the database tuples.
Common Iunctions applied to collections oI numeric values include
S&M, AVERAGE, MAXIM&M, and MINIM&M. The CO&NT
Iunction is used Ior counting tuples or values.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-33
AdditionaI ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-34
Use of the Functional operator

MAX $aa7y
(Employee) retrieves the maximum salary value
Irom the Employee relation

MIN $aa7y
(Employee) retrieves the minimum Salary value Irom
the Employee relation

S&M $aa7y
(Employee) retrieves the sum oI the Salary Irom the
Employee relation
NO

&% $$, AJERAGE $aa7y


(Employee) groups employees by
NO (department number) and computes the count oI
employees and average salary per department.| Note: count
just counts the number oI rows, without removing duplicates|
AdditionaI ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-35
AdditionaI ReIationaI Operations (cont.)
Recursive Closure Operations
Another type oI operation that, in general, cannot be speciIied in the
basic original relational algebra is recursive closure. This operation is
applied to a recursive relationship.
An example oI a recursive operation is to retrieve all S&PERVISEES oI
an EMPLOYEE e at all levelsthat is, all EMPLOYEE e` directly
supervised by e; all employees e`` directly supervised by each employee
e`; all employees e``` directly supervised by each employee e``; and so
on .
Although it is possible to retrieve employees at each level and then take
their union, we cannot, in general, speciIy a query such as 'retrieve the
supervisees oI James org` at all levels without utilizing a looping
mechanism.
The SQL3 standard includes syntax Ior recursive closure.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-36
AdditionaI ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-37
AdditionaI ReIationaI Operations (cont.)
The OUTER 1O Operation
In NAT&RAL JOIN tuples without a mat.n (or 7eated) tuple are eliminated
Irom the join result. Tuples with null in the join attributes are also eliminated.
This amounts to loss oI inIormation.
A set oI operations, called outer joins, can be used when we want to keep all the
tuples in R, or all those in S, or all those in both relations in the result oI the
join, regardless oI whether or not they have matching tuples in the other relation.
The leIt outer join operation keeps every tuple in the 17st or e1t relation R in
R S; iI no matching tuple is Iound in S, then the attributes oI S in the join
result are Iilled or 'padded with null values.
A similar operation, right outer join, keeps every tuple in the se.4nd or right
relation S in the result oI R S.
A third operation, Iull outer join, denoted by keeps all tuples in both the
leIt and the right relations when no matching tuples are Iound, padding them
with null values as needed.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-38
AdditionaI ReIationaI Operations (cont.)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-39
AdditionaI ReIationaI Operations (cont.)
OUTER UO Operations
The outer union operation was developed to take the union oI tuples Irom two
relations iI the relations are n4t un4n .4mpatbe.
This operation will take the union oI tuples in two relations R(X, Y) and S(X, Z)
that are partially compatible, meaning that only some oI their attributes, say X,
are union compatible.
The attributes that are union compatible are represented only once in the result,
and those attributes that are not union compatible Irom either relation are also
kept in the result relation T(X, Y, Z).
Example: An outer union can be applied to two relations whose schemas are
ST&ENT(Name, SSN, epartment, Advisor) and INSTR&CTOR(Name, SSN,
epartment, Rank). Tuples Irom the two relations are matched based on having
the same combination oI values oI the shared attributesName, SSN,
epartment. II a student is also an instructor, both Advisor and Rank will have a
value; otherwise, one oI these two attributes will be null.
The result relation ST&ENTORINSTR&CTOR will have the Iollowing
attributes:
STUET_OR_STRUCTOR (ame, SS, epartment, Advisor, Rank)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-40
ampIes of Queries in ReIationaI AIgebra
Q1: Retrieve the name and address of all employees who
work for the Research` department.
RESEARCEPT 9 NAME`Research` (EPARTMENT)
RESEARCEMPS (RESEARCEPT
N&MER
NOEMPLOYEE
EMPLOYEE)
RES&LT 6 FNAME, LNAME, ARESS (RESEARCEMPS)
Q6: Retrieve the names of employees who have no
dependents.
ALLEMPS 6SSN(EMPLOYEE)
EMPSITEPS(SSN) 6ESSN(EPENENT)
EMPSITO&TEPS (ALLEMPS EMPSITEPS)
RES&LT 6 LNAME, FNAME (EMPSITO&TEPS * EMPLOYEE)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-41
ReIationaI CaIcuIus
A relational calculus expression creates a new relation, which is
speciIied in terms oI variables that range over rows oI the stored
database relations (in tuple calculus) or over columns oI the
stored relations (in domain calculus).
In a calculus expression, there is n4 47de7 41 4pe7at4ns to
speciIy how to retrieve the query resulta calculus expression
speciIies only what inIormation the result should contain. This is
the main distinguishing Ieature between relational algebra and
relational calculus.
Relational calculus is considered to be a nonprocedural
language. This diIIers Irom relational algebra, where we must
write a sequen.e 41 4pe7at4ns to speciIy a retrieval request;
hence relational algebra can be considered as a procedural way
oI stating a query.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-42
TupIe ReIationaI CaIcuIus
The tuple relational calculus is based on speciIying a number oI tuple variables. Each
tuple variable usually 7anes 4;e7 a particular database relation, meaning that the
variable may take as its value any individual tuple Irom that relation.
A simple tuple relational calculus query is oI the Iorm
t , CON(t)}
where t is a tuple variable and CON (t) is a conditional expression involving t. The
result oI such a query is the set oI all tuples t that satisIy CON (t).
Example: To Iind the Iirst and last names oI all employees whose salary is above
$50,000, we can write the Iollowing tuple calculus expression:
t.FAME, t.LAME [ EMPLOYEE(t) At.SALARY>50000]
The condition EMPLOYEE(t) speciIies that the range relation oI tuple variable t is
EMPLOYEE. The Iirst and last name (PROJECTION 6
FAME, LAME
) oI each
EMPLOYEE tuple t that satisIies the condition t.SALARY~50000 (SELECTION
9
SALARY >50000
) will be retrieved.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-43
The istentiaI and &niversaI
Quantifiers
Two special symbols called 6uantifiers can appear in Iormulas; these are the
universal 6uantifier ) and the existential 6uantifier ).
InIormally, a tuple variable t is bound iI it is quantiIied, meaning that it
appears in an (t) or ( t) clause; otherwise, it is free.
II F is a Iormula, then so is ( t)(F), where t is a tuple variable. The Iormula
( t)(F) is true iI the Iormula F evaluates to true Ior s4me (at least one) tuple
assigned to Iree occurrences oI t in F; otherwise ( t)(F) is false.
II F is a Iormula, then so is (t)(F), where t is a tuple variable. The Iormula
( t)(F) is true iI the Iormula F evaluates to true Ior e;e7y tupe (in the
universe) assigned to Iree occurrences oI t in F; otherwise (t)(F) is false.
It is called the universal or 'Ior all quantiIier because every tuple in 'the
universe oI tuples must make F true to make the quantiIied Iormula true.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-44
ampIe Query &sing istentiaI Quantifier
Retrieve the name and address oI all employees who work Ior the Research`
department.
Query :
t.FNAM, t.LNAM, t.ADDRSS | MPLOY(t) and d)
(DPARTMNT(d) and d.DNAMResearch' and d.DN&MRt.DNO) }
The 4ny 17ee tupe ;a7abes in a relational calculus expression should be
those that appear to the leIt oI the bar ( , ). In above query, t is the only Iree
variable; it is then b4und su..ess;ey to each tuple. II a tuple sats1es te
.4ndt4ns speciIied in the query, the attributes FNAME, LNAME, and
ARESS are retrieved Ior each such tuple.
The conditions EMPLOYEE (t) and EPARTMENT(d) speciIy the range
relations Ior t and d. The condition d.NAME Research` is a selection
condition and corresponds to a SELECT operation in the relational algebra,
whereas the condition d.N&MER t.NO is a JOIN condition.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-45
ampIe Query &sing &niversaI Quantifier
Find the names oI employees who work on a the projects controlled by
department number 5.
Query :
e.LNAM, e.FNAM | MPLOY(e) and )(not(PROJCT()) or
not(.DN&M)
OR ( ( w)(WORKS_ON(w) and w.SSNe.SSN and .PN&MRw.PNO) ) ) )}
Exclude Irom the universal quantiIication all tuples that we are not interested in
by making the condition true 147 a su. tupes. The Iirst tuples to exclude (by
making them evaluate automatically to true) are those that are not in the relation R
oI interest.
In query above, using the expression not(PROJECT(x)) inside the universally
quantiIied Iormula evaluates to true all tuples x that are not in the PROJECT
relation. Then we exclude the tuples we are not interested in Irom R itselI. The
expression not(x.N&M5) evaluates to true all tuples x that are in the project
relation but are not controlled by department 5.
Finally, we speciIy a condition that must hold on all the remaining tuples in R.
( ( w)(WORKS_ON(w) and w.SSNe.SSN and .PN&MRw.PNO)
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-46
Languages ased on TupIe
ReIationaI CaIcuIus
The language SQL is based on tuple calculus. It uses the basic
SELECT list oI attributes~
FROM list oI relations~
ERE conditions~
block structure to express the queries in tuple calculus where the SELECT clause
mentions the attributes being projected, the FROM clause mentions the relations
needed in the query, and the ERE clause mentions the selection as well as the
join conditions.
SQL syntax is expanded Iurther to accommodate other operations. (See Chapter 8).
Another language which is based on tuple calculus is QUEL which actually
uses the range variables as in tuple calculus.
Its syntax includes:
RANGE OF variable name~ IS relation name~
Then it uses
RETRIEVE list oI attributes Irom range variables~
ERE conditions~
This language was proposed in the relational MS INGRES.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-47
The Domain ReIationaI CaIcuIus
Another variation oI relational calculus called the domain relational calculus, or
simply, domain calculus is equivalent to tuple calculus and to relational algebra.
The language called QE (Query-y-Example) that is related to domain calculus was
developed almost concurrently to SQL at IM Research, Yorktown eights, New
York. omain calculus was thought oI as a way to explain what QE does.
omain calculus diIIers Irom tuple calculus in the type 41 ;a7abes used in Iormulas:
rather than having variables range over tuples, the variables range over single values
Irom domains oI attributes. To Iorm a relation oI degree n Ior a query result, we must
have n oI these domain variablesone Ior each attribute.
An expression oI the domain calculus is oI the Iorm
x1, x2, . . ., xn , CON(x1, x2, . . ., xn, xn1, xn2, . . ., xnm)}
where x1, x2, . . ., xn, xn1, xn2, . . ., xnm are domain variables that range over
domains (oI attributes) and CON is a condition or formula oI the domain relational
calculus.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-48
ampIe Query &sing Domain CaIcuIus
Retrieve the birthdate and address oI the employee whose name is John .
Smith`.
Query :
uv | ( 6) ( r) ( s) ( t) ( w) ( ) ( y) ( z)
(MPLOY(6rstuvwyz) and 6'John' and r'' and s'Smith')}
Ten variables Ior the employee relation are needed, one to range over the
domain oI each attribute in order. OI the ten variables q, r, s, . . ., z, only u and
v are Iree.
SpeciIy the 7equested att7butes, ATE and ARESS, by the Iree domain
variables u Ior ATE and v Ior ARESS.
SpeciIy the condition Ior selecting a tuple Iollowing the bar ( [ )namely, that
the sequence oI values assigned to the variables qrstuvwxyz be a tuple oI the
employee relation and that the values Ior q (FNAME), r (MINIT), and s
(LNAME) be John`, `, and Smith`, respectively.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-49
Q A Query Language ased on Domain
CaIcuIus (Appendi D)
This language is based on the idea oI giving an example oI a query using
example elements.
An example element stands Ior a domain variable and is speciIied as an
example value preceded by the underscore character.
P. (called P dot) operator (Ior 'print) is placed in those columns which are
requested Ior the result oI the query.
A user may initially start giving actual values as examples, but later can get
used to providing a minimum number oI variables as example elements.
The language is very user-Iriendly, because it uses minimal syntax.
QE was Iully developed Iurther with Iacilities Ior grouping, aggregation,
updating etc. and is shown to be equivalent to SQL.
The language is available under QMF (Query Management Facility) oI 2
oI IM and has been used in various ways by other products like ACCESS oI
MicrosoIt, PARAOX.
For details, see Appendix in the text.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-50
Q ampIes
QE initially presents a relational schema as a 'blank schema in
which the user Iills in the query as an example:
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-51
Q ampIes
The Iollowing domain calculus query can be successively
minimized by the user as shown:
Query :
uv | ( 6) ( r) ( s) ( t) ( w) ( ) ( y) ( z)
(MPLOY(6rstuvwyz) and 6'John' and r'' and s'Smith')}
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-52
Q ampIes
SpeciIying complex cinditions in QE:
A technique called the 'condition box is used in QE to state
more involved oolean expressions as conditions.
The .4(a) gives employees who work on either project 1 or 2,
whereas the query in .4(b) gives those who work on both the
projects.
Copyright 2004 Ramez Elmasri and Shamkant Navathe
Imasri/Navathe, FundamentaIs of Database Systems, Fourth dition Chapter 6-53
Q ampIes
Illustrating join in QE. The join is simple accomplished by
using the same example element in the columns being joined.
Note that the Result is set us as an independent table.

Você também pode gostar