0% found this document useful (0 votes)
46 views34 pages

2 Relational Algebra

The document summarizes relational algebra operations that can be used to query and manipulate data in a relational database. It describes basic operations like selection, projection, and cross product. Selection retrieves rows in a relation based on a condition. Projection returns only specified attributes. Cross product pairs all rows from one relation with all rows from another relation. The document uses an example teacher-student database to illustrate how these operations can be combined to form complex queries and retrieve answers from the database.

Uploaded by

Song Kheang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views34 pages

2 Relational Algebra

The document summarizes relational algebra operations that can be used to query and manipulate data in a relational database. It describes basic operations like selection, projection, and cross product. Selection retrieves rows in a relation based on a condition. Projection returns only specified attributes. Cross product pairs all rows from one relation with all rows from another relation. The document uses an example teacher-student database to illustrate how these operations can be combined to form complex queries and retrieve answers from the database.

Uploaded by

Song Kheang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 2: Relational algebra

Prepared by : CHHUO Vanna

1 2011 - 2012
Planning
2

 Introduction
 Example database
 Relational algebra
 Basic operation
 Additional operation
 Complex query
Introduction
3

 DBMS stores data and allows data manipulation.


Program/users

Query Language

Data
DBMS
model

Disk
Introduction
4

 Query language (QL): language used to retrieve


and manipulate data in database.
 Query language supported by RDBMS:
 Relational algebra
 Relational calculus
Example database
5
Teacher Department
TID Tname DoB Degree Field #Dname Dname Description
1 Sok Dara 01/01/85 Master Math Math Math …
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
Electronic …
3 Sao Piseth 05/08/70 PhD Biology Environment
Mechanic …
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
Environment …
5 Van Dany 08/12/87 Engineer hydropower Environment

Children StudentCourse Course Student


#TID ChildName DoB #StudentID #CID CID Cname StudentID Sname DoB

1 Dan 01/01/2010 1 C1 C1 Math 1 Chea Nary 01/01/2000

1 Paul 01/02/2011 1 C2 C2 Physic 2 Sok Palin 02/01/2001


2 Heng 09/08/2000 3 Yi Sophea 03/09/1999
2 C2
3 Lundi 08/08/1995
2 C1 4 Van Dany 08/12/1987
4 Dimanche 07/12/1987
3 C2
Example database
6

 Find out teachers who get PhD degree.


 Find out teachers who teach in a department which has the same
name as his/her skill.
 Show name and skill of each teachers.
 What if every student studies all courses.
 Show all teachers in the city.
 Show teachers who work for ITC and don’t work for URPP.
 Show teachers who work for both ITC and URPP.
 Show all teachers and their children.
 Find out teachers who are younger than a child of other teacher.
 Find out students who study all courses.
How can we get answer?
Relational algebra
7

 Operation based query language

Operation
Basic operations:
•Selection
•Projection
•Cross Product
•Union
•Different
•Rename
Additional operations:
•Intersection
•Join
•Division

Complex query and query tree


Relational algebra
8

 Take one or two relation as argument and return a


relation as output
 Operation based query language
 Complex query is formed by combining basic
operations
 Each query describe procedure for computing
answer
Basic operation
9

 Selection( ): select a subset of rows


from a relation; retrieve certain rows
R: a relation; a, b: attribute of R
: comparison operation (=, != or <>, >, <, >=, <=)
Teacher TID Tname DoB Degree Field #Dname
1 Sok Dara 01/01/85 Master Math Math
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment

TID Tname DoB Degree Field #Dname


2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
Basic operation
10

 Selection
Teacher
TID Tname DoB Degree Field #Dname
1 Sok Dara 01/01/85 Master Math Math
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment

TID Tname DoB Degree Field #Dname


1 Sok Dara 01/01/85 Master Math Math
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
Basic operation
11

 Selection
 Find out teacher who was born after 1985

Teacher
TID Tname DoB Degree Field #Dnam
1 Sok Dara 1985 Master Math Math
2 Sam Sambath 1980 PhD Mechanic of fluid Mechanic
3 Sao Piseth 1970 PhD Biology Environment
4 Tao Pisey 1965 Engineer Electronic Electronic
5 Van Dany 1987 Engineer hydropower Environment
Basic operation
12

 Projection ( ): return only the wanted


attributes and their values; retrieve certain columns

Tname Field
Sok Dara Math
Sam Sambath Mechanic of fluid
Sao Piseth Biology
Tao Pisey Electronic
Van Dany hydropower
Basic operation
13

Degree
Master
PhD
Engineer
Basic operation
14
14
Teacher
TID Tname DoB Degree Field #Dnam
1 Sok Dara 01/01/85 Master Math Math
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment

Write RA expression for the following question:


 Which department does “Sok Dara” work in?
Basic operation
15

 Cross Product (RXS): each row of R is paired with


each row of S.
 R(a1, a2, a3); S(b1, b2, b3) SC.StudentID SC.CID C.CID C.Cname

R X S = T (a1, a2, a3, b1, b2, b3) 1 C1 C1 Math


1 C1 C2 Physic
StudentCourse X Course 1 C2 C1 Math
1 C2 C2 Physic
StudentCourse=SC Course=C
#StudentID #CID CID Cname
10 2 C2 C1 Math
2 C2 C2 Physic
1 C1 2 C1 Math
= 2 C1 C1 Math
1 C2
X
C2 Physic 2 C1 C2 Physic
5 2 C2 3 C2 C1 Math
2 C1 3 C2 C2 Physic
3 C2
Basic operation
16

 Cross Product (RXS)


 List all students’ ID and name of course they enrolled
SC.StudentID SC.CID C.CID C.Cname
SC X C SC.StudentID SC.CID C.CID C.Cname
1 C1 C1 Math
1 C1 C1 Math
1 C1 C2 Physic
1 C2 C2 Physic
1 C2 C1 Math
2 C2 C2 Physic
1 C2 C2 Physic
2 C1 C1 Math
2 C2 C1 Math
3 C2 C2 Physic
2 C2 C2 Physic
2 C1 C1 Math SC.StudentID C.Cname
2 C1 C2 Physic 1 Math
3 C2 C1 Math 1 Physic
3 C2 C2 Physic 2 Physic
2 Math
3 Physic
Basic operation
17

 Cross Product(RxS)
 List all students’ name and the name of the course they
enroll in.
Course
CID Cname
StudentCourse
C1 Math
#StudentID #CID
C2 Physic
1 C1
Student 1 C2
StudentID Sname 2 C2
1 Chea Nary 2 C1
2 Sok Polin 3 C2
3 Yi Sophea
Basic operation
18

 Union (R U S) : return all rows occur in R or S


R and S must be union compatible:
 Schema of R and S have the same degree
 Attributes in the same order must have the same data type
(domain)
 Set Different (R - S): Return rows occur in R but not
in S. R and S must be union compatible.
Basic operation
19

Teacher
TID Tname DoB Degree Field #Dnam
1 Sok Dara 01/01/85 Master Math Math
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment

Teacher1
TID Tname DoB Degree Field #Dnam
1 Sok Dara 01/01/85 Master Math Math
12 Nicole John 01/02/87 Master BD CS
3 Sao Piseth 05/08/70 PhD Biology Environment
14 Michael Jam 14/07/81 Engineer Electronic Electronic
Basic operation
20

 Teacher U Teacher1
TID Tname DoB Degree Field #Dnam
1 Sok Dara 01/01/85 Master Math Math
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
3 Sao Piseth 05/08/70 PhD Biology Environment
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment
12 Nicole John 01/02/87 Master BD CS
14 Michael Jam 14/07/81 Engineer Electronic Electronic

 Teacher – Teacher1
TID Tname DoB Degree Field #Dnam
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic
4 Tao Pisey 14/07/65 Engineer Electronic Electronic
5 Van Dany 08/12/87 Engineer hydropower Environment
Additional operation
21

 Intersection ( ): Return all rows that occur in both


R and S. R and S must be Union compatible.
 Teacher Teacher1

TID Tname DoB Degree Field Dnam


1 Sok Dara 01/01/85 Master Math Math
3 Sao Piseth 05/08/70 PhD Biology Environment
Additional operation
22

 Put the right operation for the result

R1 R2 =
R1 R2 =

R1 R2 =
Additional operation
23

 Join: used to combine information from many tables.


 can be expressed using Cross Product, selection and
projection
 Condition join or Theta join ( )=( ):
selection of cross product based on condition c.
 R(a1, a2, a3); S(b1, b2, b3)
= T (a1, a2, a3, b1, b2, b3)

=
Additional operation
24

Teacher (T) Children (C)


TID Tname DoB Degree Field #Dnam #TID ChildName DoB
1 Sok Dara 01/01/1985 Master Math Math 1 Dan 01/01/2010
2 Sam Sambath 01/02/1980 PhD Mechanic of fluid Mechanic 1 Paul 01/02/2011
3 Sao Piseth 05/08/1970 PhD Biology Environment 2 Heng 09/08/2000
4 Tao Pisey 14/07/1965 Engineer Electronic Electronic 3 Lundi 08/08/1995

5 Van Dany 08/12/1987 Engineer hydropower Environment 4 Dimanche 07/12/1987

T.TID T.Tname T.DoB T.Degree T.Field T.Dnam C.TID C.ChildName C.DoB


5 Van Dany 08/12/87 Engineer hydropower Environment 4 Dimanche 07/12/1987
Additional operation
25

 Equi-Join: special case of condition join where only


equalities are allowed in condition.
 R(a1, x, a3); S(b1, b2, y)
= T (a1, x, a3, b1, b2)
Additional operation
26

 Equi-Join
Teacher (T) Children (C)
TID Tname DoB Degree Field #Dname #TID ChildName DoB
1 Sok Dara 01/01/1985 Master Math Math 1 Dan 01/01/2010
2 Sam Sambath 01/02/1980 PhD Mechanic of fluid Mechanic 1 Paul 01/02/2011
3 Sao Piseth 05/08/1970 PhD Biology Environment 2 Heng 09/08/2000
4 Tao Pisey 14/07/1965 Engineer Electronic Electronic 3 Lundi 08/08/1995

5 Van Dany 08/12/1987 Engineer hydropower Environment 4 Dimanche 07/12/1987

T.TID T.Tname T.DoB T.Degree T.Field T.Dname C.ChildName C.DoB


1 Sok Dara 01/01/85 Master Math Math Dan 01/01/2010
1 Sok Dara 01/01/85 Master Math Math Paul 01/02/2011
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic Heng 09/08/2000
3 Sao Piseth 05/08/70 PhD Biology Environment Lundi 08/08/1995
4 Tao Pisey 14/07/65 Engineer Electronic Electronic Dimanche 07/12/1987
Additional operation
27

 Natural join ( ): equi-join on all common


attributes (attributes with the same name).
 R(a1, a2, x, y); S(b1, x, y, b2, b3)
= T(a1, a2, x, y, b1, b2, b3)
 x, y: are common attributes of R and S
StudentID CID Cname
#StudentID #CID 1 C1 Math
CID Cname
1 C1 1 C2 Physic
C1 Math
1 C2 2 C2 Physic
C2 Physic
2 C2 2 C1 Math
2 C1 3 C2 Physic
3 C2
Additional operation
28

 Join
 List
all students’ name and the name of the course they
enroll in.
Course
CID Cname
StudentCourse
C1 Math
#StudentID #CID
C2 Physic
1 C1
Student 1 C2
StudentID Sname 2 C2
1 Chea Nary 2 C1
2 Sok Polin 3 C2
3 Yi Sophea
Additional operation
29

 Division (R/S or R S): used to express certain kind


of question like: “List students who enroll to all
courses”.
 Can be expressed using projection, cross product, and
different.
 R(x, y) and S(y)
R S=
 StudentCourse Course
What does the query really mean?
 (studentID, CID) CID = studentID
Additional operation
30
StudentCourse Course
#StudentID #CID CID Cname StudentID CID

1 C1 1 C1
C1 Math
1 C2 1 C2
C2 Physic
2 C2 2 C1

2 C1 2 C2

3 C2 3 C1

3 C2

 StudentCourse Course
=
StudentID

2
Additional operation
31

 Rename( ): used to rename attribute and relation.


 Solve the problem of attribute conflict in cross product
and join operation or self-join and self cross product
 R(a,a2,a3) and S(a, b2)

RxS = (R.a,R.a2, R.a3,S.a, S.b2)


 Rename attribute R.a to x, S.a to y, and the result of
RXS to T

=> RxS = T(x, a2, a3, y, b2)


Additional operation
32

TID Tname TDoB Degree Field Dnam ChildName CDoB


1 Sok Dara 01/01/85 Master Math Math Dan 01/01/2010
1 Sok Dara 01/01/85 Master Math Math Paul 01/02/2011
2 Sam Sambath 01/02/80 PhD Mechanic of fluid Mechanic Heng 09/08/2000
3 Sao Piseth 05/08/70 PhD Biology Environment Lundi 08/08/1995
4 Tao Pisey 14/07/65 Engineer Electronic Electronic Dimanche 07/12/1987
Complex query
33

 Complex query is composed of many operations


 Ex: List the name of teacher who was not a student of
the school
Reference
34

 R. Ramakrishnan et J. Gehrke Database


Management Systems. 2003 (troisième édition)
 Jean-Marc Petit(2011). Fondamentaux de la
modélisation des données. Département
Informatique-INSA de Lyon

You might also like