376a. Database Design Dept. of Computer Science Vassar College

Slides:



Advertisements
Similar presentations
Chapter 6 The Relational Algebra
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 (Continued) The Relational Algebra and Calculus.
Chapter 6 Additional Relational Operations Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
The Relational Algebra
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra. Manipulating Databases To access information in a database we use a query Ex: How many customers have the first name = `John’? Good.
The Relational Algebra and Calculus. Relational Algebra Overview Relational algebra is the basic set of operations for the relational model These operations.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra and Calculus.
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 )
CS 380 Introduction to Database Systems Chapter 7: The Relational Algebra and Relational Calculus.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Algebra by Pinar Senkul resources: mostly froom Elmasri, Navathe and other.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Relational Algebra. 2 Outline  Relational Algebra Unary Relational Operations Relational Algebra Operations from Set Theory Binary Relational Operations.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
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 Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
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 )
The Relational Algebra. Slide 6- 2 Outline Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
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.
Chapter 6 The Relational Algebra Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
The Relational Data Model & Relational Algebra
Ritu CHaturvedi Some figures are adapted from T. COnnolly
CSE202 Database Management Systems
Database Systems Chapter 6
The Relational Algebra and Relational Calculus
The Relational Algebra and Relational Calculus
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
The Relational Algebra and Calculus
Module 2: Intro to Relational Model
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
The Relational Algebra and Relational Calculus
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
376a. Database Design Dept. of Computer Science Vassar College
Chapter 6: Relational Algebra Fall 2015 Dr. Abdullah Almutairi
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Chapter 4 The Relational Algebra and Calculus
Chapter 3 The Relational Database Model
The Relational Algebra and Calculus
The Relational Algebra and Relational Calculus
376a. Database Design Dept. of Computer Science Vassar College
Relational Database Design by ER- and EER-to-Relational Mapping
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
The Relational Algebra and The Relational Calculus
Presentation transcript:

376a. Database Design Dept. of Computer Science Vassar College http://www.cs.vassar.edu/~cs376 Class 4 – Relational Data Model and Relational Algebra Prof. Billibon Yoshimi 11/25/2018

SELECT operation (s) s<condition>(RELATION) - select a subset of tuples from RELATION where <condition> is satisfied. e.g. sSSN=‘333445555’(EMPLOYEE), sDEPT=5(EMPLOYEE) Degree of result is same as R. # of relations returned <= # of relations in R. All relations in R are evaluated. SELECT is commutative. Prof. Billibon Yoshimi 11/25/2018

PROJECT operation (s) p<list of attributes>(RELATION) -selects <list of attributes> columns from RELATION e.g. pSEX, SALARY(EMPLOYEE) PROJECT removes duplicate tuples from result set. Degree of result is = length of <list of attributes>. If one attribute is a key of RELATION, result will have same number of relations as RELATION. Prof. Billibon Yoshimi 11/25/2018

Relational Algerbra expression Created by nesting operations or applying operations one at a time. pFNAME,LNAME(sDNO=5(EMPLOYEE)) Or as a sequence of operations DEP5_EMPL <-sDNO=5(EMPLOYEE) RESULT <- pFNAME,LNAME(DEP5_EMPL) Prof. Billibon Yoshimi 11/25/2018

Rename operation ( r ) rho rS(B1, B2, .. Bn) ( R ) - attributes of R (A1, A2, … An) are mapped to their new attribute names B1, B2, … Bn. For example: RESULT(FIRSTNAME, LASTNAME, SALARY) <- pFNAME,LNAME,SALARY(EMPLOYEE) Prof. Billibon Yoshimi 11/25/2018

Set theory operations Union, intersection and set difference. Sets must contain same type tuples (union compatibility constraint). Union - R1  R2 includes all tuples in R1, R2 and the intersection of R1 and R2 without duplication. Intersection - R1  R2 tuples in both R1 and R2. Set difference - R1 - R2 tuples in R1 but not in R2. Prof. Billibon Yoshimi 11/25/2018

Set theory cont. By convention, attributes of resulting relation have the same name as first argument. Notes: - UNION and INTERSECTION are commutative and associative R1 (op) R2 = R2 (op) R1 R1 (op) ( R2 (op) R3 ) = (R1 (op) R2) op R3 Prof. Billibon Yoshimi 11/25/2018

Set theory cont. Set difference is not commutative Prof. Billibon Yoshimi 11/25/2018

Cartesian Product (x) Also known as cross product or cross join. No union compatibility requirement. Q = R1 x R2 where R1 (A1..An), R2(B1..Bm) Performs all pairs match between the two relations. Q(A1..An,B1…Bm) Total number of instances in Q? |R1|*|R2| Prof. Billibon Yoshimi 11/25/2018

Use for these operations? Generate a list of all the employees of department #5 and their children. DEPT5EMPL <- sDNO=5(EMPLOYEE) EMPNAMES <- pFNAME,LNAME,SSN(DEP5EMPL) EMPCHILDREN <- EMPNAMES x DEPENDENT ACTUALDEPEND <- sSSN=ESSN(EMPCHILDREN) RESULT <- pFNAME,LNAME,DEPENDENT_NAME(ACTUALDEPEND) Prof. Billibon Yoshimi 11/25/2018

Join operations () Product followed by select is given a special name, join, indicated by  Joins tuples satisfying condition. Q = R1  condition R2 if R1(A1..An) and R2 (B2..Bm) Q(A1..An,B1..Bm) where each tuple satisfies condition. DEP = EMPLOYEE  SSN=ESSN DEPENDENT A tuple should only appear once in the output set. (preserving the set constraint). Tuples with null join attributes do not appear in Q. Prof. Billibon Yoshimi 11/25/2018

Theta join () If the join constraint can be specified as cond1 and cond2 and … condn and each condition, condi can be represented as A1  A2 where   { =, <, , , >, } Then join is called a theta join. Prof. Billibon Yoshimi 11/25/2018

Join operations cont. Join operation using only = condition are called EQUIJOINS. (Always have one duplicate field) Natural join (*) removes duplicate by requiring the attributes to the = have the same name. PROJ_DEPT <- PROJECT*r(DNAME, DNUM,MGRSSN,MGRSTARTDATE) (DEPARTMENT) DNUM is join attribute. Join selectivity = expected size of join/(|R1|*|R2|) Prof. Billibon Yoshimi 11/25/2018

Complete set of relational algebra operations. {s, p, r, , -, x} Intersection can be represented in terms of union and -. While not all of the joins are necessary, they make database operations easier. Prof. Billibon Yoshimi 11/25/2018

Non relational algebra functions. Aggregate functions ()- describe a trait of a set of tuples. SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT <grouping attributes>  <function list> ( R ) <grouping attributes> - attributes of R (or none) <function list> - list of <function> <attribute> pairs Rename called to give attributes names. For example: DNO F COUNT SSN, AVERAGE SALARY (EMPLOYEE) Would result in <DNO, COUNT_SSN, AVERAGE_SALARY> tuples. Prof. Billibon Yoshimi 11/25/2018

Recursive closure Find everyone reporting to ‘John Borg’ 1. Find John’s SSN 2. Find all SSN, SUPERSSN relations 3. Find cross 2 and 1 and project to get all SSN of John’s supervisees. To get next level down 4. Find cross of 3 and 2 to get all supervisees supervisees. Prof. Billibon Yoshimi 11/25/2018

Left Outer Join ( ) Keeps every tuple in the first, or left relation R in R S. Example: EMPLOYEE SSN=MGRSSNDEPARTMENT EMPLOYEES with no matches are padded with NULLs. Prof. Billibon Yoshimi 11/25/2018

Right outer join ( )and full outer join ( ) Keep respective tuples accordingly. Prof. Billibon Yoshimi 11/25/2018

Outer Union Subset of attributes from both relations are compatible (expected that compatible attributes include a key for both relations). Prof. Billibon Yoshimi 11/25/2018

Let’s try a few queries Select the names and addresses of all employees in department #4. For all projects not in Houston, list the project, project manager’s name and address Make a list of the names of the children whose parent report to a manager with the last name ‘Smith’ Prof. Billibon Yoshimi 11/25/2018

More queries How many employees have more than 1 child. How many employees have no children? Prof. Billibon Yoshimi 11/25/2018

Now to chapter 9, mapping ER and EER to relational model How to convert from an ER or EER model (a graph) to a relational model (using tables)? Prof. Billibon Yoshimi 11/25/2018

1. Convert strong types Create relation R that includes all simple attributes of E. Select key of E to be key of R. Key may include several attributes. Prof. Billibon Yoshimi 11/25/2018

2. Convert weak entities Create relation R include foreign key of E (the owner of weak entity W) Create new primary key for R using E foreign key and weak key Prof. Billibon Yoshimi 11/25/2018

3. Convert 1:1 relations For entities S and T that participate in R. If S participates totally in R, use S’s primary key and T as foreign key in R. If both participate totally, combine relationships into single relation. (merge two entity types.) Prof. Billibon Yoshimi 11/25/2018

4. Convert 1:N relations S - n - R - 1 - T Use T as foreign key in S. Each instance in S is related to at most 1 T Prof. Billibon Yoshimi 11/25/2018

5. Convert M:N relations Create new relation S to represent R. Use primary keys of both relations as super key in S. Attach any attributes to relation to this relation. Prof. Billibon Yoshimi 11/25/2018

6. Convert multi-valued attributes Create a new relation. Primary key of R and attribute, A, is primary key of new relation. Prof. Billibon Yoshimi 11/25/2018

7. Convert n-ary relations Create new relation S. Primary key of S contains as foreign key keys of individual entities. If cardinality constraint of any of relations is 1, don’t add its key to the super key. Prof. Billibon Yoshimi 11/25/2018

8. For Enhanced ER Create a relationship for parent and each child. Add primary key of parent to each child Primary key of each child is primary key of parent. Prof. Billibon Yoshimi 11/25/2018