Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT 

Similar presentations


Presentation on theme: "Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT "— Presentation transcript:

1 Relational Algebra Indra Budi

2 Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT  or . l Binary Operations à Set operations:  UNION   INTERSECTION  F DIFFERENCE –  CARTESIAN PRODUCT  à JOIN operations  What is Relational Algebra? Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)

3 Fakultas Ilmu Komputer UI 3 Relational Algebra R  Sunion R  Sintersection R \ Sset difference R  SCartesian product  A1, A2,..., An (R)projection  F (R)selection R Snatural join R   Stheta-join R  Sdivision  [ A 1 B 1,.., An Bn ]rename

4 Fakultas Ilmu Komputer UI 4 Selection Selects the Set of Tuples (Rows) From a Relation, Which Satisfy a Selection Condition General Form  selection condition> (R) R is a Relation Selection condition is a Boolean Expression on the Attributes of R Resulting 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 5 ENOENAMETITLE E1J. DoeElect. Eng E6L. ChuElect. Eng.  TITLE='Elect. Eng.' (EMP) ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE='Elect. Eng.’ OR TITLE=‘Mech.Eng’ (EMP) Selection Example

6 Fakultas Ilmu Komputer UI 6 Another Selection Example A S C null W B null

7 Fakultas Ilmu Komputer UI 7 A SELECT Condition is a Boolean Expression Form F 1  F 2  F q (Q>=1), Where F i (I=1,…,q) are Atomic Boolean Expressions of the Form a  c or a  b, 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 8 Select certain Columns (Attributes) Specified in an Attribute List X From a Relation R General Form  ( R ) R is a Relation Attribute-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 9 Projection Example  PNO,BUDGET (PROJ) PNOBUDGET P P P P P PROJ PNOBUDGET P P P P PNAME P Instrumentation Database Develop. CAD/CAM Maintenance CAD/CAM

10 Fakultas Ilmu Komputer UI 10 Other Projection Examples

11 Fakultas Ilmu Komputer UI 11 Relational 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 12 ENOENAMETITLE E1J. DoeElect. Eng. E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP  TITLE (PROJ) Elect.Eng Syst.Anal Mec.Eng Programmer TITLE Characteristics 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 13 Selection with Projection Example

14 Fakultas Ilmu Komputer UI 14 Renaming The RENAME operator gives a new schema to a relation. R1 := RENAME R1(A1,…,A n ) (R2) makes R1 be a relation with attributes A1,…,A n and the same tuples as R2. Simplified notation: R1(A1,…,A n ) := R2. Other use (  ) notation R2  R1(A1,…,A n )

15 Fakultas Ilmu Komputer UI 15 Sequences of Assignments Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 JOIN C R2 can be written: R4 := R1 * R2 R3 := SELECT C (R4) OR R4  R1 * R2 R3  SELECT C (R4)

16 Fakultas Ilmu Komputer UI 16 General Form R  SR  S where R, S are Relations Result contains Tuples from both R and S Duplications are Removed The 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 17 Union Compatibility Two Relations R 1 (A 1, A 2,..., A n ) and R 2 (B 1, B 2,..., B n ) are said Union-compatible If and Only If They Have The Same Number of Attributes The Domains of Corresponding Attributes are Compatible, i.e., Dom(A i )=dom(B i ) for I=1, 2,..., N Names Do Not Have to be Same! For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible The Resulting Relation for Relational Set Operations Has the Same Attribute Names as the First Operand Relation R 1 (by Convention)

18 Fakultas Ilmu Komputer UI 18 General Form R – S where R and S are Relations Result 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 Difference

19 Fakultas Ilmu Komputer UI 19 General Form R  S where R and S are Relations Result 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 Intersection

20 Fakultas Ilmu Komputer UI 20 Union, Difference, Intersection Examples What are these Other Three Result Tables?

21 Fakultas Ilmu Komputer UI 21 Given Relations R of Degree k 1 and Cardinality card 1 S of Degree k 2 and Cardinality card 2 Cartesian Product R  S R  S is a Relation of Degree ( k 1 + k 2 ) and Consists of Tuples of Degree ( k 1 + k 2 ) 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 card 1 * card 2 What is One Problem with Cartesian Product w.r.t. the Result Set? Cartesian Product

22 Fakultas Ilmu Komputer UI 22 Cartesian Product: Example ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 F E f1 f5 e1 e2 ABCE a1 a2 a3 b1 b4 c3 c5 c7 e1 e2 e1 e2 e1 e2 RS R S F f1 f5 f1 f5 f1 f5 

23 Fakultas Ilmu Komputer UI 23 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 24 Cartesian Product Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng Syst. Anal Mech. Eng Programmer24000

25 Fakultas Ilmu Komputer UI 25 The Join Operation Used to combine related tuples from two relation into single tuples with some condition Cartesian 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 26 General Form R  S where R, S are Relations,  is a Boolean Expression, called a Join Condition. A Derivative of Cartesian Product R  S =   ( R  S ) R(A 1, A 2,..., A m, B 1, B 2,..., B n ) is the Resulting Schema of a  -Join over R 1 and R 2 : R 1 (A 1, A 2,..., A m )  R 2 (B 1, B 2,..., B n ) Theta Join (  -Join)

27 Fakultas Ilmu Komputer UI 27 A  -Join Condition is a Boolean Expression of the form F 1  1  F 2  2  n-1  F q (q>=1), where F i (i=1,…,q) are Atomic Boolean Expressions of the form A i  B j, A i, B j are Attributes of R 1 and R 2 Respectively  is one of the Algorithmic Comparison Operators =, <>, >, =, <= The Operator  i ( i=1,…,n-1 ) is Either a Logical AND operator  or a logical OR operator   -Join Condition

28 Fakultas Ilmu Komputer UI 28  -Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng Syst. Anal Mech. Eng Programmer24000 ENOENAME E1J. Doe M. SmithE2 E3A. Lee E4J. Miller E5B. Casey E6L. Chu E7R. Davis E8J. Jones TITLE Elect. Eng. Analyst Mech. Eng. Programmer Syst. Anal. Elect. Eng. Mech. Eng. Syst. Anal. SAL 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 29 Other Types of Join Equi-join (EQUIJOIN) The  Expression only Contains one or more Equality Comparisons Involving Attributes from R 1 and R 2 Natural Join Denoted as R S Special Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and S By 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 30 Examples ABC a1 a2 a3 b1 b1 b4 c3 c5 c7 BE b1 b5 e1 e2 RS R R.B=S.B S AR.B a1 a2 b1 CE c3 c5 e1 S.B b1 EQUIJOIN AR.BCE a1 a2 b1 c3 c5 e1 R S Natural Join

31 Fakultas Ilmu Komputer UI 31 Natural Join Natural Join Combines Relations on Attributes with the Same Names STUDENT(S#, SN, CITY, ) 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 Operators Example Query 2: “ print all students info (courses taken and grades) ”  S#, SN, CITY, , C#, G (  STUDENT.S# = S-C.S# (STUDENT  S-C))

32 Fakultas Ilmu Komputer UI 32 Natural Join Example ENOENAMETITLE E1J. DoeElect. Eng E2M. SmithSyst. Anal. E3A. LeeMech. Eng. E4J. MillerProgrammer E5B. CaseySyst. Anal. E6L. ChuElect. Eng. E7R. DavisMech. Eng. E8J. JonesSyst. Anal. EMP TITLESAL Elect. Eng Syst. Anal Mech. Eng Programmer60000 ENOENAMEE.TITLE SAL E1J. DoeElect. Eng E2M. SmithSyst. Anal E3A. LeeMech. Eng. E8J. JonesSyst. Anal. EMP SAL E4J. MillerProgrammer E5B.CaseySyst.Anal E6L. ChuElect.Eng E7R.DavisMech.Eng

33 Fakultas Ilmu Komputer UI 33 Another Natural Join Example

34 Fakultas Ilmu Komputer UI 34 Yet Another Natural Join Example

35 Fakultas Ilmu Komputer UI 35 Given Relations R(T,U) of degree r S(U) of degree s The Division of R by S, R ÷ S Results is a Relation of Degree ( r  s ) Consists of all ( r  s )-tuples t such that for all s -tuples u in S, the tuple tu is in R. Quotient (Division)

36 Fakultas Ilmu Komputer UI 36 Division Example ENO E3 R ÷ S ENOPNOPNAME E1P1Instrumentation BUDGET E2P1Instrumentation E2P2Database Develop E3P1Instrumentation E3P4Maintenance E4P2Instrumentation E5P2Instrumentation E6P4 E7P3CAD/CAM E8P3CAD/CAM R Maintenance S PNO PNAME BUDGET P1 Instrumentation P4Maintenance Find the employees who work for both project P1 and project P4?

37 Fakultas Ilmu Komputer UI 37 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-C   C# (  Instructor=‘Smith’ (C)))

38 Fakultas Ilmu Komputer UI 38 Relational Algebra Fundamental OperatorsDerivable from the fundamental operators Selection Projection Union Difference Cartesian Product n Intersection n Join, Equi-join, Natural Join

39 Fakultas Ilmu Komputer UI 39 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 Set Any 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 Operations

40 Fakultas Ilmu Komputer UI 40 Fundamental Operators Selection Projection Union Set Difference Cartesian Product Relational Algebra: Summary Form:  Result>  Relation (s) Relation Additional Operators Join Intersection Quotient (Division) Union Compatibility Same Degree Corresponding Attributes Defined Over the Same Domain

41 Fakultas Ilmu Komputer UI 41 References Elmasri & Navathe, “Fundamental of Database Systems 3 rd ed”, Addison-Wesley, 2000


Download ppt "Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT "

Similar presentations


Ads by Google