Professional Documents
Culture Documents
Relational Algebra
• Procedural language
• Six basic operators
— select
— project
— Union
— Intersection
— set difference
— Cartesian product
— rename
• The operators take one or more relations as inputs and give a new
relation as a result.
Select Operation
• Notation: (r)
p
• p is called the selection predicate
• Defined as:
p(r) = {t | t r and p(t)}
Where p is a formula in propositional calculus consisting of terms
connected by :
(and), (or), (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <.
• Example of selection:
branch-name=“Perryridge”(account)
Select Operation – Example 2 & 3
• Select Electrical Engineers from Employee Relation.
• Sol. EMP
TITLE='Elect. Eng.'(EMP)
ENO ENAME TITLE
ENO ENAME TITLE E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng E2 M. Smith Syst. Anal.
E6 L. Chu Elect. Eng. E3 A. Lee Mech. Eng.
E4 J. Miller Programmer
E5 B. Casey Syst. Anal.
E6 L. Chu Elect. Eng.
E7 R. Davis Mech. Eng.
E8 J. Jones Syst. Anal.
• Select Electrical or mechanical engineers from Employee Relation.
• Sol.
TITLE='Elect. Eng.’ TITLE=‘Mech.Eng’(EMP)
Select Operation – Example4
• Find the projects with budget less than equal to $200,000 & greater than
$200,000 from the relation PROJ using select operation. Define the
relations PROJ1 & PROJ2 based on Budget.
PROJ
PNO PNAME BUDGET LOC
PROJ1 PROJ2
Database
P1 Instrumentation 150000 Montreal P2 135000 New York
Develop.
P3 CAD/CAM 250000 New York
PROJ3
P.Query2
P.Query3
U.Query3
Select Operation – Example7 & 8
• Select the EMPLOYEE tuples whose department number is four.
Sol. DNO = 4 (EMPLOYEE)
• Sol.
Project Operation
• Notation:
A1, A2, …, Ak (r)
where A1, A2 are attribute names and r is a relation name.
• The result is defined as the relation of k columns obtained by erasing the
columns that are not listed
• Duplicate rows removed from result, since relations are sets
• E.g. To eliminate the branch-name attribute of account
account-number, balance (account)
Project Operation – Example2
• List each employee’s first and last name and salary from Employee
Relation. Company database schema
Sol. LNAME, FNAME,SALARY (EMPLOYEE)
Project Operation – Example3
• List each employee’s sex and salary from Employee Relation.
Sol. SEX,SALARY (EMPLOYEE) Company database schema
Project Example4
• Select PNO & BUDGET from the relation PROJ.
PROJ
PNO BUDGET
P1 150000
P2 135000
P3 250000
P4 310000
P5 500000
Selection with Projection Example
• List each employee’s first and last name and salary from Employee
Relation whose DNO is 5 from the employee relation.
• Sol.
Rno Name Age
1 A 20
2 B 21
3 A 19
Pie rno(Student)
123
Pie name(Student)
A, B
Pie rno,name(Student)
1A
2B
3A
Union Operation
• Notation: r s
• Defined as:
r s = {t | t r or t s}
• For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)
• E.g. to find all customers with either an account or a loan
customer-name (depositor) customer-name (borrower)
Union Operation: Example2
• Find STUDENT STRUCTOR.
• Sol:
Intersection Operation
Intersection Operation: Example1
• Find STUDENT STRUCTOR.
• Sol:
Set Difference Operation
• Notation r – s
• Defined as:
r – s = {t | t r and t s}
• Set differences must be taken between compatible relations.
— r and s must have the same arity
— attribute domains of r and s must be compatible
Set Difference Operation – Example2
• Find (a) STUDENT STRUCTOR
(b) INSTRUCTOR – STUDENT
a. Find the names of all employees who work for First Bank Corporation.
Sol. Πperson-name (σcompany-name = “First Bank Corporation” (works))
Example1: Queries contd.
b. Find the names, street address, and cities of residence of all employees
who work for First Bank Corporation and earn more than $10,000 per
annum.
Sol.
Natural-Join Operation
Notation: r s
• Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R S obtained as follows:
— Consider each pair of tuples tr from r and ts from s.
— If tr and ts have the same value on each of the attributes in R S, add
a tuple t to the result, where
– t has the same value as tr on r
– t has the same value as ts on s
Natural Join Operation – Example1
• Example1:
R = (A, B, C, D)
S = (E, B, D)
— Result schema = (A, B, C, D, E)
— r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Natural Join Operation – Example2
Natural Join Operation – Example3
Natural Join Operation – Example4