The Relational Algebra and Calculus

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

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
Relational Calculus
– Tuple Relational Calculus
– Domain Relational Calculus
Example Database Application (COMPANY)
Overview of the QBE language (appendix D)

Slide 2
Relational Algebra Overview

Relational algebra is the basic set of operations for the


relational model
These operations enable a user to specify basic
retrieval requests (or queries)
The result of an operation is a new relation, which may
have been formed from one or more input relations

Slide 3
Relational Algebra Overview (continued)

The algebra operations thus produce new relations


– These can be further manipulated using operations of the same algebra

A sequence 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
query (or retrieval request)

Slide 4
Slide 5
The following
query results
refer to this
database
state

Slide 6
Unary Relational Operations: SELECT

The SELECT operation (denoted by  (sigma)) is used to select


a subset of the tuples from a relation based on a selection
condition.
– Keeps only those tuples that satisfy the qualifying condition
Examples:
– Select the EMPLOYEEs whose department # is 4:

 DNO = 4 (EMPLOYEE)
– Select the EMPLOYEEs whose salary > $30,000:

 SALARY > 30,000 (EMPLOYEE)

Slide 7
SELECT Operation Properties

The SELECT operation  <selection condition>(R) produces a relation S that


has the same schema (same attributes) as R
SELECT  is commutative:
 <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))

Because of commutativity, a sequence of SELECT operations may be


applied in any order:
<cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))
A cascade of SELECT operations may be replaced by a single
selection with a conjunction of all the conditions:
<cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))

Slide 8
Unary Relational Operations: PROJECT

PROJECT Operation is denoted by  (pi)


This operation keeps certain columns (attributes) from a
relation and discards the other columns.
Example: To list each employee’s first and last name and
salary, the following is used:

LNAME, FNAME,SALARY(EMPLOYEE)

Slide 9
PROJECT (cont.)

The general form of the project operation is:

<attribute list>(R)
<attribute list> is the desired list of attributes from relation
R.
The project operation removes any duplicate tuples
– This is because the result of the project operation is still a relation, that is, a set
of tuples

Slide 10
PROJECT Operation Properties

The number of tuples in the result of projection <list>(R) is


always less or equal to the number of tuples in R
– If the list of attributes includes a key of R, then the number of tuples in the result
of PROJECT is equal to the number of tuples in R

PROJECT is not commutative


–  <list1> ( <list2> (R) ) =  <list1> (R) as long as <list2> contains the attributes in <list1>

Slide 11
Examples of applying SELECT and PROJECT
operations

Slide 12
Single expression versus sequence of
relational operations (Example)
Task: To retrieve the first name, last name, and salary of all
employees who work in department number 5.
Solution 1: A single relational algebra expression:
– FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

Solution 2: a sequence of operations, giving a name to


each intermediate relation:
– DEP5_EMPS   DNO=5(EMPLOYEE)
– RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)

Slide 13
Example of applying multiple operations and
RENAME
LNAME, FNAME,SALARY(Dno=5 (EMPLOYEE))

Temp = Dno=5 (EMPLOYEE)

R (First_name, Last_name, Salary)


= LNAME, FNAME,SALARY(Temp)
* Notice the implied renaming
Slide 14
Important Note
The Select and Project operations produce relations.

Dno ( SSN = 123456789 (EMPLOYEE)) results in


{(5)}.
– The schema of the relation is (Dno).
– The result relation has only one tuple.
– It is still a relation, not a number.

Slide 15
Unary Relational Operations: RENAME

The RENAME operator is denoted by  (rho)


In some cases, we may want to rename the attributes of a
relation or the relation name or both
– Useful when a query requires multiple
operations
– Necessary in some cases (see JOIN
operation later)

Slide 16
Example of applying multiple operations and
RENAME
Temp = Dno=5 (EMPLOYEE)

R = (First_name, Last_name Salary) (LNAME, FNAME,SALARY(Temp))


* Notice the explicit renaming

Slide 17
Relational Algebra Operations from
Set Theory
Type Compatibility of operands is required for the binary set
operation UNION , (also for INTERSECTION , and
SET DIFFERENCE –, see next slides)
R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are type
compatible if:
– they have the same number of attributes, and
– the domains of corresponding attributes are type compatible
(i.e. dom(Ai)=dom(Bi) for i=1, 2, ..., n).
The resulting relation for R1R2 (also for R1R2, or R1–R2)
has the same attribute names as the first operand relation
R1 (by convention)

Slide 18
Example Uses of UNION

To retrieve the SSNs of all employees who either work in


department 5 (RESULT1 below) or directly supervise an
employee who works in department 5 (RESULT2 below)
We can use the UNION operation as follows:
DEP5_EMPS  DNO=5 (EMPLOYEE)
RESULT1  SSN(DEP5_EMPS)
RESULT2(SSN)  SUPERSSN(DEP5_EMPS)
RESULT  RESULT1  RESULT2
The union operation produces the tuples that are in either
RESULT1 or RESULT2 or both

Slide 19
Union

(two type compatible relations)

Intersection STU - INS

INS - STU

Slide 20
Troubles with Attribute Names
We define the domain Date (mm/dd/yyyy).
R1 (Birth_date, SSN), where Birth_date is of domain Date
R2 (Death_date, SSN), where Death_date is also of
domain Date.
R3 = R1  R2 will have the attribute list (Birth_date, SSN),
which sounds weird.
R3 is best named as
R3(BorD_date, SSN) = R1  R2
Or more formally,
R3 =  (BorD_date, SSN) (R1  R2)

Slide 21
Exercises
Give the SSNs of those managers have dependent(s).

Give the SSNs non-manager employees

Give the SSNs of employees who are both supervisors and


supervisees

Slide 22
Properties of Set Operations
Notice that both union and intersection are commutative;
that is
– R  S = S  R, and R  S = S  R

Both union and intersection are associative operations; that


is
– R  (S  T) = (R  S)  T
– (R  S)  T = R  (S  T)

The minus operation is not commutative; that is, in general


– R–S≠S–R

Slide 23
Relational Algebra Operations from Set
Theory: CARTESIAN PRODUCT
CARTESIAN (or CROSS) PRODUCT Operation
– This operation is used to combine tuples from two relations
in a combinatorial fashion.
– Denoted by R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm)
– Result is a relation Q with degree n + m attributes:
• Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that order.
– The resulting relation state has one tuple for each
combination of tuples—one from R and one from S.
– Hence, if R has nR tuples (denoted as |R| = nR ), and S has
nS tuples, then R x S will have nR * nS tuples.
– The two operands do NOT have to be "type compatible”

Slide 24
CARTESIAN PRODUCT (cont.)

Example: To find the dependents’ names of all female


employees
– FEMALE_EMPS   SEX=’F’(EMPLOYEE)
– EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS  EMPNAMES x DEPENDENT
– ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)
– RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

Slide 25
Example of applying
CARTESIAN PRODUCT

Slide 26
More Exercises

List the names of those departments that have at least one


female employee whose salary  $100,000.

List the Names and SSNs of the managers of the above


departments.

Slide 27
JOIN Operator ( )
The sequence of CARTESIAN PRODECT followed by
SELECT is used quite commonly to identify and select
related tuples from two relations
A special operation, called JOIN combines this sequence
into a single operation
The general form of a join operation on two relations R(A1,
A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S
where R and S can be any relations that result from
general relational algebra expressions.

Slide 28
Exercise

Use the JOIN operator to find the dependents’ names of all


female employees
– FEMALE_EMPS   SEX=’F’(EMPLOYEE)
– EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS  EMPNAMES x DEPENDENT
– ACTUAL_DEPS   SSN=ESSN(EMP_DEPENDENTS)
– RESULT   FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPS)

Slide 29
Exercises
List the names of department managers.
Use Cartesian Product and Select

Use Join

Slide 30
Binary Relational Operations: EQUIJOIN
EQUIJOIN Operation
The most common use of join involves join conditions with
equality comparisons only
Such a join, where the only comparison operator used is =,
is called an EQUIJOIN.
– In the result of an EQUIJOIN we always have one or more pairs of attributes
(whose names need not be identical) that have identical values in every tuple.
– The JOIN seen in the previous example was an EQUIJOIN.

Slide 31
NATURAL JOIN Operations

Another variation of JOIN called NATURAL JOIN —


denoted by * — was created to get rid of the second
(superfluous) attribute in an EQUIJOIN condition.
– because one of each pair of attributes with identical values is superfluous
The standard definition of natural join requires that the two
join attributes, or each pair of corresponding join
attributes, have the same name in both relations
If this is not the case, a renaming operation is applied first.

Slide 32
Exercise

Use the Natural JOIN operator to find the dependents’


names of all female employees
– FEMALE_EMPS   SEX=’F’(EMPLOYEE)
– EMPNAMES   FNAME, LNAME, SSN (FEMALE_EMPS)

Slide 33
Results of
Natural JOINs

Slide 34
NATURAL JOIN: Example 2
Because of the same attribute name Dnumber, Natural Join
can be applied directly to DEPARTMENT and
DEP_LOCATION
– DEPT_LOCS  DEPARTMENT *
DEPT_LOCATIONS

Slide 35
NATURAL JOIN (contd.)

Notice that all pairs of identical attribute names


are compared
Example: Q  R(A,B,C,D) * S(C,D,E)
– The implicit join condition includes each pair
of attributes with the same name, “AND”ed
together:
• R.C=S.C AND R.D=S.D
– Result keeps only one attribute of each such
pair:
• Q(A,B,C,D,E)

Slide 36
Slide 37
Exercise
List names and SSNs of those EMPLOYEE who work on
some project(s) located in Houston.

Slide 38
Exercise
List the names and birth dates of all female dependents
born after 1980.

Slide 39
Exercise
List the names of all employees who earns
salary  $10,000 but does not have a
supervisor.

Slide 40
Exercises
List the names of all employees who earns
salary  $10,000 but does not supervise
anyone.

Slide 41
Representation of Relations

• Student(idNo, name, cgpa, admissionYear)


OR
• Student
IdNO name cgpa admissionYear
Relational Representation of
Strong Entity Sets
• Create a relation Ri for each strong entity set Ei.
• Attributes of Ri: simple attributes and simple components
of composite attributes of entity set Ei
• ƒMulti-valued attributes of entities are dealt with separately.

• The key of Ei will also be the primary key of Ri.


• The primary key can be referred to by other relations via
foreign keys in them to capture relationships as we see
later.
Relational Representation of
Weak Entity Sets
• Create R for weak entity E’
• ƒAttributes of R will be
1. Attributes of the weak entity set E' and
2. Primary key attributes of the identifying owner entity E.
3. These attributes will also be a foreign key in R' referring to the relation
corresponding to E.
• Primary Key of R: partial key of E' + Key of E.
• If there is a weak entity type E2 whose owner is also a
weak entity type E1, then E1 should be mapped before
E2 to determine its primary key first.
Example

Mapping to relational schema:

Primary key of section: {sectionNo, courseId)


Relational Representation of
Multi-valued Attributes
• One relation R for each multi-valued attribute Mi.
• Attributes of R:
1. multi-valued attribute Mi
2. primary key attribute of entity / relationship set for which M i is an attribute.

Primary Key of R: multi-valued attribute of R + Key of E.

emailId is multi-valued attribute.


Mapping One-to-One Binary
Relationships
• Let S and T be entity sets in relationship R and S', T' be
the relations corresponding to these entity sets.
• Choose an entity set which has total participation if there
is one (says, S)
• include the primary key of T' as a foreign key in S' referring to relation T’.
• include all simple attributes (and simple components of composite attributes) of
R as attributes of S’.
• If there is total participation on both sides, both entity
sets can be merged into one relation.
Example

• Bank gives at most one student loan to a student. All


student loans must be given to students only.

STUDENTLOAN
loanNo dateIssued Amount interest Institute StudentId

STUDENT
Institute Stdid Fathername
Mapping One-to-Many/Many-
to-One Binary Relationships
• Let S be the participating entity on the many-side and T
the other entity. Let S' and T' be the corresponding
relations.
• ƒInclude primary key of T' as foreign key in S‘.
• ƒInclude any simple attribute (and simple components of
composite attributes) of reltionship set as attributes of S‘.
Example
Mapping Many-to- Many
Binary Relationships
• Make a separate relation T for this relationship R
between entity sets E1 and E2.
• Let R1 and R2 be the relations corresponding to E1 and
E2 .
• Include primary key attributes of R1 and R2 as foreign
keys in T. Their combination is the primary key in T.
Example

Primary key of enrollment relation is {rollNo, courseId}


Mapping Recursive
Relationships
• Make a relation T for the participating entity set E ( this
might already be existing) and
• one relation for recursive relationship R.
• ƒAttributes of R will be
1. It key has two attributes both takes the values from T. Both have similar but not
same.
2. Any other attribute of relationship
• Example:
Mapping n-ary Relationshps

• For each n-ary relationship type R, where n > 2, create a


new relation S to represent R.
• Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity types.
• Include simple attributes of R as attributes in S.
• The primary key of S is usually a combination of all the
foreign keys that reference the relations representing the
participating entity types.
• If cardinality of a participating entity is 1, then S should not include the foreign
key referencing that relation in the primary key of S.
Example

1 M

SUPPLY
Sname Part_no Proj_name quantity

• Primary key is {Part_no, Proj_name}


• Sname, Part_no, Proj_name are foreign keys to respective relations.
•Multivalued attribute
• Relation and foreign key
ER Model &
•Value set Relational Model
• Domain
• Entity type
•Key attribute
• Entity relation
• Primary
• 1:1 (or secondary)
or 1:N relationship type key
• Foreign key (or relationship relation)
• M:N relationship type
• Relationship relation and two foreign keys
• n-ary relationship type
• Relationship relation and n foreign keys
• Simple attribute
• Attribute
Mapping Super/Sub Entity
Sets
• Multi-relation approach
• Approach 1:
• Make relation for super type and one for each subtype. Include PK of super type,
as FK in subtype relations.
• Works for all {total/partial specialization} and {disjoint/overlapping}
• Approach2:
• Make no relation for super type. Make separate relations for all sub types.
Repeat super type attributes in each relation.
• Works only for total specialization and disjoint cases. For partial specialization,
some entities may be lost. For overlapping, attributes will be redundantly stored
in two or more relations.
Example
Example
Mapping Super/Sub Entity
Sets
• Single-relation approach
• Good if subtypes have only few attributes.
• Approach 1:
• Make a single relation and include all attributes of super type and subtype entity
sets.
• Include as many boolean attributes as there are subtypes.
• Works for disjoint and overlapping cases.
• Approach2:
• Make a single relation and include all attributes of super type and subtype entity
sets.
• Include a single attribute called ‘discriminator’ whose value indicates to which
subtype that tuple belongs to.
• Works only for disjoint case. If there are many attributes in a subtype, they may
all be null.
Example
Vehicle_ Pric License_plat CarFlg Max_sp No_of_p TruckFlg No_of_a
id e e_no eed assenger xles Tonnage
s

VEHICLE
Example
Relational Schema
Acknowledgements

1. Pictures from Elmarsi R, & Navathe S B, Fundamental


of Database System, 5e, Pearson Education, 2008 are
used.

You might also like