DBMS 7
DBMS 7
DBMS 7
DATABASE SYSTEMS
SELECT operation is used to select a subset of the tuples from a relation that
satisfy a selection condition. It is a filter that keeps only those tuples that
satisfy a qualifying condition – those satisfying the condition are selected
while others are discarded.
Example: To select the EMPLOYEE tuples whose department number is four
or those whose salary is greater than $30,000 the following notation is used:
σDNO = 4 (EMPLOYEE)
σSALARY > 30,000 (EMPLOYEE)
In general, the select operation is denoted by σ <selection condition>(R) where the
symbol σ (sigma) is used to denote the select operator, and the selection
condition is a Boolean expression specified on the attributes of relation R
π
The general form of the project operation is <attribute list>(R) where π
(pi) is the symbol used to represent the project operation and <attribute list>
is the desired list of attributes from the attributes of relation R.
The project operation removes any duplicate tuples, so the result of the
project operation is a set of tuples and hence a valid relation.
– π (π <list2> (R) ) = π
<list1> <list1> (R) as long as <list2> contains
the attributes in <list2>
- ρ S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B1,
…..Bn.
- ρ (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B1, …..Bn which does
The union operation produces the tuples that are in either RESULT1 or RESULT2
or both. The two operands must be “type compatible”.
Chapter 6-14 DBMS - Nguyen Thi Hau
Relational Algebra Operations From
Set Theory
• Type Compatibility
– The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn)
must have the same number of attributes, and
– the domains of corresponding attributes must be compatible;
that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n.
• INTERSECTION OPERATION
Example: The result of the intersection operation (figure below) includes only
those who are both students and instructors.
STUDENT ∩ INSTRUCTOR
Example: The figure shows the names of students who are not instructors, and
the names of instructors who are not students.
FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE)
EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)
EMP_DEPENDENTS ← EMPNAMES x DEPENDENT
α 1 α 2
α 2 β 3
β 3
Tính:
• R ∪ S, R S, R ∩ S
• R - S, S – R
• RxS
R A B C S D E
1 2 3 3 1
4 5 6 6 2
7 8 9
• R B<D S
• R C=D S
• R C=S.C S
• DIVISION Operation
– The division operation is applied to two relations
R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and hence Z
= X ∪ Y); that is, let Y be the set of attributes of R that are
not attributes of S.
1. Y = Z-X
2. T1 = π Y(R)
3. T2 = π Y((S × T1)-R)
4. T= T1-T2
R A B C D S A B
a1 b1 x1 y1 a1 b1
a2 b2 x1 y1 a2 b2
a1 b2 x1 y1
a2 b2 x1 y2
a1 b1 x1 y2
a1
a2
b1
b2
x2
x2
y1
y1
T=R÷S ?
a1 b1 x2 y2
a2 b3 x1 y2
a3 b1 x1 y1
R A B C D S A B
a1 b1 x1 y1 a1 b1
1. Y = {C,D }
a2 b2 x1 y1 a2 b2
a1 b2 x1 y1
2. T1 = π Y (R)
a2 b2 x1 y2
a1 b1 x1 y2
T1 C D
a1 b1 x2 y1
a2 b2 x2 y1 x1 y1
a1 b1 x2 y2 x1 y2
a2 b3 x3 y2 x2 y1
a3 b1 x1 y3 x2 y2
x3 y2
x1 y3
source: Bài giảng CSDL - Khoa CNTT 35
DIVISION Operation
1. Y = {C,D }
2. T1 = π Y (R)
R A B C D S A B 3. T2 = π Y (S × T1) - R)
a1 b1 x1 y1 a1 b1
A B C D
a2 b2 x1 y1 a2 b2
a1 b1 x1 y1
a1 b2 x1 y1
a1 b1 x1 y2
a2 b2 x1 y2
T1 C D a1 b1 x2 y1
a1 b1 x1 y2 x1 y1
a1 b1 x2 y2
a1 b1 x2 y1 x1 y2
a1 b1 x3 y2
a2 b2 x2 y1 x2 y1
a1 b1 x1 y3
a1 b1 x2 y2 x2 y2
a2 b2 x1 y1
a2 b3 x3 y2 x3 y2
a2 b2 x1 y2
a3 b1 x1 y3 x1 y3
a2 b2 x2 y1
a2 b2 x2 y2
a2 b2 x3 y2
R A B C D S A B 3. T2 = π Y ((S × T1) - R)
a1 b1 x1 y1 a1 b1
a2 b2 x1 y1 a2 b2
A B C D
a1 b2 x1 y1 T1 C D
a1 b1 x3 y2
a2 b2 x1 y2 x1 y1
a1 b1 x1 y3
a1 b1 x1 y2 x1 y2
a2 b2 x2 y2
a1 b1 x2 y1 x2 y1
a2 b2 x3 y2
x2 y2
a2 b2 x2 y1
x3 y2 a2 b2 x1 y3
a1 b1 x2 y2
x1 y3
a2 b3 x3 y2 4. T = T1-T2
a3 b1 x1 y3 T2 C D T C D
x3 y2 x1 y1
x1 y3 x1 y2
x2 y2
source: Bài giảng CSDL - Khoa CNTT
x2 y1 37
Exercise 3
R A B C D E S D E
α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1
β a γ a 1
β a γ b 3
γ a γ a 1
a b 1
R÷S
γ γ
γ a β b 1
Hanoi_PROJS ← σPlocation=‘Hanoi’(PROJECT)
CONTR_DEPTS ← (Hanoi_PROJS Dnum=DnumberDEPARTMENT)
PROJ_DEPT_MGRS ← (CONTR_DEPTS Mgr_ssn=Ssn EMPLOYEE)
– In NATURAL JOIN tuples without a matching (or related) tuple are eliminated
from the join result. Tuples with null in the join attributes are also eliminated.
This amounts to loss of information.
– A set of operations, called outer joins, can be used when we want to keep all the
tuples in R, or all those in S, or all those in both relations in the result of the join,
regardless of whether or not they have matching tuples in the other relation.
– The left outer join operation keeps every tuple in the first or left relation R in
R S; if no matching tuple is found in S, then the attributes of S in the join
result are filled or “padded” with null values.
– A similar operation, right outer join, keeps every tuple in the second or right
relation S in the result of R S.
– A third operation, full outer join, denoted by keeps all tuples in both the
left and the right relations when no matching tuples are found, padding them
with null values as needed.