Chap 6
Chap 6
1
What is an Algebra?
• A language based on operators and a domain of values
• Operators map values taken from the domain into other
domain values
• Hence, an expression involving operators and arguments
produces a value in the domain
• When the domain is a set of all relations (and the operators are
as described later), we get the relational algebra
• We refer to the expression as a query and the value produced
as the query result
2
Relational Algebra
• Domain: set of relations
Fundamental Operations
SELECT
PROJECT
UNION
SET DIFFERENCE
CARTESIAN PRODUCT
• Select and project operations are unary operation as they
operate on a single relation. Union, set difference, Cartesian
product and operations are binary operations as they operate on
pairs of relations.
3
Relational Algebra
• Basic operations:
– Selection ( ) Selects a subset of rows from relation.
– Projection ( ) Deletes unwanted columns from relation.
–
Cross-product ( ) Allows us to combine two relations.
–
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
– Union ( ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!)
useful.
• Since each operation returns a relation, operations can be
composed!
Selection Condition
• Selects rows that satisfy • Operators: <, , , >,
selection condition.
• Schema of result identical to =,
schema of (only) input relation. • Simple selection
• Result relation can be the input
for another relational algebra
condition:
operation. • <condition> AND
<condition>
• <condition> OR
<condition>
• NOT <condition>
5
Selection Condition - Examples
• salary>3000 Or name=‘Tom’ (Employee)
• nr>5 AND nr <8 (Employee)
• NOT(name=‘John’) (Employee)
Table1: Employee • nr 7 (Employee)
Note that the select operation in relational algebra has nothing to do with the SQL keyword select.
Selection in relational algebra returns those tuples in a relation that fulfil a condition, while the SQL
keyword select means "here comes an SQL statement".
Projection
• Deletes attributes that are not in projection list.
• Schema of result contains exactly the fields in the projection list, with the same names that they had
in the (only) input relation.
• Projection operator has to eliminate duplicates! (Why??, what are the consequences?)
– Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it. (Why not?)
– where'Π. (pi) is the symbol used to represent the PROJECT
operation,and<attributelist>
– Is the desired list of attributes from the attributes of relation R
• Produces table containing subset of columns of argument table
attribute list(relation)
Example:
Person Name,Hobby(Person)
Id Name Address Hobby Name Hobby
1123 John 123 Main stamps John stamps
1123 John 123 Main coins John coins
5556 Mary 7 Lake Dr hiking Mary hiking
9876 Bart 5 Pine St stamps Bart stamps
Project Operator
• Example:
Person Name,Address(Person)
Id Name Address Hobby Name Address
8
sid bid day
R1
22 101 10/10/96
58 103 11/12/96
S1
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0 sid sname rating age
28 yuppy 9 35.0
S2 31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Union, Intersection, Set-Difference
• union: The result of this operation, denoted by R U
S, is a relation that includes all tuples that are
either in R or in S or in both R and S. Duplicate
tuples are eliminated.
• intersection: The result of this operation, denoted
by R n S, is a relation that includes all tuples that
are in both R and S.
• set difference(or MINUS):The result of this
operation, denoted by R - S, is a relation that
includes all tuples that are in R but not in S.
Union, Intersection, Set-Difference
sid sname rating age
• All of these operations take two 22 dustin 7 45.0
input relations, which must be 31 lubber 8 55.5
union-compatible: 58 rusty 10 35.0
– Same number of fields. 44 guppy 5 35.0
28 yuppy 9 35.0
– `Corresponding’ fields
S1 S2
have the same type.
• What is the schema of result? sid sname rating age
sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1 S2 S1 S2
Cross-Product
• Each row of S1 is paired with each row of R1.
• Result schema has one field per field of S1 and
R1, with field names `inherited’ if possible.
– C=AXB
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Cont..
• Notice that both UNION and INTERSECTION
are commutative operations; that is,R U S=SU
R, and RnS=SnR
• Both UNION and INTERSECTION can be
treated as n-ary operations applicable to any
number of relations because both are
associative operations;that is,RU (S U T)=(RU
S) U T, and (RnS)nT=Rn(S nT)
• The MINUS operation is not commutative;that
is, in general,R-S#S-R
Relational calculus
24
The differences b/n them
• Relational Algebra (RA) and Relational Calculus (RC) are formal
languages for the database relational model while
• SQL is the practical language in the database relational model.
• In these formal languages a conceptual database model is expressed in
mathematical terms and notations while in the practical language –
SQL, the mathematical expressions of the functionality and transaction
of the database operations are implemented physically.
• Each language, that RA, RC, and SQL have their own notations to
express their notations.
• RA Notations – Procedural Expression Language
• RC Notation – Non Procedural Expression Language
• SQL is what is called a declarative language or non procedural , meaning
that you only need to specify what needs to be accomplished (such as a
query or insert) and not how to do it; the DBMS determines and
performs internally the step by step operation needed to obtain the
result.
Cont..
• For more about SQL
Read your lab class handout
d !
En