Relational Algebra Indra Budi [email protected]. Fakultas Ilmu Komputer UI 2 n Basic Relational...
-
Upload
everett-summerhays -
Category
Documents
-
view
220 -
download
1
Transcript of Relational Algebra Indra Budi [email protected]. Fakultas Ilmu Komputer UI 2 n Basic Relational...
Relational Algebra
Indra [email protected]
2 Fakultas Ilmu Komputer UI
Basic Relational Operations: Unary Operations
SELECT PROJECT or .
Binary Operations Set operations:
UNION INTERSECTION DIFFERENCE –
CARTESIAN PRODUCT JOIN operations
What is Relational Algebra?What is Relational Algebra?
Relational Algebra is a Procedural ParadigmYou Need to Tell What/How to Construct the ResultConsists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)
3 Fakultas Ilmu Komputer UI
Relational Algebra
R S unionR S intersectionR \ S set differenceR S Cartesian
product
A1, A2, ..., An (R) projection
F (R) selection
R S natural join
R S theta-joinRS division[A1 B1,.., An Bn] rename
4 Fakultas Ilmu Komputer UI
SelectionSelection
Selects the Set of Tuples (Rows) From a Relation, Which Satisfy a Selection ConditionGeneral Form selection condition> (R)
R is a Relation Selection condition is a Boolean Expression on the Attributes of RResulting Relation Has the Same Schema as R
Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)
5 Fakultas Ilmu Komputer UI
ENO ENAME TITLE
E1 J. Doe Elect. EngE6 L. Chu Elect. Eng.
TITLE='Elect. Eng.'(EMP)ENO ENAME TITLE
E1 J. Doe Elect. Eng.
E2 M. Smith Syst. Anal.
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.
EMP
TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’(EMP)
Selection ExampleSelection Example
6 Fakultas Ilmu Komputer UI
Another Selection ExampleAnother Selection Example
ASCnullWBnullnull
7 Fakultas Ilmu Komputer UI
A SELECT Condition is a Boolean Expression
Form F1 F2 Fq (Q>=1), Where
Fi (I=1,…,q) are Atomic Boolean Expressions of the Form
ac or ab, a, b are Attributes of R and c is a Constant.
The Operator is one of the Arithmetic Comparison Operators: <, >, =, <>The Operator is one of the Logical Operators: , , ¬Nesting: ( )
Selection Condition
8 Fakultas Ilmu Komputer UI
Select certain Columns (Attributes) Specified in an Attribute List X From a Relation R
General Form <attribute-list>(R)R is a RelationAttribute-list is a Subset of the Attributes of R Over Which the Projection is Performed
Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples)
Projection
9 Fakultas Ilmu Komputer UI
Projection Example
PNO,BUDGET(PROJ)
PNO BUDGET
P1 150000
P2 135000
P3 250000P4 310000P5 500000
PROJ
PNO BUDGET
P2 135000
P3 250000
P4 310000P5 500000
PNAME
P1 150000Instrumentation
Database Develop.
CAD/CAM
MaintenanceCAD/CAM
10 Fakultas Ilmu Komputer UI
Other Projection Examples
11 Fakultas Ilmu Komputer UI
Relational Algebra ExpressionRelational Algebra Expression
Several Operations can be Combined to form a Relational Algebra Expression (query)Example: Retrieve all Customers over age 60?
Method 1: CNAME, ADDRESS, AGE ( AGE>60(CUSTOMER) )
Method 2: Senior-CUST(C#, Addr, Age)
= CNAME, ADDRESS, AGE ( AGE>60(CUSTOMER) )
Method 3: CNAME, ADDRESS, AGE (C) where
C AGE>60(CUSTOMER)
12 Fakultas Ilmu Komputer UI
ENO ENAME TITLE
E1 J. Doe Elect. Eng.
E2 M. Smith Syst. Anal.
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.
EMP TITLE(PROJ)
Elect.EngSyst.AnalMec.EngProgrammer
TITLE
Characteristics of ProjectionCharacteristics of Projection
The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation
Why?Projection Must Maintain a Mathematical Set (No Duplicate Elements)
13 Fakultas Ilmu Komputer UI
Selection with Projection Example
14 Fakultas Ilmu Komputer UI
RenamingRenaming
The RENAME operator gives a new schema to a relation.R1 := RENAMER1(A1,…,An)(R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2.Simplified notation: R1(A1,…,An) := R2.Other use () notation
R2 R1(A1,…,An)
15 Fakultas Ilmu Komputer UI
Sequences of AssignmentsSequences of Assignments
Create temporary relation names.Renaming can be implied by giving relations a list of attributes.Example: R3 := R1 JOINC R2 can be written:R4 := R1 * R2R3 := SELECTC (R4)
ORR4 R1 * R2R3 SELECTC (R4)
16 Fakultas Ilmu Komputer UI
General FormR S
where R, S are Relations Result contains Tuples from both R and SDuplications are RemovedThe two Operands R, S should be union-compatible
Example: “find students registered for course C1 or
C3”
s#(CNO=‘C1’ (S-C))
s#(
CNO=‘C3’ (S-C))
Union
17 Fakultas Ilmu Komputer UI
Union CompatibilityUnion Compatibility
Two Relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said Union-compatible If and Only If They Have
The Same Number of AttributesThe Domains of Corresponding Attributes are Compatible, i.e., Dom(Ai)=dom(Bi) for I=1, 2, ..., NNames Do Not Have to be Same!
For Relational Union and Difference Operations, the Operand Relations Must Be Union CompatibleThe Resulting Relation for Relational Set Operations
Has the Same Attribute Names as the First Operand Relation R1 (by Convention)
18 Fakultas Ilmu Komputer UI
General Form
R – S
where R and S are RelationsResult Contains all tuples that are in R, but not in S.
R – S <> S – R
Again, there Must be Compatibility
Example “Find the students who registered course C1
but not C3”
s#(CNO=‘C1’
(S-C)) –s#
(CNO=‘C3’
(S-C))
Set DifferenceSet Difference
19 Fakultas Ilmu Komputer UI
General Form
R S
where R and S are RelationsResult Contains all Tuples that are in R and S.
R S = R – (R – S)Again, there Must be Compatibility
Example “find the students who registered for both C1
and C3”
s#(CNO=‘C1’
(S-C)) s#
(CNO=‘C3’
(S-C))
Set IntersectionSet Intersection
20 Fakultas Ilmu Komputer UI
Union, Difference, Intersection ExamplesUnion, Difference, Intersection Examples
What are these OtherThree Result Tables?
21 Fakultas Ilmu Komputer UI
Given RelationsR of Degree k1 and Cardinality card1
S of Degree k2 and Cardinality card2
Cartesian Product R S
is a Relation of Degree (k1+ k2) and Consists of Tuples of Degree (k1+ k2) where each Tuple is a Concatenation of one Tuple of R with one Tuple of S
Cardinality of the Result of the Cartesian Product R S is card1 * card2
What is One Problem with Cartesian Product w.r.t. the Result Set?
Cartesian Product
22 Fakultas Ilmu Komputer UI
Cartesian Product: Example Cartesian Product: Example
A B C
a1a2a3
b1b1b4
c3c5c7
FE
f1f5
e1e2
A B C E
a1a1a2a2a3a3
b1b1b1b1b4b4
c3c3c5c5c7c7
e1e2e1e2e1e2
R S
R S F
f1f5f1f5f1f5
23 Fakultas Ilmu Komputer UI
Given R(A1, …,An) and S(B1,…,Bm), the result of a Cartesian product R S is a relation of schema
R’(A1, …, An, B1, …, Bm).Example
“Get a list containing (S#, C#) for all students who live in Storrs but are not registered for the database course”
Cartesian Product
(S#(
city=‘Storrs’(STUDENT))
C#
(CNAME=‘Database’
(COURSE)))– S#,
C#(S-C)
24 Fakultas Ilmu Komputer UI
Cartesian Product Example
ENO ENAME EMP.TITLE SAL.TITLE SAL
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
E1 J. Doe Elect. Eng.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
E2 M. Smith Syst. Anal.
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
E8 J. Jones Syst. Anal.
EMP SAL
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
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.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
25 Fakultas Ilmu Komputer UI
The Join OperationThe Join Operation
Used to combine related tuples from two relation into single tuples with some conditionCartesian product all combination of tuples are included in the result, meanwhile in the join operation, only combination of tuples satisfying the join condition appear in the result
26 Fakultas Ilmu Komputer UI
General FormR S
whereR, S are Relations, is a Boolean Expression, called a Join Condition.
A Derivative of Cartesian ProductR S = (R S)
R(A1, A2, ..., Am, B1, B2, ..., Bn) is the Resulting Schema of a -Join over R1 and R2:
R1(A1, A2, ..., Am) R2 (B1, B2, ..., Bn)
Theta Join (-Join)Theta Join (-Join)
27 Fakultas Ilmu Komputer UI
A -Join Condition is a Boolean Expression of the form F1 1F2 2n-1Fq (q>=1), where
Fi (i=1,…,q) are Atomic Boolean Expressions of the form
Ai Bj,
Ai, Bj are Attributes of R1 and R2 Respectively
is one of the Algorithmic Comparison Operators =, <>, >, <. >=, <= The Operatori (i=1,…,n-1) is Either a Logical AND operator or a logical OR operator
-Join Condition
28 Fakultas Ilmu Komputer UI
-Join Example
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
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.
EMP
TITLE SAL
SAL
Elect. Eng. 40000
Syst. Anal. 34000
Mech. Eng. 27000
Programmer 24000
ENO ENAME
E1 J. Doe
M. SmithE2
E3 A. Lee
E4 J. Miller
E5 B. Casey
E6 L. Chu
E7 R. DavisE8 J. Jones
TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
SAL
40000
34000
27000
24000
34000
40000
2700034000
EMP E.TITLE=SAL.TITLE SAL
SAL.TITLE
Elect. Eng.
Analyst
Mech. Eng.
Programmer
Syst. Anal.
Elect. Eng.
Mech. Eng.Syst. Anal.
29 Fakultas Ilmu Komputer UI
Other Types of Join
Equi-join (EQUIJOIN)The Expression only Contains one or more Equality Comparisons Involving Attributes from R1 and R2
Natural JoinDenoted as R SSpecial Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and SBy Common, it means that each Join Attribute in A has not only Compatible Domains but also the Same Name in both Relations R and S
30 Fakultas Ilmu Komputer UI
ExamplesExamples
A B C
a1a2a3
b1b1b4
c3c5c7
B E
b1b5
e1e2
R S
R R.B=S.B S
A R.B
a1a2
b1b1
C E
c3c5
e1e1
S.B
b1b1
EQUIJOIN
A R.B C E
a1a2
b1b1
c3c5
e1e1
R SNatural Join
31 Fakultas Ilmu Komputer UI
Natural Join
Natural Join Combines Relations on Attributes with the Same Names
STUDENT(S#, SN, CITY, Email) S-C(S#, C#, G)
Example Query 1: “list of students with complete course grade
info” STUDENT S-C
All Natural Joins can be Expressed by a Combination of Primitive OperatorsExample Query 2:
“print all students info (courses taken and grades)”
S#, SN, CITY, Email, C#, G
(STUDENT.S# = S-C.S#
(STUDENT S-C))
32 Fakultas Ilmu Komputer UI
Natural Join Example
ENO ENAME TITLE
E1 J. Doe Elect. Eng
E2 M. Smith Syst. Anal.
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.
EMP
TITLE SAL
SAL
Elect. Eng. 70000
Syst. Anal. 80000
Mech. Eng. 56000
Programmer 60000
ENO ENAME E.TITLE SAL
E1 J. Doe Elect. Eng. 70000
E2 M. Smith Syst. Anal. 80000
56000
80000
E3 A. Lee Mech. Eng.
E8 J. Jones Syst. Anal.
EMP SAL
60000E4 J. Miller Programmer
80000E5 B.Casey Syst.Anal
70000E6 L. Chu Elect.Eng
56000E7 R.Davis Mech.Eng
33 Fakultas Ilmu Komputer UI
Another Natural Join Example
34 Fakultas Ilmu Komputer UI
Yet Another Natural Join Example
1455
35 Fakultas Ilmu Komputer UI
Given RelationsR(T,U) of degree rS(U) of degree s
The Division of R by S, R ÷ S
Results is a Relation of Degree (rs) Consists of all (rs)-tuples t such that for
all s-tuples u in S, the tuple tu is in R.
Quotient (Division)
36 Fakultas Ilmu Komputer UI
Division Example
ENO
E3
R ÷ S
ENO PNO PNAME
E1 P1 Instrumentation 150000
BUDGET
E2 P1 Instrumentation 150000E2 P2 Database Develop. 135000E3 P1 InstrumentationE3 P4 MaintenanceE4 P2 InstrumentationE5 P2 InstrumentationE6 P4E7 P3 CAD/CAME8 P3 CAD/CAM
310000150000150000310000250000250000
R
Maintenance
150000
S
PNO PNAME BUDGET
P1 Instrumentation 150000P4 Maintenance 310000
Find the employees who work for both project P1 and project P4?
37 Fakultas Ilmu Komputer UI
Division: Another Example
“list the S# of students that have taken all of the courses listed in S-C”
S#
(S-C)C#
(S-C)
“list the S# of students who have taken all of the courses taught by instructor Smith”
S#
(S-CC#
(Instructor=‘Smith’(C)))
38 Fakultas Ilmu Komputer UI
Relational Algebra
Fundamental OperatorsDerivable from the fundamental operators
SelectionProjectionUnionDifferenceCartesian Product
Intersection Join, Equi-join, Natural Join
39 Fakultas Ilmu Komputer UI
A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If
Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in SetAny Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set
Important Concepts: The Set of Algebra Operations { , , , –, } is a
Complete Set of Relational Algebra Operations
Any Query Language Equivalent to These Five Operations is Called Relationally Complete
All Relational Algebra OperationsAll Relational Algebra Operations
40 Fakultas Ilmu Komputer UI
Fundamental Operators
SelectionProjectionUnionSet DifferenceCartesian Product
Relational Algebra: Summary
Form:
<Operator><Operand(s)> Result>
Relation (s) Relation
Additional OperatorsJoinIntersectionQuotient (Division)
Union CompatibilitySame DegreeCorresponding Attributes Defined Over the Same Domain
41 Fakultas Ilmu Komputer UI
ReferencesReferences
Elmasri & Navathe, “Fundamental of Database Systems 3rd ed”, Addison-Wesley, 2000