Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:

Similar presentations


Presentation on theme: "This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:"— Presentation transcript:

1

2 This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management: Databases and Organization, 5 th Ed., the instructor’s experience, and other sources as noted. Some items © 2006 John Wiley & Sons. All rights reserved.

3 One-to-One and Recursive Relationships MIS 421 Dr. Steven C. Ross Fall 2011

4 One-to-One ≠ Recursive One-to-one means a single instance of one entity is related to zero or one instances of another entity. Recursive means that an instance of an entity is related to zero, one, or more instances of the same entity. Sometimes called “unary relationship” There might be a 1:1 recursive.

5 A One-to-One Relationship The 1:1 relationship is labeled “Relationship descriptor” Use when more than 1 relationship between entities Use when meaning of relationship is not easily inferred EMP *empno empfname empsalary DEPT *deptname deptfloor deptphone department’s boss

6 Mapping a 1:1 Relationship Same procedure for entities, attributes, and identifiers “Optional Max” rule for FK One side of most 1:1 relationships is optional Department has one and only one boss Employee is boss of zero or one departments  Department side is optional Treat optional side as if it were the many side FK goes on optional side, in this case department

7 Potential Problem When there are two-way relationships … EMP has FK pointing to DEPT and DEPT has FK pointing to EMP Only one of the FK can be created Other means can be used to enforce referential (foreign key) integrity

8 Querying a 1:1 Relationship SELECT empfname, tblEmp.deptname, empsalary FROM tblEmp, tblDept WHERE tblDept.empno = tblEmp.empno SELECT empfname, tblEmp.deptname, empsalary FROM tblEmp INNER JOIN tblDept ON tblDept.empno = tblEmp.empno

9 Sample Database Recursive Relationship 1:1 or 1:M ???

10 SELECT Command Example I SELECT DepartmentName,FirstName,LastName FROM dbo.tblWorker INNER JOIN dbo.tblDepartment ON tblDepartment.DeptManagerID=tblWorker.WorkerID DepartmentName FirstName LastName Bellingham City Bob Visser Whatcom County Lisa Schneider

11 A 1:M Recursive Relationship The recursive relationship is labeled EMP *empno empfname empsalary DEPT *deptname deptfloor deptphone department’s boss employee’s boss

12 Mapping a 1:M Recursive Relationship Same procedure for entities, attributes, and identifiers FK is in same entity FK points to PK of entity Must have a different field name

13 Querying a 1:M Recursive Relationship Called a self join Create two “copies” of the table Requires an alias for one or both SELECT wrk.empfname,wrk.empsalary,boss.empfname, boss.empsalary FROM tblEmp wrk, tblEmp boss WHERE wrk.bossno = boss.empno AND wrk.empfname='Nancy'; SELECT wrk.empfname,wrk.empsalary,boss.empfname, boss.empsalary FROM tblEmp wrk INNER JOIN tblEmp boss ON wrk.bossno = boss.empno WHERE wrk.empfname='Nancy';

14 SELECT Command Example II SELECT W.FirstName,W.LastName, S.LastName AS Supervisor FROM dbo.tblWorker W INNER JOIN dbo.tblWorker S ON W.SupervisorID=S.WorkerID FirstName LastName Supervisor Bob Visser Zigler Lisa Schneider Zigler Shannon Spicer Visser Billie McMaster Visser James Watts Schneider Danny Dean Schneider

15 Skill Builder* A consulting company has assigned each of its employees to a specialist group. Each specialist group has a team leader. When employees join the company, they are assigned a mentor for the first year. One person might mentor several employees, but an employee has at most one mentor. EMP *empno empfname GROUP *groupname grpspec team leader employee’s mentor * From Richard T. Watson, Data Management: Databases and Organization, 5 th Ed., p. 140

16 A 1:1 Recursive Relationship One instance of an entity is related to only one other instance of the same entity FK is in same entity FK points to PK of entity Must have a different field name Points one way or the other – not both ways MONARCH monarch type *monarch name *monarch number succession

17 Querying a 1:1 Recursive Relationship SELECT premonname, premonnum FROM tblMonarch WHERE monname='Elizabeth' AND monnum='II'; SELECT pre.montype FROM tblMonarch pre, tblMonarch cur WHERE cur.premonname=pre.monname AND cur.premonnum=pre.monnum AND cur.monname='Elizabeth' AND cur.monnum='II'; SELECT pre.montype FROM tblMonarch pre INNER JOIN tblMonarch cur ON cur.premonname=pre.monname AND cur.premonnum=pre.monnum WHERE cur.monname='Elizabeth' AND cur.monnum='II';

18 Skill Builder* Who succeeded Queen Victoria? SELECT monname, monnum FROM tblMonarch WHERE premonname='Victoria' AND premonnum='I'; * From Richard T. Watson, Data Management: Databases and Organization, 5 th Ed., p. 146

19 A M:M Recursive Relationship Many instances of an entity are related to many other instances of the same entity 2 FKs in associative entity FKs points to PK of main entity Must have different field names e.g., prodid and subprodid Very common: Bill of Materials ASSEMBLY quantity PRODUCT *prodid proddesc prodcost

20 Querying a M:M Recursive Relationship Often requires that you refer to the same table twice SELECT b.proddesc,b.prodcost FROM tblProduct a INNER JOIN tblAssembly as ON a.prodid = as.prodid INNER JOIN tblProduct b ON b.prodid = as.subprodid WHERE a.proddesc='Animal Photography Kit'

21 Skill Builder* How many lens cleaning clothes are there in the animal photography kit? SELECT as.quantity FROM tblProduct a INNER JOIN tblAssembly as ON a.prodid = as.prodid INNER JOIN tblProduct b ON b.prodid = as.subprodid WHERE a.proddesc='Animal Photography Kit' AND b.proddesc='Lens Cleaning Cloth'; * From Richard T. Watson, Data Management: Databases and Organization, 5 th Ed., p. 150

22 Next Lecture Data Modeling Basic Structures


Download ppt "This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:"

Similar presentations


Ads by Google