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

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
The Relational Algebra
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Outline  Introduction  Background  Distributed DBMS Architecture  Distributed Database Design  Semantic Data Control ➠ View Management ➠ Data Security.
Relational Model dww-database system.
1 Relational Algebra* and Tuple Calculus * The slides in this lecture are adapted from slides used in Standford's CS145 course.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Algebra Fall 2014.
Relational Algebra Tim Kaddoura CS157A. Introduction  Relational query languages are languages for describing queries on a relational database  Three.
1 Relational Algebra. Motivation Write a Java program Translate it into a program in assembly language Execute the assembly language program As a rough.
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Distributed DBMSPage 6. 1© 1998 M. Tamer Özsu & Patrick Valduriez Outline Introduction Background Distributed DBMS Architecture Distributed Database Design.
Relational Model The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4 th edition,
Relational Model Indra Budi Fakultas Ilmu Komputer UI 2 Essentials of Relational Approach The Relational Model of Data is Based on.
Review Indra Budi Fakultas Ilmu Komputer UI 2 Database Introduction Database vs File Processing Main purpose of database Database Actors.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra Basic Operations Algebra of Bags.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Relational Algebra - Chapter (7th ed )
CSE 4701 Chapter 6-1 Chapter 6 6e & 7 5e : Relational Algebra – Part 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE 4701 Chapter 3-1 Chapter 3 6e & 7 5e : Relational Model – Part 1 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
The Relational Algebra The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4 th.
CSE 4701 Chapter 3-1 Chap 3,6 6e & 7 5e : Relational Model Parts 1 & 2 Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University.
Relational Algebra (Chapter 7)
The Relational Algebra and Calculus
From Relational Algebra to SQL CS 157B Enrique Tang.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Relational Algebra Operators
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Relational Algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Database Group, Georgia Tech 1 Relational Model The Relational Model - theoretical foundation.
1 Introduction to Database Systems, CS420 Relational Algebra.
LECTURE THREE RELATIONAL ALGEBRA 11. Objectives  Meaning of the term relational completeness.  How to form queries in relational algebra. 22Relational.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Basic Operations Algebra of Bags
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
CPSC-310 Database Systems
COMP3017 Advanced Databases
COP4710 Database Systems Relational Algebra.
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Relational Algebra - Part 1
ER Modeling Exercise Consider a set of courses, both at grad and undergrad level. Each course has at least one section. Each section is taught by only.
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
The Relational Algebra and Relational Calculus
Basic Operations Algebra of Bags
Data Manipulation using Relational Algebra
Chapter 2: Intro to Relational Model
Presentation transcript:

Relational Algebra Indra Budi

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)

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

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)

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

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

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

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

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

Fakultas Ilmu Komputer UI 10 Other Projection Examples

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)

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)

Fakultas Ilmu Komputer UI 13 Selection with Projection Example

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 )

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)

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

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)

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

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

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

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

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 

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 )

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

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

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)

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

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.

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

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

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))

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

Fakultas Ilmu Komputer UI 33 Another Natural Join Example

Fakultas Ilmu Komputer UI 34 Yet Another Natural Join Example

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)

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?

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)))

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

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

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

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