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.

Slides:



Advertisements
Similar presentations
The Relational Algebra
Advertisements

พีชคณิตแบบสัมพันธ์ (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.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
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.
Relational Algebra Indra Budi Fakultas Ilmu Komputer UI 2 n Basic Relational Operations: l Unary Operations  SELECT   PROJECT 
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.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
The Relational Data Model 1.Relational Model Concepts 2.Characteristics of Relations 3.Relational Integrity Constraints 3.1Key Constraints 3.2Entity Integrity.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Relational Model The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4 th edition,
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.
Lecture 15: Relational Algebra
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
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.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
Relational Algebra – Part 2
The Relational Algebra and Calculus
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
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 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 )
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CSC271 Database Systems Lecture # 7. Summary: Previous Lecture  Relational keys  Integrity constraints  Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Dr. Mohamed Hegazi1 The Relational Algebra and Relational Calculus.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 CSE 480: Database Systems Lecture 16: 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
Relational Algebra National University of Computer and Emerging Sciences Lecture # 6 June 30,2012.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
Ritu CHaturvedi Some figures are adapted from T. COnnolly
Database Systems Chapter 6
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
COMP3017 Advanced Databases
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
Chapter 4 The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Presentation transcript:

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 of Connecticut 191 Auditorium Road, Box U-155 Storrs, CT (860) n A large portion of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech. n The remainder of these slides have been adapted from the AWL web site for the textbook.

CSE 4701 Chapter 6-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? n Relational Algebra is a Procedural Paradigm You Need to Tell What/How to Construct the Result n Consists of a Set of Operators Which, When Applied to Relations, Yield Relations (Closed Algebra)

CSE 4701 Chapter 6-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

CSE 4701 Chapter 6-4 Selection n Selects the Tuples (Rows) From a Relation, Which Satisfy a Selection Condition n Selection Produces a Horizontal Subset of the Operand Relation General Form  F (R) l R is a Relation l F is a Boolean Expression on the Attributes of R l Resulting Relation Has the Same Schema as R n Select Finds and Retrieves All Relevant Rows (Tuples) of Table/Relation R which Includes ALL of its Columns (Attributes)

CSE 4701 Chapter 6-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

CSE 4701 Chapter 6-6 Another Selection Example A S C null W B null

CSE 4701 Chapter 6-7 n A SELECT Condition is a Boolean Expression Form F 1  F 2  F q (Q>=1), Where l F i (I=1,…,q) are Atomic Boolean Expressions of the Form a  c or a  b, l 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: , , ¬ l Nesting: ( ) Selection Condition

CSE 4701 Chapter 6-8 n Extract Only Certain Columns (Attributes) Specified in an Attribute List X From a Relation R n Produces a New Relation, which is a Vertical Subset of the Operand Relation R n The Schema (Columns) of the Resulting Relation is X General Form  X (R) l R is a Relation l X is a Subset of the Attributes of R Over Which the Projection is Performed n Project Retrieves Specified Columns of Table/Relation R which Includes ALL of its Rows (Tuples) Projection

CSE 4701 Chapter 6-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

CSE 4701 Chapter 6-10 Other Projection Examples

CSE 4701 Chapter 6-11 Relational Algebra Expression n Several Operations can be Combined to form a Relational Algebra Expression (query) n Example: Retrieve all Customers over age 60? Method 1:  CNAME, ADDRESS, AGE (  AGE>60 (CUSTOMER) ) l Method 2: Senior-CUST(C#, Addr, Age) =  CNAME, ADDRESS, AGE (  AGE>60 (CUSTOMER) ) Method 3:  CNAME, ADDRESS, AGE (C) where  C  AGE>60 (CUSTOMER)

CSE 4701 Chapter 6-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 n The PROJECT Operation Eliminates Duplicate Tuples in the Resulting Relation l Why? l Projection Must Maintain a Mathematical Set (No Duplicate Elements)

CSE 4701 Chapter 6-13 Selection with Projection Example

CSE 4701 Chapter 6-14 n General Form R  S where R, S are Relations l Result contains Tuples from both R and S l Duplications are Removed l The two Operands R, S should be union-compatible (type-compatible w.r.t Columns/Attributes) n Example: “find students registered for course C1 or C3”  s# (  CNO=‘C1’ (S-C))  s# (  CNO=‘C3’ (S-C)) Union

CSE 4701 Chapter 6-15 Union Compatibility n 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 l The Same Number of Attributes l The Domains of Corresponding Attributes are Compatible, i.e., Dom(A i )=dom(B i ) for I=1, 2,..., N l Names Do Not Have to be Same! n For Relational Union and Difference Operations, the Operand Relations Must Be Union Compatible n The Resulting Relation for Relational Set Operations l Has the Same Attribute Names as the First Operand Relation R 1 (by Convention)

CSE 4701 Chapter 6-16 n General Form R – S where R and S are Relations l Result Contains all tuples that are in R, but not in S. l R – S <> S – R l Again, there Must be Compatibility n Example “Find the students who registered course C1 but not C3”   s# (  CNO=‘C1’ (S-C)) –   s# (  CNO=‘C3’ (S-C)) Set Difference

CSE 4701 Chapter 6-17 n General Form R  S where R and S are Relations l Result Contains all Tuples that are in R and S. R  S = R – (R – S) l Again, there Must be Compatibility n Example “find the students who registered for both C1 and C3”   s# (  CNO=‘C1’ (S-C))    s# (  CNO=‘C3’ (S-C)) Set Intersection

CSE 4701 Chapter 6-18 Union, Difference, Intersection Examples What are these Other Three Result Tables?

CSE 4701 Chapter 6-19 n Given Relations l R of Degree k 1 and Cardinality card 1 l S of Degree k 2 and Cardinality card 2 n Cartesian Product 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 n What is One Problem with Cartesian Product w.r.t. the Result Set? Cartesian Product

CSE 4701 Chapter 6-20 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 

CSE 4701 Chapter 6-21 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). n 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 )

CSE 4701 Chapter 6-22 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

CSE 4701 Chapter 6-23 n General Form R  S where l R, S are Relations, l F is a Boolean Expression, called a Join Condition. n 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)

CSE 4701 Chapter 6-24 A  -Join Condition is a Boolean Expression of the form F 1  1  F 2  2  n-1  F q (q>=1), where l F i (i=1,…,q) are Atomic Boolean Expressions of the form A i  B j, l 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

CSE 4701 Chapter 6-25  -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.

CSE 4701 Chapter 6-26 Other Types of Join n Equi-join (EQUIJOIN) The  Expression only Contains one or more Equality Comparisons Involving Attributes from R 1 and R 2 n Natural Join Denoted as R S l Special Equi-join of Two Relations R and S Over a Set of Attributes Common to both R and S l 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

CSE 4701 Chapter 6-27 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

CSE 4701 Chapter 6-28 Natural Join n Natural Join Combines Relations on Attributes with the Same Names l STUDENT(S#, SN, CITY, ) l S-C(S#, C#, G) n Example Query 1: “list of students with complete course grade info” STUDENT S-C n All Natural Joins can be Expressed by a Combination of Primitive Operators n Example Query 2: “print all students info (courses taken and grades)”  S#, SN, CITY, , C#, G (  STUDENT.S# = S-C.S# (STUDENT  S-C))

CSE 4701 Chapter 6-29 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

CSE 4701 Chapter 6-30 Another Natural Join Example

CSE 4701 Chapter 6-31 Yet Another Natural Join Example

CSE 4701 Chapter 6-32 n Given Relations l R(T,U) of degree r l S(U) of degree s n 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)

CSE 4701 Chapter 6-33 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?

CSE 4701 Chapter 6-34 Division: Another Example n “list the S# of students that have taken all of the courses listed in S-C”  S# (S-C)   C# (S-C) n “list the S# of students who have taken all of the courses taught by instructor Smith”  S# (S-C   C# (  Instructor=‘Smith’ (C)))

CSE 4701 Chapter 6-35 Relational Algebra Fundamental OperatorsDerivable from the fundamental operators l Selection l Projection l Union l Difference l Cartesian Product n Intersection n Join, Equi-join, Natural Join

CSE 4701 Chapter 6-36 n A Set of Relational Algebra Operations Is Called a Complete Set, If and Only If l Any Relational Algebra Operator in the Set Cannot be Derived in Terms of a Sequence of Others in Set l Any Relational Algebra Operator Not in the Set Can Be Derived in Terms of a Sequence of Only the Operators in the Set n 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

CSE 4701 Chapter 6-37 n Fundamental Operators l Selection l Projection l Union l Set Difference l Cartesian Product Relational Algebra: Summary Form:  Result>  Relation (s) Relation n Additional Operators l Join l Intersection l Quotient (Division) n Union Compatibility l Same Degree l Corresponding Attributes Defined Over the Same Domain

CSE 4701 Chapter 6-38 Semi-Join General form: R F S R is Target Relation, S is the Source Relation n Semi-join l A Form of Join where the Result Contains only those Tuples of the Target Relation, which Participate in the Join with the Source Relation n Benefit: Decreases the Number of Tuples that need to be Handled in the Join R F S =   (R F S)   (R) F   (S) = R F   (S) l where A is a set of Attributes of R, B is a set of attributes of S

CSE 4701 Chapter 6-39 Semi-Join Example BCD b2 b3 b4 b5 b6 b7 b8 b9 b10 S A B R b1 b3 b4 a1 a2 a3 a4. R   SAL R  B S) = c1 c2 c3 c4 d9 d8 d7 d9 d8 d7 d5 d4 d9 AB b3 b4 a3 a4.

CSE 4701 Chapter 6-40 Additional DB Operations n Additional Operations, Needed for Database Applications, but were Not Part of the Original Relational Algebra, Include: l OUTER JOIN l OUTER UNION l Aggregate Functions and Grouping

CSE 4701 Chapter 6-41 n Definition l LEFT OUTER JOIN: R 1 F R 2 lets every Tuple in R 1 Appear in the result. l RIGHT OUTER JOIN: R 1 F R 2 lets every Tuple in R 2 Appear in the result. l FULL OUTER JOIN: R 1 F R 2 lets every Tuple in R 1 or R 2 Appear in the result n Examples - See Textbook Chapter 7 Outer Joins

CSE 4701 Chapter 6-42 n Commonly Used Aggregate Functions: l SUM, COUNT, AVERAGE, MIN, MAX l They Often Applied to sets of Values or sets of Tuples in Database Applications [ ] f (R) l The Grouping Attributes are Optional Aggregate Functions

CSE 4701 Chapter 6-43 n Example 1: l Retrieve the average salary of all employees (no grouping): R(AVG_SAL) =  AVG(SALARY) (EMPLOYEE) n Example 2: l For each department, retrieve the department number, the number of employees, and the average salary (in the department): R(D#,Num_E,AVG_SAL) = GROUP DNO (  DNO, COUNT(ENO), AVG(SALARY) (EMPLOYEE)) l DNO is called the grouping attribute in this example Examples of Aggregate Functions

CSE 4701 Chapter 6-44 Concluding Remarks n What have we Seen in Chapter 7? l Basic Concepts of Relational Model Including Relation/Table, Tuple/Row, Attribute/Column, Domain/Attribute Value l Concept of SK, CK, PK, and FK for Identification and Referential Integrity l Integrity Constraints as they Relate to Referential Dependencies Check for Modification Operations l Relational Algebra Operations that Allow Querying via Selection, Project, Join, and Other Set Operations n Overall, Relational Theory is Basis for SQL, Normal Forms, ER-Relational Translation, etc.