Sei sulla pagina 1di 109

Theory, Practice & Methodology of Relational Database Design and Programming

Copyright Ellis Cohen 2002-2008

M:N Relationships & Bridge Classes


These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see http://www.openlineconsult.com/db
1

Overview of Lecture
M:N Relationships M:N Relationships and Cascading Delete Joins Involving M:N Relationships Bridge Entity Classes Bridge Class Attributes Partial Associations Using Dependent & Identifying Relationships Uniqueness Weak Bridge Classes Weak Bridge Classes with Attributes Weak Bridge Classes with Participation & Cardinality Discriminated Weak Bridge Classes Relationships with Bridge Entity Classes
Ellis Cohen 2001-2008 2

M:N Relationships

Ellis Cohen 2001-2008

M:N Relationships
Visual Conceptual Model (Crow Magnum) Employee assigned to Project

Each employee may be assigned to a number of projects Each project may have a number of employees

Textual Conceptual Model (Brief ConText) Assignment: (*) Employee assigned to (*) Project
Ellis Cohen 2001-2008 4

M:N Relationship Notations


Chen

Employee
No key constraints!

assigned to

Project

UML

Employee

assigned to

Project

Crow Magnum Easy Crow Magnum

assigned to

Employee

Project

Employee

assigned to

Project

Ellis Cohen 2001-2008

Simple Relational Mapping


1. Make each entity class a table 2. Make each relationship a table (called a bridge table)
Use singular for entity class Use plural for table

Ellis Cohen 2001-2008

Entity Classes

Tables

Make each entity class a table


Each attribute of the entity class becomes an attribute (i.e. a column) of the table Define a primary key for the entity class if necessary The primary key of the entity class becomes the primary key of the table
Use singular for entity class

Emps
empno ename 7499 7654 7698 7839 7844 7986 ALLEN MARTIN BLAKE KING TURNER STERN address 12 Lehigh 1400

Use plural for table

Projs
pno 2618 2621 2622 pname Payroll Bridge Design Update Reqs

Ellis Cohen 2001-2008

M:N Related Instances


Employee
empno

assigned to

Project
pno

empno ename 7499 7654 7698 7839 7844 7986 ALLEN

address ... ... ... ... ... ... pno 2618 2621 2622 pname

MARTIN BLAKE KING TURNER STERN

Ellis Cohen 2001-2008

Relationships
Employee
empno
Composite primary key

Tables
Project
pno

assigned to

Emps
empno ename address 7499 7654 7698 7839 7844 7986 ALLEN MARTIN BLAKE KING TURNER STERN ...

Asns
empno 7654 7698 7698 7844 7844 7986 pno 2621 2618 2622 2618 2622 2622

Projs
pno 2618 2621 2622 pname

Bridge Table
Ellis Cohen 2001-2008 9

Tables & Relational Schema


Emps
empno ename address 7499 7654 7698 7839 7844 7986 ALLEN MARTIN BLAKE KING TURNER STERN ...

Asns
empno 7654 7698 7698 7844 7844 7986 pno 2621 2618 2622 2618 2622 2622

Projs
pno 2618 2621 2622 pname

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno

Projs
pno pname budget
10

Note cascading deletes


Ellis Cohen 2001-2008

M:N Relationships and Cascading Delete

Ellis Cohen 2001-2008

11

M:N Related Instances


Employee
empno

assigned to

Project
pno

empno ename 7499 7654 7698 7839 7844 7986 ALLEN

address ... ... ... ... ... ...

If a project is deleted, what happens to its assignment links?


pno 2618 pname

MARTIN BLAKE KING TURNER STERN

2621 2622

Ellis Cohen 2001-2008

12

No Dangling Links
Employee
empno

assigned to

Project
pno

empno ename 7499 7654 7698 7839 7844 7986 ALLEN

address ... ... ... ... ... ...

Links that don't connect to an entity instance are not allowed!

MARTIN BLAKE KING TURNER STERN

2621 2622

Ellis Cohen 2001-2008

13

M:N & Cascading Delete


Visual CONCEPTUAL Model (Crow Magnum) Employee
empno

assigned to

Project
pno

Visual RELATIONAL Model (Relational Schema) The standard relational mapping for an M:N relationship uses cascading delete Emps
empno ename

Asns
empno pno

Projs
pno pname budget

Ellis Cohen 2001-2008

14

Mapping M:N to Relational Schema


CONCEPTUAL Model
Assignment: (*) Employee assigned to (*) Project

RELATIONAL Mapping
Assignment mapped to Asns( empno, pno ), with empno references Emps on cascade delete pno references Projs on cascade delete

RELATIONAL Model
Asns empno number(4) references Emps on cascade delete pno number(3) references Projs on cascade delete primary key( empno, pno )
Ellis Cohen 2001-2008 15

M:N w Cascading Delete in SQL


Emps
empno ename

Asns
empno pno

Projs
pno pname budget

CREATE TABLE Asns( empno number(5) references Emps on delete cascade, pno number(6) references Projs on delete cascade, primary key( empno, pno ) ); What would be the implication of not using cascading delete for one or both of the foreign key constraints, on operations that delete Employees or Projects?
Ellis Cohen 2001-2008 16

Eliminating Cascading Delete


Emps
empno ename

Asns
empno pno

Projs
pno pname budget

+ An employee assigned to a project can't be deleted Emps


empno ename

Asns
empno pno

Projs
pno pname budget

+ A project with employees assigned to it can't be deleted Added business rules can eliminate cascading delete from the relational model
Ellis Cohen 2001-2008 17

Implication of No Cascading Deletes


Emps
empno ename

Asns
empno pno

Projs
pno pname budget

Since deletes do not cascade, the application must make sure that before it deletes an employee or project, it explicitly eliminates all of its assignments. For example, before deleting an employee, it must explicitly delete its assignments, perhaps in the meantime assigning a different employee to the project!

Ellis Cohen 2001-2008

18

Incorrect M:N Relational Mapping


Visual Conceptual Model (Crow Magnum) Employee
empno ename assigned to

Project
pno pname budget

What's WRONG with this relational mapping? Emps


empno ename pno

Projs
pno pname budget empno

WRONG!

Ellis Cohen 2001-2008

19

Incorrect M:N Relational Mapping Explanation


Emps
empno ename pno

Projs
pno pname budget empno

1. Each employee (identified by empno) has a single associated pno. Implies there is only a single project associated with each employee! 2. Each project (identified by pno) has a single associated empno. Implies there is only a single employee associated with each project! 1:1 relationship Employee assigned to
Ellis Cohen 2001-2008 20

Project

Reflexive M:N Relationships


likes to work with

Employee
empno ename

What's the corresponding relational schema?

Ellis Cohen 2001-2008

21

Reflexive M:N Mapping


likes to work with Employee
empno ename

Emps
empno ename

LikesWorkingWith
emp1 emp2

Ellis Cohen 2001-2008

22

SQL for Reflexive M:N Mapping


Emps
empno ename

LikesWorkingWith
emp1 emp2

CREATE TABLE LikesWorkingWith( emp1 number(5) references Emps on delete cascade, emp2 number(6) references Emps on delete cascade, primary key( emp1, emp2 ) );

Ellis Cohen 2001-2008

23

Joins Involving M:N Relationships

Ellis Cohen 2001-2008

24

Joins with M:N Relationships


Employee Emps
empno ename

assigned to

Project

Asns
empno pno

Projs
pno pname budget

For each (named) project, list the names of the employees assigned to it
SELECT pname, ename FROM ( (Emps NATURAL JOIN Asns) NATURAL JOIN Projs ) ORDER BY pname, ename

Ellis Cohen 2001-2008

25

Size of M:N Joins


If there are 200 tuples in Emps 5 tuples in Projs How many tuples (min and max) could there be in the NATURAL JOIN of Emps and Asns and Projs? Consider each of these 4 cases
1 Employee Project 2 Employee Project

3 Employee Project

4 Employee Project

Ellis Cohen 2001-2008

26

Answer: Size of M:N Joins


If there are 200 tuples in Emps 5 tuples in Projs How many tuples (min and max) could there be in the NATURAL JOIN of Emps and Asns and Projs?
1 Employee Project 2 Employee Project

200..1000
3 Employee Project 4

5..1000

Employee

Project

200..1000
Ellis Cohen 2001-2008

0..1000
27

Bridge Entity Classes

Ellis Cohen 2001-2008

28

M:N Relationships as Entity Classes


Sometimes an M:N relationship is thought of at the conceptual level as an entity class in its own right:

An Assignment can be thought as an entity class representing an assignment of an employee to a project An employee may be associated with a number of assignments
each one assigning that employee to a project

A project may be associated with a number of assignments


each one assigning an employee to that project
Ellis Cohen 2001-2008 29

Bridge Entity Classes for M:N Relationships


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget

empno ename

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno

Projects
pno pname budget

Note, no primary key and no cascading delete


Ellis Cohen 2001-2008

30

Bridge Classes vs M:N


Emps
empno ename

Asns
empno pno

Projects
pno pname budget

No primary key No mandatory participation (in an assignment, empno or pno can be null) No uniqueness constraint (can have multiple assignments with same empno & pno) No unique identification of assignments No cascading delete When an employee or project is deleted, its assignments are not automatically deleted
Ellis Cohen 2001-2008 31

Uses of Bridge Entity Classes


M:N Relationships are simpler and should be used whenever possible, but Bridge Entity Classes allow flexibility w.r.t.
Relationship attributes Selective control of entity identity, mandatory participation and cascading delete

Ellis Cohen 2001-2008

32

Uniquely Identifying Bridge Entities


Using bridge classes provides choices in uniquely identifying bridge entities (e.g. assignments)
no unique identification (i.e. no primary key) add new primary key attribute use weak identity (e.g. number the assignments associated with each project) make empno+pno unique / primary key

Ellis Cohen 2001-2008

33

Mapping Bridge Entity Classes with Primary Keys


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid

Project
pno pname budget

empno ename

Visual Relational Model (Relational Schema) Asns Emps


empno ename asnid empno ! pno !

Projs
pno pname budget
34

Ellis Cohen 2001-2008

Bridge Class Attributes

Ellis Cohen 2001-2008

35

Relationship Attributes
Employee
empno

assigned to

Project
pno

Suppose that when we assign an employee to a project, we want to indicate the # hrs per week they are supposed to work on the project.

Can we associate that with either the Employee or the Project?

Ellis Cohen 2001-2008

36

Bridge Classes With Attributes

Employee

assigned via

Assignment
asnid hrsPerWeek

assigned via

Project
pno pname budget

empno ename

Relationship Attribute: How many hrs per week is the employee assigned to the project

Ellis Cohen 2001-2008

37

Mapping Bridge Entity Classes with Attributes


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid hrsPerWeek

Project
pno pname budget

empno ename

Visual Relational Model (Relational Schema) Asns Emps


empno ename asnid empno ! pno ! hrsPerWeek

Projs
pno pname budget

Ellis Cohen 2001-2008

38

Attributes Example

Emps
empno 7499 7654 7698 7839 7844 7986 ... asnid 1 2 3 4 5 6

Asns
empno 7499 7698 7698 7844 7844 7986 pno hrsPerWeek 2621 2618 2622 2618 2622 2622 22 4 1 12 5

Projs
pno 2618 2621 2622

Ellis Cohen 2001-2008

39

Partial Associations

Ellis Cohen 2001-2008

40

M:N Related Instances


Employee
empno

assigned to

Project
pno

empno ename 7499 7654 7698 7839 7844 7986 ALLEN

address ... ... ... ... ... ...

Links that don't connect to an entity instance are not allowed!

MARTIN BLAKE KING TURNER STERN

2621 2622

Ellis Cohen 2001-2008

41

Standard M:N Assignments


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid

Project
pno pname budget

empno ename

Every assignment must have both an employee and a project Visual Relational Model (Relational Schema) Emps
empno ename

Asns
asnid empno ! pno !
Ellis Cohen 2001-2008

Projects
pno pname budget
42

Partial Assignments
Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid job

Project
pno pname budget

empno ename

Bridge classes allow partial assignments: An assignment can be specified for a project, without having an employee to fill it.

Visual Relational Model (Relational Schema) Asns Emps


empno ename asnid empno pno ! job
Ellis Cohen 2001-2008

Projects
pno pname budget
43

Partial Association Example


Asns
asnid 1 2 3 4 5 6 pno 2613 2618 2618 2622 2622 2622 empno 7839 7698 7499 7844 job MGR MGR DEV MGR DEV DEV

Project 2622 needs two developers, but no employee has been assigned to those positions yet

Ellis Cohen 2001-2008

44

Using Dependent & Identifying Relationships

Ellis Cohen 2001-2008

45

Specifying Lifetime Dependency


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid job

Project
pno pname budget

empno ename

With bridge classes, lifetime dependency can be selectively added Visual Relational Model (Relational Schema) Asns Emps
empno ename asnid empno pno ! job
Ellis Cohen 2001-2008

Projs
pno pname budget
46

Partially Dependent Bridge Class


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asnid job

Project
pno pname budget

empno ename

When the project is deleted, all assignment for the projects (including partial ones) are deleted

Textual Conceptual Model (Brief ConText) [Employee, Project] Assignment( asnid, job ) (1) Employee assigned via (*) Assignment (1) Project assigned via (*) Assignment
Ellis Cohen 2001-2008 47

Using an Identifying Relationship


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asndx job

Project
pno pname budget

empno ename

Textual Conceptual Model (Brief ConText) [Project] Assignment( asndx, job ) (1) Employee assigned via (*) Assignment (1) Project assigned via (*) Assignment What's the relational schema diagram?
Ellis Cohen 2001-2008 48

Mapping the Identifying Relationship


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment
asndx job

Project
pno pname budget

empno ename

Visual Relational Model (Relational Schema) Asns Emps


empno ename pno asndx empno job

Projs
pno pname budget

Ellis Cohen 2001-2008

49

Uniqueness

Ellis Cohen 2001-2008

50

Uniqueness Issue

Employee

assigned via

Assignment

assigned via

Project
pno pname budget

empno ename

asnid job hrsPerWeek

Could an employee have more than one assignment for the same project?

Ellis Cohen 2001-2008

51

Multiple Assignments Example


Asns
asnid 1 2 3 4 5 6 pno 2613 2618 2618 2622 2622 2622 empno 7839 7698 7499 7844 7316 7316 hrsPerWeek job 20 40 40 40 30 10 MGR MGR DEV MGR DEV LIB

Employee 7316 works 30 hrs per week on proj 2622 as a developer 10 hrs per week on proj 2622 as a librarian Is there a uniqueness constraint?
Ellis Cohen 2001-2008 52

Add Uniqueness Constraint


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget

empno ename

asnid job hrsPerWeek

+ There is at most one assignment for the same employee, project and job

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
asnid empno ! pno ! job hrsPerWeek
Ellis Cohen 2001-2008

Projects
pno pname budget
53

Uniqueness Constraint in SQL


Emps
empno ename

Asns
asnid empno ! pno ! job hrsPerWeek

Projects
pno pname budget

CREATE TABLE Asns( asnid number(8) primary key, empno number(5) not null references Emps, pno number(6) not null references Projs, job varchar(10), hrsPerWeek number(2), unique( empno, pno, job ) );

Ellis Cohen 2001-2008

54

Stronger Uniqueness Constraint


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget
Doesnt allow a person to have two jobs on the same project

empno ename

asnid job hrsPerWeek

+ There is at most one assignment for the same employee and project

Visual Relational Model (Relational Schema) Asns Emps


empno ename asnid empno ! pno ! job hrsPerWeek

Projects
pno pname budget

Ellis Cohen 2001-2008

55

Eliminate Primary Key


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget

empno ename

No need for PK

hrsPerWeek job

+ There is at most one assignment for the same employee and project

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno ! pno ! job hrsPerWeek

Projects
pno pname budget

Ellis Cohen 2001-2008

56

Upgrade to Relational PK
Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget

empno ename

No need for PK

hrsPerWeek job

+ There is at most one assignment for the same employee and project

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno job hrsPerWeek

Projects
pno pname budget

Since empno + pno are unique and non-null, and there is no primary key, we make empno + pno the primary key during relational mapping
Ellis Cohen 2001-2008 57

M:N Relationships & Bridge Classes


Emps Emp
empno

Asns
empno pno !

Asn

Proj
pno

empno

Projs
pno

Emp
empno

Asn

Proj
pno

+ There is at most one assignment for the same employee and project

Emps
empno

Asns
empno pno

Projs
pno

Emp
empno

assigned to

Proj
pno

+ An employee assigned to a project can't be deleted & vice versa

Ellis Cohen 2001-2008

58

M:N Relationships & Bridge Classes w Dependency


Emps Emp
empno

Asns
empno pno !

Asn

Proj
pno

empno

Projs
pno

Emp
empno

Asn

Proj
pno

+ There is at most one assignment for the same employee and project

Emps
empno

Asns
empno pno

Projs
pno

Emp
empno

assigned to

Proj
pno

+ An employee assigned to a project can't be deleted

Ellis Cohen 2001-2008

59

Weak Bridge Classes

Ellis Cohen 2001-2008

60

Weak Project Assignments


Suppose we only had an Employee class & an Assignments class (but no Project class) We might represent their relationship as: Employee
empno ename assigned via

Assignment
pno

An employee has many project assignments. An employee's project assignments are discriminated by the project number Emps
empno ename

Asns
empno pno

Ellis Cohen 2001-2008

61

Weak Employee Assignments


Suppose we only had an Project class & an Assignments class (but no Employee class) We might represent their relationship as: Assignment
empno assigned via

Project
pno pname budget

A project has many employee assignments. A project's employee assignments are discriminated by the employee number Asns
empno pno

Projs
pno pname budget
Ellis Cohen 2001-2008 62

Weak Bridge Entity Classes


A Weak Bridge Entity Class is a Weak Entity Class that has multiple identifying relationships
assigned via assigned via

Employee
empno ename

Assignment

Project
pno pname budget

Assignment does not need an instance discriminator. The combination of the identities of its two identifying owners uniquely identifies an assignment

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno
Relational "fingerprint" of a Weak Bridge Entity Class (or an M:N relationship) Ellis Cohen 2001-2008

Projs
pno pname budget
63

Weak Bridge Entity Classes and M:N Relationships


Employee
empno ename assigned via

Assignment

assigned via

Project
pno pname budget

Identical

Employee
empno ename

assigned to

Project
pno pname budget

Ellis Cohen 2001-2008

64

M:N Relationships & Weak & Dependent Bridge Classes


Emp
empno

Asn

Proj
pno

+ There is at most one assignment for the same employee and project

Emps Emp
empno

Asns
empno pno

Asn

Proj
pno

empno

Projs
pno

Emp
empno

Proj
pno

Ellis Cohen 2001-2008

65

Composite Primary Keys with Partial Dependency


Emp
empno

Asn

Proj
pno

+ There is at most one assignment for the same employee and project

Emp
empno

Asn

Proj
pno

Emps
empno

Asns
empno pno

Projs
pno

+ An employee with assignments cannot be deleted

Emp
empno

assigned to

Proj
pno

+ An employee assigned to a project can't be deleted Ellis Cohen 2001-2008 66

Weak Bridge Classes with Attributes

Ellis Cohen 2001-2008

67

Mapping Bridge Entity Classes with Attributes


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee
empno ename

Assignment

Project
pno pname budget

hrsPerWeek

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno hrsPerWeek

Projs
pno pname budget

Ellis Cohen 2001-2008

68

Attribute Example

Emps
empno ename address 7499 7654 7698 7839 7844 7986 ALLEN MARTIN BLAKE KING TURNER STERN ... empno 7654 7698 7698 7844 7844 7986

Asns
pno hrsPerWeek 2621 2618 2622 2618 2622 2622 22 4 1 12 5

Projs
pno 2618 2621 2622

Ellis Cohen 2001-2008

69

Weak Bridge Entity Class Models


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee
empno ename

Assignment

Project
pno pname budget

hrsPerWeek
just an ordinary class attribute

Textual Conceptual Model (Brief ConText)


Entity Classes Employee( empno, ename ) Project( pno, pname, budget ) [Employee, Project] Assignment( hrsPerWeek ) Relationships EmpAssign: (1) Employee assigned via (*) Assignment ProjAssign: (1) Project assigned via (*) Assignment
Ellis Cohen 2001-2008 70

M:N Relationships with Attributes


Crow Magnum assigned via

Employee

Assignment

assigned via

Project

hrsPerWeek Easy Crow Magnum assigned via assigned via

Employee

Assignment

Project

hrsPerWeek

UML

Employee

* Assignment hrsPerWeek

Project
Association Class

hrsPerWeek

Chen

Employee

assigned to

Project

Ellis Cohen 2001-2008

71

No Dependent Bridge Classes in UML


UML

Employee
PK

Assignment hrsPerWeek

Project
PK

empno

pno

Dependent bridge entity classes cannot be used in UML. Composition implies containment in UML, and it's not possible for Assignment to be contained in both the Employee and the Project classes * Assignment
Association Class

UML

Employee
PK

Project
PK

empno

pno

hrsPerWeek

Ellis Cohen 2001-2008

72

UML Qualifying Relationships


Employee
PK

pno

0..1
assigned via

empno

Assignment
assigned via

hrsPerWeek

Project
PK

empno

0..1

pno

Bridge classes could potentially be modeled in UML just using qualifying relationships, which captures weak identity, but not lifetime dependency. BUT: Use Association Classes instead
Ellis Cohen 2001-2008 73

Weak Bridge Classes with Participation and Cardinality

Ellis Cohen 2001-2008

74

Bridge Entity Classes & M:N Participation


Employee
empno assigned to

Project
pno

Identical

Employee

assigned via

Assignment

assigned via

Project
pno

empno

Ellis Cohen 2001-2008

75

Assertions for Mandatory Participation


Employee
empno assigned to

Project
pno

Every project has at least one employee Every project has at least one assignment Emps
empno ename

Asns
empno pno

Projs
pno pname budget

(SELECT count(*) FROM Projs) = (SELECT count(DISTINCT pno) FROM Asns)


Ellis Cohen 2001-2008 76

Bridge Entity Classes & M:N Cardinality


3..20 0..4

Employee
empno

assigned to

Project
pno

Essentially Identical

assigned via

Employee
empno

Assignment
0..4

assigned via
3..20

Project
pno

Ellis Cohen 2001-2008

77

M:N Related Relationships


A laboratory has a number of pieces of very expensive equipment. A database application keeps track of which researchers are permitted to use which piece of equipment. Also, each piece of equipment has a single manager a researcher who is responsible for that piece of equipment (and who, of course, is permitted to use it) What's the best conceptual model?
Ellis Cohen 2001-2008 78

Modeling 1:M/M:N Related Relationships


manages

Researcher Why?
permitted to use

Equipment

Which conceptual state constraint is also required?

Ellis Cohen 2001-2008

79

1:M/M:N w State Constraint


+ State Constraint: If a researcher manages a piece of equipment, then that researcher must also be permitted to use it
manages

Mandatory participation implied by state constraint

Researcher
permitted to use

Equipment

What's the corresponding relational model?

Ellis Cohen 2001-2008

80

1:M/M:N Relational Models


Researchers
rschid rname

Permissions
rschid eqpid

Equipment
eqpid eqpnam mgr !

If a researcher manages a piece of equipment, then that researcher must also be permitted to use it

(SELECT count(*) FROM Equipment) = (SELECT count(*) FROM (Equipment e JOIN Permissions p ON e.eqpid = p.eqpid AND rschid = mgr))
Ellis Cohen 2001-2008 81

Bridge Class Representation


manages

Researcher

Equipment

Permission

Is it possible to eliminate the manages relationship?

Ellis Cohen 2001-2008

82

Using Boolean Attributes

Researcher

permitted via

Permission
isManager

permitted via

Equipment

could generalize this to accessLevel

Which conceptual state constraint is also required?

Ellis Cohen 2001-2008

83

Required Constraint
+ State Constraint: Every piece of equipment is managed by a single researcher

Researcher

permitted via

Permission
isManager

permitted via

Equipment

What's the corresponding relational model?

Ellis Cohen 2001-2008

84

1:M/M:N Related Relational Models


Researchers
rschid rname

Permissions
rschid eqpid isManager

Equipment
eqpid eqpnam

Every piece of equipment is managed by a single researcher (SELECT count(rschid) FROM (Permissions p RIGHT JOIN Equipment e ON p.eqpid = e.eqpid AND p.isManager = 'T')) ALL = 1

Ellis Cohen 2001-2008

85

Using Inclusion Constraints


Researchers
rschid rname

Permissions
eqpid rschid

Equipment
eqpid eqpnam mgr !

If a researcher manages a piece of equipment, then that researcher must also be permitted to use it

Can be represented directly in the relational model!

Permissions Researchers
rschid rname eqpid rschid

Equipment
eqpid eqpnam mgr !
86

Ellis Cohen 2001-2008

Discriminated Weak Bridge Classes

Ellis Cohen 2001-2008

87

Discriminated Weak Bridge Classes


Visual Conceptual Model (Crow Magnum)
for with

Employee
empno ename

Assigtnment

Project
pno pname budget

job hrsPerWeek

An employee may have multiple assignments to the same project These are discriminated by job

Textual Conceptual Model (Brief ConText) [Employee, Project] Assignment( job, hrsPerWeek )

Ellis Cohen 2001-2008

88

Mapping Discriminated Weak Bridge Entity Classes


Visual Conceptual Model (Crow Magnum)
for with

Employee
empno ename

Assigtnment

Project
pno pname budget

job hrsPerWeek

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
empno pno job hrsPerWeek

Projs
pno pname budget

Ellis Cohen 2001-2008

89

SQL for Discriminated Weak Bridge Entity Class


Emps
empno ename

Asns
empno pno job hrsPerWeek

Projs
pno pname budget

CREATE TABLE Asns( empno number(5) not null references Emps, pno number(6) not null references Projs, job varchar(10), hrsPerWeek number(2), primary key( empno, pno, job ) );

Ellis Cohen 2001-2008

90

Add Surrogate Primary Key


Visual Conceptual Model (Crow Magnum)
assigned via assigned via

Employee

Assignment

Project
pno pname budget
Add surrogate PK add uniqueness constraint

empno ename

asnid job hrsPerWeek

+ There is at most one assignment for the same employee, project and job

Visual Relational Model (Relational Schema) Emps


empno ename

Asns
asnid empno ! pno ! job hrsPerWeek
Ellis Cohen 2001-2008

Projects
pno pname budget
91

Discriminated Bridge Classes

Emp
empno

Asn
job

Proj
pno

+ There is at most one assignment for the same employee, project & job

Emps
empno

Asns
empno pno job

Projs
pno

Emp
empno

Asn
job

Proj
pno

Ellis Cohen 2001-2008

92

Discriminated Bridge Classes with Partial Dependency

Emp
empno

Asn
job

Proj
pno

+ There is at most one assignment for the same employee, project & job

Emps
empno

Asns
empno ! pno job

Projs
pno

Emp
empno

Asn
job

Proj
pno

+ An employee with assignments cannot be deleted

Ellis Cohen 2001-2008

93

Relationships with Bridge Entity Classes

Ellis Cohen 2001-2008

94

1:M Relationship to a Weak Bridge Entity Class


Suppose that when a contractor works on a project, they work through a single agency. Any particular agency may arrange any number of assignments.
Contractor
cno

Assignment
hrsPerWeek

Project
pno

Agency
agencyid

What's the relational schema?

Ellis Cohen 2001-2008

95

Mapping 1:M Relationship to Bridge


Visual Conceptual Model (Crow Magnum) Contractor
cno

Assignment
hrsPerWeek

Project
pno

Agency
agencyid

Visual Relational Model (Relational Schema) Asns Agencies


agencyid cno pno hrsPerWeek agencyid

Contrs
cno

Projs
pno

Ellis Cohen 2001-2008

96

SQL for 1:M Relationship to Bridge


Asns Agencies
agencyid cno pno hrsPerWeek agencyid

Contrs
cno

Projs
pno

CREATE TABLE Asns( cno number(5) not null references Contrs on delete cascade, pno number(6) not null references Projs on delete cascade, hrsPerWeek number(2), agencyid number(5) references Agencies, primary key( cno, pno ) );

Ellis Cohen 2001-2008

97

UML Bridge Relationship

UML

Contractor
PK

Project
PK

cno Assignment hrsPerWeek *

pno

Agency
PK

agencyid

Ellis Cohen 2001-2008

98

1:M Relationship from a Weak Bridge Entity Class


When a contractor works on a project, they submit multiple bills. Each bill is for only one assignment.

Contractor
cno

Assignment
hrsPerWeek

Project
pno

Bill
billid billdate

What's the relational schema?

Ellis Cohen 2001-2008

99

Mapping 1:M Relationship from Bridge


Visual Conceptual Model (Crow Magnum) Contractor
cno

Assignment
hrsPerWeek

Project
pno

Bill Visual Relational Model (Relational Schema) Asns Bills


billid billdate cno ! pno ! cno pno hrsPerWeek billid billdate

If an assignment cant be deleted if it has any outstanding bills, what happens when an attempt is made to delete a contractor?

Contrs
cno

Projs
pno
Ellis Cohen 2001-2008 100

SQL for 1:M Relationship from Bridge


Asns Bills
billid billdate cno ! pno ! cno pno hrsPerWeek

Contrs
cno

Projs
pno

CREATE TABLE Bills( billid number(8) primary key, billdate date, cno number(5) not null, pno number(6) not null, foreign key( cno, pno ) references Asns );

Ellis Cohen 2001-2008

101

Possible Representation?
Contractor
cno

Assignment
hrsPerWeek

Project
pno

Bill
billid billdate

Would this relational model work instead? Contrs Asns Bills


billid billdate cno ! pno ! cno pno hrsPerWeek cno

Projs
pno
Ellis Cohen 2001-2008 102

Incorrect Representation
Contractor
cno

Assignment
hrsPerWeek

Project
pno

Bill
billid billdate

It ensures that the cno and the pno on the bill are both correct, but doesn't guarantee that the contractor actually worked on the project! Asns Bills
billid billdate cno ! pno ! cno pno hrsPerWeek

Contrs
cno

Projs
pno
Ellis Cohen 2001-2008 103

M:N Relationship with a Weak Bridge Entity Class


This system keeps track of which tools were used during an assignment (when a contractor worked on a project)

Contractor
cno

Assignment
hrsPerWeek used

Project
pno

Tool
toolid
Ellis Cohen 2001-2008

What's the relational schema?

104

Mapping M:N Relationship from Bridge


Visual Conceptual Model (Crow Magnum) Contractor
cno

Assignment
hrsPerWeek uses

Project
pno

Tool
toolid

Visual Relational Model (Relational Schema) Uses


cno pno toolid

Asns
cno pno hrsPerWeek
This represents a 3way combination of contractors, projects & tools

Contrs
cno

Projs Tools
toolid

Ellis Cohen 2001-2008

pno

105

SQL for M:N Relationship from Bridge


Uses
cno pno toolid

Asns
cno pno hrsPerWeek

Contrs
cno

Projs Tools
toolid pno

CREATE TABLE Uses( cno number(5), pno number(6), toolid number(5) references Tools on delete cascade, primary key( cno, pno, toolid ), foreign key( cno, pno ) references Asns on delete cascade );
Ellis Cohen 2001-2008 106

Simplifying Requirements
Contractor
cno used

Assignment
hrsPerWeek

Project
pno

Tool
toolid

Suppose we didn't need to keep track of how many hours each contractor worked on each project, but just wanted to know which tools each contractor use on each project
Ellis Cohen 2001-2008 107

3-Way Weak Bridge Relationship


Visual Conceptual Model (Crow Magnum) Contractor
cno

Uses

Project
pno

Tool
toolid

Visual Relational Model (Relational Schema) Uses Tools


toolid cno pno toolid

Contrs
cno

Projs
pno

Ellis Cohen 2001-2008

108

SQL for 3-Way Weak Bridge Relationship


Contrs Uses Tools
toolid cno pno toolid cno

Projs
pno

CREATE TABLE Uses( cno number(5) references Contrs on delete cascade, pno number(6) references Projs on delete cascade, toolid number(5) references Tools on delete cascade, primary key( cno, pno, toolid );
Ellis Cohen 2001-2008 109

Potrebbero piacerti anche