0% found this document useful (0 votes)
32 views

Lecture 7

Relational algebra is a theoretical language used to manipulate relations through operations without changing the original relations. The operations include unary operations like projection and selection that work on a single relation, and binary operations like various join, set, and other operations that work on two relations. Relations are closed under these relational algebra operations.

Uploaded by

Endash Haile
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lecture 7

Relational algebra is a theoretical language used to manipulate relations through operations without changing the original relations. The operations include unary operations like projection and selection that work on a single relation, and binary operations like various join, set, and other operations that work on two relations. Relations are closed under these relational algebra operations.

Uploaded by

Endash Haile
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Relational Algebra

 It is a theoretical language with operations that


work on one or more relations to define another
relation without changing the original relation (s).

 Relations are closed under relational algebra


operations.
Relational Algebra
 Unary operations
 Projection
 The projection operation works on a single relation R and
defines a relation that contains a vertical subset of R,
extracting the values of specified attributes and
eliminating duplicates.
 Note: All the examples in these slides refer to the
relations you were given on students records db unless
otherwise specified.
  a1,a2,a3,…an(R)
• Where a1,a2,…,an are attributes and R is a relation.
 Examples:
1. Produce a list of all students showing their name, father’s
name, and sex.
2. List the rooms by showing their type, number, campus, and
capacity.
Relational Algebra
 Selection (or Restriction)
 The selection operation works on a single relation R and
defines a relation that contains only those tuples of R
that satisfy the specified condition (predicate).
  condition(R)
 Examples:
1. List all students in the department of BAIS.
2. List all female instructors whose department is BAIS, but are
not living in Addis Ababa.
3. List the lecture halls at the main campus with capacity
between 150 and 200 students together with all computer
labs.
4. List the instructors whose date of birth is not entered.
5. Display the identification numbers and names of the
female instructors with the rank of Full Professor.
Relational Algebra
 Binary Operations
 Set Operations
 Union
• The Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R and S,
duplicate tuples being eliminated. R and S must be
union-compatible.
• R∪S
• Examples:
1. Take two relations A and B of your choice that are
union compatible and perform the union.
2. List the Kebeles where Students or Instructors of
AAUCC live in Addis Ababa.
Relational Algebra
 Intersection
• The Intersection operation defines a relation consisting
of a set of all tuples that are in both R and S. R and S
must be union-compatible.
• R∩S
• Examples:
1. Take two relations A and B of your choice that are
union compatible and perform the intersection.
2. List the Kebeles where Students and Instructors of
AAUCC live in Addis Ababa.
Relational Algebra
 Set difference
• The set difference operation defines a relation consisting
of the tuples that are in relation R, but not in S. R and S
must be union-compatible.

• R–S

• Examples:
1. Take two relations A and B of your choice that are
union compatible and perform A – B and also B – A.
2. List the Kebeles where Students but not Instructors
of AAUCC live in Addis Ababa.
Relational Algebra
 Cartesian product
• The Cartesian product operation defines a relation that is
the concatenation of every tuple of relation R with every
tuple of relation S.
• RxS
• Examples:
1. Perform this operation on two relations, say A and B,
of your choice.
2. Register each student in the department of BAIS for
every course given by the department of BAIS.
3. List the names and departments of all students who
registered for the course BAIS 322 in the second
semester of the academic year 2002.
Relational Algebra
Join Operations
 Theta join (θ)
• The Theta join operation defines a relation that contains tuples
satisfying the predicate F from the Cartesian product of R and
S. The predicate F is of the form R.ai θ S.bi where θ may be
one of the comparison operators (<,≤,>,≥,=, ≠). If the
comparison operator is the = sign, then the join operation is
called Equijoin.

• R ⋈F S

• Note that: R ⋈F S = F (R x S)

• Examples:
 Take your own example
 Use example 3 of the previous slide.
Relational Algebra
Natural join
• The Natural join is an Equijoin of the two relations R and
S over all common attributes x. One occurrence of each
common attribute is eliminated from the result.

• R⋈S

• Examples:
 Take your own example.
 Use the 2nd example of the previous slide.
Relational Algebra
 Outer join
• The (left) Outer join is a join in which tuples form R that do no
have matching values in the common attributes of S are also
included in the result relation. Missing values in the second
relation are set to null.

• R⋊S
• Examples:
1. Take your own example.
2. Produce a status report on registration of students.
3. Produce a status report on advisory assignment of
instructors.
4. Produce a status report on room schedules.
• Assignment:
 Provide the definition for right outer join.
Relational Algebra
Semijoin
• The Semijoin operation defines a relation that contains
the tuples of R that participate in the join of R with S.

• R ⊳F S

• Examples:
 Take your own example.
 List complete details of all courses which are
registered by students of the department of
Accounting in the 1st semester of the academic year
2008/9.
Relational Algebra
 Division Operation
 Division
• Assume relation R is defined over the attribute set A and
relation S is defined over the attribute set B such that B
⊆ A. Let C = A – B. Then the Division operation R ⌯ S
is defined as a relation over the attributes C that
consists the set of tuples from R that match the
combination of every tuple in S.
• R⌯S
• Examples:
1. Take two relations R and S for which R ⌯ S can be
performed and get R ⌯ S.
2. Produce the Identification numbers of all students
who have registered for all courses given by the
department of BAIS.
Exercises
 Refer to Student Records DB schema in specifying the following
queries using Relational Algebra.
1. List the names of courses on which student Abebe Bekele got “A”
grades.
2. What is the name of the instructor who advised student Abebe Bekele
in the 1st semester of the academic year 2004?
3. What grade did student Abebe Bekele got on the course Database
Systems I?
4. List the names of the courses for which student Abebe Bekele
registered in the 2nd semester of the academic year 2008.
5. List the rooms where the course Introduction to management is
scheduled in the 3rd period on Wednesdays of the current semester.
6. Show the names and departments of the instructors who are not
assigned to advise students in the current semester.
7. List the names, ranks, and date of employment of all instructors who
are qualified to teach the courses E-Commerce or Database Systems I
and II.
8. Display the course name, course code, and the name of the
department offering the course for each course that is prerequisite to
the course BAIS 411.

You might also like