Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "376a. Database Design Dept. of Computer Science Vassar College"— Presentation transcript:

1 376a. Database Design Dept. of Computer Science Vassar College
Class 4 – Relational Data Model and Relational Algebra Prof. Billibon Yoshimi 11/25/2018

2 SELECT operation (s) s<condition>(RELATION) - select a subset of tuples from RELATION where <condition> is satisfied. e.g. sSSN=‘ ’(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

3 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

4 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

5 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

6 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

7 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

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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

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

19 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

20 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

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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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


Download ppt "376a. Database Design Dept. of Computer Science Vassar College"

Similar presentations


Ads by Google