21
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.

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

Embed Size (px)

Citation preview

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

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, 5th Ed., the

instructor’s experience, and other sources as noted. Some items © 2006 John Wiley & Sons. All rights reserved.

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

One-to-One and Recursive Relationships

MIS 421

Dr. Steven C. Ross

Fall 2011

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

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.

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

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

*empnoempfnameempsalary

DEPT

*deptnamedeptfloor

deptphonedepartment’s

boss

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

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

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

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

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

Querying a 1:1 Relationship

SELECT empfname, tblEmp.deptname, empsalaryFROM tblEmp, tblDeptWHERE tblDept.empno = tblEmp.empno

SELECT empfname, tblEmp.deptname, empsalaryFROM tblEmp INNER JOIN tblDept ON tblDept.empno = tblEmp.empno

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

tblWorkerWorkerID

LastName

FirstName

MidName

AnnualPayIn1000

RankID

SupervisorID

DepartmentID

tblDepartmentDepartmentID

DepartmentName

DeptManagerID

Sample DatabaseRecursive Relationship

1:1 or 1:M ???

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

SELECT Command Example I

SELECT DepartmentName,FirstName,LastNameFROM dbo.tblWorker INNER JOIN dbo.tblDepartmentON tblDepartment.DeptManagerID=tblWorker.WorkerID

DepartmentName FirstName LastNameBellingham City Bob VisserWhatcom County Lisa Schneider

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

A 1:M Recursive Relationship

The recursive relationship is labeled

EMP

*empnoempfnameempsalary

DEPT

*deptnamedeptfloor

deptphonedepartment’s

boss

employee’sboss

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

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

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

Querying a 1:M Recursive Relationship

Called a self join Create two “copies” of the table

Requires an alias for one or bothSELECT wrk.empfname,wrk.empsalary,boss.empfname,

boss.empsalary FROM tblEmp wrk, tblEmp bossWHERE wrk.bossno = boss.empnoAND wrk.empfname='Nancy';

SELECT wrk.empfname,wrk.empsalary,boss.empfname, boss.empsalary FROM tblEmp wrk INNER JOIN tblEmp boss ON wrk.bossno = boss.empnoWHERE wrk.empfname='Nancy';

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

SELECT Command Example II

SELECT W.FirstName,W.LastName,S.LastName AS Supervisor

FROM dbo.tblWorker W INNER JOIN dbo.tblWorker SON W.SupervisorID=S.WorkerID

FirstName LastName SupervisorBob Visser ZiglerLisa Schneider ZiglerShannon Spicer VisserBillie McMaster VisserJames Watts SchneiderDanny Dean Schneider<… more …>

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

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

*empnoempfname

GROUP

*groupnamegrpspec

team leader

employee’smentor

* From Richard T. Watson, Data Management: Databases and Organization, 5th Ed., p. 140

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

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

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

Querying a 1:1Recursive Relationship

SELECT premonname, premonnum FROM tblMonarchWHERE monname='Elizabeth' AND monnum='II';

SELECT pre.montypeFROM tblMonarch pre, tblMonarch curWHERE cur.premonname=pre.monnameAND cur.premonnum=pre.monnumAND cur.monname='Elizabeth' AND cur.monnum='II';

SELECT pre.montypeFROM tblMonarch pre INNER JOIN tblMonarch cur ON cur.premonname=pre.monname AND cur.premonnum=pre.monnumWHERE cur.monname='Elizabeth' AND cur.monnum='II';

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

Skill Builder*

Who succeeded Queen Victoria?

SELECT monname, monnum FROM tblMonarchWHERE premonname='Victoria' AND premonnum='I';

* From Richard T. Watson, Data Management: Databases and Organization, 5th Ed., p. 146

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

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

*prodidproddescprodcost

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

Querying a M:MRecursive Relationship

Often requires that you refer to the same table twice

SELECT b.proddesc,b.prodcost FROM tblProduct a

INNER JOIN tblAssembly asON a.prodid = as.prodid

INNER JOIN tblProduct bON b.prodid = as.subprodid

WHERE a.proddesc='Animal Photography Kit'

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

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, 5th Ed., p. 150

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

Next Lecture

Data Modeling

Basic Structures