1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 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
2 © Ellis Cohen 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
3 © Ellis Cohen M:N Relationships
4 © Ellis Cohen M:N Relationships EmployeeProject assigned to Assignment: (*) Employee assigned to (*) Project Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText) Each employee may be assigned to a number of projects Each project may have a number of employees
5 © Ellis Cohen M:N Relationship Notations EmployeeProject assigned to EmployeeProject EmployeeProject assigned to * * UML Crow Magnum Easy Crow Magnum assigned to EmployeeProject assigned to Chen No key constraints!
6 © Ellis Cohen 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
7 © Ellis Cohen 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 Emps empno ename address Use singular for entity class Use plural for table 7499ALLEN12 Lehigh … 7654MARTIN1400 … 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… 2618Payroll 2621Bridge Design 2622Update Reqs Projs pno pname …
8 © Ellis Cohen M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN MARTIN BLAKE KING TURNER STERN … pno pname … 2621… 2622…
9 © Ellis Cohen Relationships Tables Emps empno ename address 7499ALLEN MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno Asns Composite primary key 2618… 2621… 2622… Projs pno pname … Bridge Table EmployeeProject assigned to empno … pno …
10 © Ellis Cohen Tables & Relational Schema Emps empno ename address 7499ALLEN MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno Asns … 2621… 2622… Projs pno pname … Visual Relational Model (Relational Schema) empno pno Projs pno pname budget Emps empno ename Asns Note cascading deletes
11 © Ellis Cohen M:N Relationships and Cascading Delete
12 © Ellis Cohen M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN MARTIN BLAKE KING TURNER STERN … pno pname … 2621… 2622… If a project is deleted, what happens to its assignment links?
13 © Ellis Cohen No Dangling Links empno ename address empnopno EmployeeProject assigned to 7499ALLEN MARTIN BLAKE KING TURNER STERN … 2622… Links that don't connect to an entity instance are not allowed!
14 © Ellis Cohen M:N & Cascading Delete empno … EmployeeProject assigned to empno pno Projs pno pname budget Emps empno ename The standard relational mapping for an M:N relationship uses cascading delete Asns Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) pno …
15 © Ellis Cohen Mapping M:N to Relational Schema CONCEPTUAL Model RELATIONAL Model RELATIONAL Mapping Assignment mapped to Asns( empno, pno ), with empno references Emps on cascade delete pno references Projs on cascade delete Assignment: (*) Employee assigned to (*) Project Asns empno number(4) references Emps on cascade delete pno number(3) references Projs on cascade delete primary key( empno, pno )
16 © Ellis Cohen M:N w Cascading Delete in SQL empno pno Projs pno pname budget Emps empno ename Asns CREATE TABLE Asns( empnonumber(5) references Emps on delete cascade, pnonumber(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?
17 © Ellis Cohen Eliminating Cascading Delete empno pno Projs pno pname budget Emps empno ename empno pno Projs pno pname budget Emps empno ename Asns + An employee assigned to a project can't be deleted + A project with employees assigned to it can't be deleted Added business rules can eliminate cascading delete from the relational model
18 © Ellis Cohen Implication of No Cascading Deletes Asns empno pno Projs pno pname budget Emps empno ename 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!
19 © Ellis Cohen Incorrect M:N Relational Mapping Projs pno pname budget empno Emps empno ename pno empno ename pno pname budget EmployeeProject assigned to Visual Conceptual Model (Crow Magnum) What's WRONG with this relational mapping? WRONG!
20 © Ellis Cohen Incorrect M:N Relational Mapping Explanation Projs pno pname budget empno Emps empno ename pno 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! EmployeeProject assigned to 1:1 relationship
21 © Ellis Cohen Reflexive M:N Relationships Employee likes to work with What's the corresponding relational schema? empno ename
22 © Ellis Cohen Reflexive M:N Mapping Employee likes to work with empno ename LikesWorkingWith emp1 emp2 Emps empno ename
23 © Ellis Cohen SQL for Reflexive M:N Mapping LikesWorkingWith emp1 emp2 Emps empno ename CREATE TABLE LikesWorkingWith( emp1number(5) references Emps on delete cascade, emp2number(6) references Emps on delete cascade, primary key( emp1, emp2 ) );
24 © Ellis Cohen Joins Involving M:N Relationships
25 © Ellis Cohen Joins with M:N Relationships EmployeeProject assigned to 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 empno pno Projs pno pname budget Emps empno ename Asns
26 © Ellis Cohen 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? Employee Project Employee Project Employee Project Employee Project Consider each of these 4 cases
27 © Ellis Cohen 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? Employee Project Employee Project 12 Employee Project Employee Project 4 3
28 © Ellis Cohen Bridge Entity Classes
29 © Ellis Cohen M:N Relationships as Entity Classes 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 Sometimes an M:N relationship is thought of at the conceptual level as an entity class in its own right:
30 © Ellis Cohen Bridge Entity Classes for M:N Relationships EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns empno pno Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) Note, no primary key and no cascading delete
31 © Ellis Cohen Bridge Classes vs M:N Asns empno pno Projects pno pname budget Emps empno ename 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
32 © Ellis Cohen 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
33 © Ellis Cohen 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
34 © Ellis Cohen Mapping Bridge Entity Classes with Primary Keys Asns asnid empno ! pno ! Projs pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asnid
35 © Ellis Cohen Bridge Class Attributes
36 © Ellis Cohen Relationship Attributes empnopno EmployeeProject assigned to 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?
37 © Ellis Cohen Bridge Classes With Attributes Relationship Attribute: How many hrs per week is the employee assigned to the project EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asnid hrsPerWeek
38 © Ellis Cohen Mapping Bridge Entity Classes with Attributes Asns asnid empno ! pno ! hrsPerWeek Projs pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asnid hrsPerWeek
39 © Ellis Cohen Attributes Example Emps empno … … 7698… 7839… 7844… 7986… asnid empno pno hrsPerWeek Asns … 2621… 2622… Projs pno …
40 © Ellis Cohen Partial Associations
41 © Ellis Cohen M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN MARTIN BLAKE KING TURNER STERN … 2622… Links that don't connect to an entity instance are not allowed!
42 © Ellis Cohen Standard M:N Assignments EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns asnid empno ! pno ! Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) Every assignment must have both an employee and a project asnid
43 © Ellis Cohen Partial Assignments Asns asnid empno pno ! job Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) Bridge classes allow partial assignments: An assignment can be specified for a project, without having an employee to fill it. EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asnid job
44 © Ellis Cohen Partial Association Example asnid pno empno job Asns MGR MGR DEV MGR 52622DEV 62622DEV Project 2622 needs two developers, but no employee has been assigned to those positions yet
45 © Ellis Cohen Using Dependent & Identifying Relationships
46 © Ellis Cohen Specifying Lifetime Dependency EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns asnid empno pno ! job Projs pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) With bridge classes, lifetime dependency can be selectively added asnid job
47 © Ellis Cohen Partially Dependent Bridge Class EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Visual Conceptual Model (Crow Magnum) [Employee, Project] Assignment( asnid, job ) (1) Employee assigned via (*) Assignment (1) Project assigned via (*) Assignment Textual Conceptual Model (Brief ConText) asnid job When the project is deleted, all assignment for the projects (including partial ones) are deleted
48 © Ellis Cohen Using an Identifying Relationship Visual Conceptual Model (Crow Magnum) EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asndx job [Project] Assignment( asndx, job ) (1) Employee assigned via (*) Assignment (1) Project assigned via (*) Assignment Textual Conceptual Model (Brief ConText) What's the relational schema diagram?
49 © Ellis Cohen Mapping the Identifying Relationship Asns pno asndx empno job Projs pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asndx job
50 © Ellis Cohen Uniqueness
51 © Ellis Cohen Uniqueness Issue EmployeeAssignment assigned via empno ename Project pno pname budget assigned via asnid job hrsPerWeek Could an employee have more than one assignment for the same project?
52 © Ellis Cohen Multiple Assignments Example asnid pno empno hrsPerWeek job Asns 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?
53 © Ellis Cohen Add Uniqueness Constraint EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns asnid empno ! pno ! job hrsPerWeek Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) + There is at most one assignment for the same employee, project and job asnid job hrsPerWeek
54 © Ellis Cohen Uniqueness Constraint in SQL CREATE TABLE Asns( asnidnumber(8) primary key, empnonumber(5) not null references Emps, pnonumber(6) not null references Projs, jobvarchar(10), hrsPerWeek number(2), unique( empno, pno, job ) ); Asns asnid empno ! pno ! job hrsPerWeek Projects pno pname budget Emps empno ename
55 © Ellis Cohen Stronger Uniqueness Constraint EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns asnid empno ! pno ! job hrsPerWeek Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) + There is at most one assignment for the same employee and project asnid job hrsPerWeek Doesn’t allow a person to have two jobs on the same project
56 © Ellis Cohen Eliminate Primary Key EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns empno ! pno ! job hrsPerWeek Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) + There is at most one assignment for the same employee and project hrsPerWeek job No need for PK
57 © Ellis Cohen Upgrade to Relational PK EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns empno pno job hrsPerWeek Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) + There is at most one assignment for the same employee and project hrsPerWeek job No need for PK Since empno + pno are unique and non-null, and there is no primary key, we make empno + pno the primary key during relational mapping
58 © Ellis Cohen M:N Relationships & Bridge Classes Asns empno pno ! Projs pno Emps empno Asns empno pno Projs pno Emps empno EmpAsn empno Proj pno EmpAsn empno Proj pno + There is at most one assignment for the same employee and project Emp empno Proj pno assigned to + An employee assigned to a project can't be deleted & vice versa
59 © Ellis Cohen M:N Relationships & Bridge Classes w Dependency Asns empno pno ! Projs pno Emps empno Asns empno pno Projs pno Emps empno EmpAsn empno Proj pno + There is at most one assignment for the same employee and project Emp empno Proj pno EmpAsn empno Proj pno + An employee assigned to a project can't be deleted assigned to
60 © Ellis Cohen Weak Bridge Classes
61 © Ellis Cohen Weak Project Assignments Suppose we only had an Employee class & an Assignments class (but no Project class) We might represent their relationship as: EmployeeAssignment assigned via empno ename pno An employee has many project assignments. An employee's project assignments are discriminated by the project number Asns empno pno Emps empno ename
62 © Ellis Cohen Weak Employee Assignments Suppose we only had an Project class & an Assignments class (but no Employee class) We might represent their relationship as: empno A project has many employee assignments. A project's employee assignments are discriminated by the employee number AssignmentProject pno pname budget assigned via Asns empno pno Projs pno pname budget
63 © Ellis Cohen Weak Bridge Entity Classes EmployeeAssignment assigned via empno ename Project pno pname budget assigned via A Weak Bridge Entity Class is a Weak Entity Class that has multiple identifying relationships Asns empno pno Projs pno pname budget Emps empno ename Visual Relational Model (Relational Schema) Relational "fingerprint" of a Weak Bridge Entity Class (or an M:N relationship) Assignment does not need an instance discriminator. The combination of the identities of its two identifying owners uniquely identifies an assignment
64 © Ellis Cohen Weak Bridge Entity Classes and M:N Relationships EmployeeAssignment assigned via empno ename Project pno pname budget assigned via empno ename pno pname budget EmployeeProject assigned to Identical
65 © Ellis Cohen M:N Relationships & Weak & Dependent Bridge Classes Asns empno pno Projs pno Emps empno EmpAsn empno Proj pno Emp empno Proj pno EmpAsn empno Proj pno + There is at most one assignment for the same employee and project
66 © Ellis Cohen Composite Primary Keys with Partial Dependency Asns empno pno Projs pno Emps empno EmpAsn empno Proj pno Emp empno Proj pno EmpAsn empno Proj pno + There is at most one assignment for the same employee and project + An employee with assignments cannot be deleted + An employee assigned to a project can't be deleted assigned to
67 © Ellis Cohen Weak Bridge Classes with Attributes
68 © Ellis Cohen Mapping Bridge Entity Classes with Attributes Asns empno pno hrsPerWeek Projs pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) hrsPerWeek EmployeeAssignment assigned via empno ename Project pno pname budget assigned via
69 © Ellis Cohen Attribute Example Emps empno ename address 7499ALLEN MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno hrsPerWeek Asns … 2621… 2622… Projs pno …
70 © Ellis Cohen Weak Bridge Entity Class Models hrsPerWeek 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 Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText) EmployeeAssignment assigned via empno ename Project pno pname budget assigned via just an ordinary class attribute
71 © Ellis Cohen M:N Relationships with Attributes EmployeeProject * * UML Assignment hrsPerWeek EmployeeProject assigned to hrsPerWeek Chen Crow Magnum Easy Crow Magnum hrsPerWeek EmployeeAssignment assigned via Project assigned via hrsPerWeek EmployeeAssignment assigned via Project assigned via Association Class
72 © Ellis Cohen No Dependent Bridge Classes in UML * * UML Assignment hrsPerWeek Employee PK empno Project PK pno Employee PK empno Project PK pno Assignment hrsPerWeek * * UML 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 Association Class
73 © Ellis Cohen UML Qualifying Relationships Employee Assignment PK empno hrsPerWeek pno Project PK pno empno assigned via 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
74 © Ellis Cohen Weak Bridge Classes with Participation and Cardinality
75 © Ellis Cohen Bridge Entity Classes & M:N Participation EmployeeProject empnopno assigned to Identical EmployeeAssignment assigned via empno Project pno assigned via
76 © Ellis Cohen Assertions for Mandatory Participation Asns empno pno Projs pno pname budget Emps empno ename (SELECT count(*) FROM Projs) = (SELECT count(DISTINCT pno) FROM Asns) EmployeeProject empnopno assigned to Every project has at least one employee Every project has at least one assignment
77 © Ellis Cohen Bridge Entity Classes & M:N Cardinality 0..4 EmployeeProject empnopno assigned to Essentially Identical EmployeeAssignment empno Project pno assigned via
78 © Ellis Cohen 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?
79 © Ellis Cohen Modeling 1:M/M:N Related Relationships EquipmentResearcher manages permitted to use Which conceptual state constraint is also required? Why?
80 © Ellis Cohen :M/M:N w State Constraint EquipmentResearcher manages permitted to use + State Constraint: If a researcher manages a piece of equipment, then that researcher must also be permitted to use it Mandatory participation implied by state constraint What's the corresponding relational model?
81 © Ellis Cohen rschid rname 1:M/M:N Relational Models Permissions rschid eqpid Equipment eqpid eqpnam mgr ! Researchers 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))
82 © Ellis Cohen Bridge Class Representation EquipmentResearcher manages Permission Is it possible to eliminate the manages relationship?
83 © Ellis Cohen Using Boolean Attributes could generalize this to accessLevel isManager Researcher Permission permitted via Equipment permitted via Which conceptual state constraint is also required?
84 © Ellis Cohen Required Constraint isManager Researcher Permission permitted via Equipment permitted via + State Constraint: Every piece of equipment is managed by a single researcher What's the corresponding relational model?
85 © Ellis Cohen :M/M:N Related Relational Models Permissions rschid eqpid isManager Equipment eqpid eqpnam Researchers rschid rname 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
86 © Ellis Cohen rschid rname Using Inclusion Constraints If a researcher manages a piece of equipment, then that researcher must also be permitted to use it rschid rname Permissions eqpid rschid Equipment eqpid eqpnam mgr ! Researchers Can be represented directly in the relational model! Permissions eqpid rschid Equipment eqpid eqpnam mgr ! Researchers
87 © Ellis Cohen Discriminated Weak Bridge Classes
88 © Ellis Cohen Discriminated Weak Bridge Classes [Employee, Project] Assignment( job, hrsPerWeek ) Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText) An employee may have multiple assignments to the same project These are discriminated by job EmployeeAssigtnment for Project with empno ename pno pname budget job hrsPerWeek
89 © Ellis Cohen Mapping Discriminated Weak Bridge Entity Classes Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) EmployeeAssigtnment for Project with empno ename pno pname budget job hrsPerWeek Asns empno pno job hrsPerWeek Projs pno pname budget Emps empno ename
90 © Ellis Cohen SQL for Discriminated Weak Bridge Entity Class Asns empno pno job hrsPerWeek Projs pno pname budget Emps empno ename CREATE TABLE Asns( empnonumber(5) not null references Emps, pnonumber(6) not null references Projs, jobvarchar(10), hrsPerWeek number(2), primary key( empno, pno, job ) );
91 © Ellis Cohen Add Surrogate Primary Key EmployeeAssignment assigned via empno ename Project pno pname budget assigned via Asns asnid empno ! pno ! job hrsPerWeek Projects pno pname budget Emps empno ename Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) + There is at most one assignment for the same employee, project and job asnid job hrsPerWeek Add surrogate PK add uniqueness constraint
92 © Ellis Cohen Discriminated Bridge Classes Asns empno pno job Projs pno Emps empno EmpAsn empno Proj pno job EmpAsn empno Proj pno job + There is at most one assignment for the same employee, project & job
93 © Ellis Cohen Discriminated Bridge Classes with Partial Dependency Asns empno ! pno job Projs pno Emps empno EmpAsn empno Proj pno job + There is at most one assignment for the same employee, project & job EmpAsn empno Proj pno job + An employee with assignments cannot be deleted
94 © Ellis Cohen Relationships with Bridge Entity Classes
95 © Ellis Cohen :M Relationship to a Weak Bridge Entity Class Contractor Project hrsPerWeek cno … pno … Agency agencyid Suppose that when a contractor works on a project, they work through a single agency. Any particular agency may arrange any number of assignments. What's the relational schema? Assignment
96 © Ellis Cohen Mapping 1:M Relationship to Bridge Contractor Project hrsPerWeek cno … pno … Agency agencyid Assignment Agencies agencyid … Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) Asns cno pno hrsPerWeek agencyid Projs pno … Contrs cno …
97 © Ellis Cohen SQL for 1:M Relationship to Bridge Agencies agencyid … Asns cno pno hrsPerWeek agencyid Projs pno … Contrs cno … CREATE TABLE Asns( cnonumber(5) not null references Contrs on delete cascade, pnonumber(6) not null references Projs on delete cascade, hrsPerWeek number(2), agencyidnumber(5) references Agencies, primary key( cno, pno ) );
98 © Ellis Cohen UML Bridge Relationship * * UML Assignment hrsPerWeek Contractor PK cno Project PK pno Agency PK agencyid *
99 © Ellis Cohen :M Relationship from a Weak Bridge Entity Class Bill billid billdate When a contractor works on a project, they submit multiple bills. Each bill is for only one assignment. What's the relational schema? Contractor Project hrsPerWeek cno … pno … Assignment
100 © Ellis Cohen Mapping 1:M Relationship from Bridge Contractor Project hrsPerWeek cno … pno … Bill Assignment Asns cno pno hrsPerWeek Projs pno … Contrs cno … Bills billid billdate cno ! pno ! Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) If an assignment can’t be deleted if it has any outstanding bills, what happens when an attempt is made to delete a contractor? billid billdate
101 © Ellis Cohen SQL for 1:M Relationship from Bridge Asns cno pno hrsPerWeek Projs pno … Contrs cno … Bills billid billdate cno ! pno ! CREATE TABLE Bills( billidnumber(8) primary key, billdatedate, cnonumber(5) not null, pnonumber(6) not null, foreign key( cno, pno ) references Asns );
102 © Ellis Cohen cno … Possible Representation? Contractor Project hrsPerWeek cno … pno … Bill Assignment Asns cno pno hrsPerWeek Projs pno … Contrs Would this relational model work instead? billid billdate Bills billid billdate cno ! pno !
103 © Ellis Cohen cno … Incorrect Representation Contractor Project hrsPerWeek cno … pno … Bill Assignment Asns cno pno hrsPerWeek Projs pno … Contrs 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! Bills billid billdate cno ! pno ! billid billdate
104 © Ellis Cohen M:N Relationship with a Weak Bridge Entity Class Tool toolid This system keeps track of which tools were used during an assignment (when a contractor worked on a project) What's the relational schema? used Contractor Project hrsPerWeek cno … pno … Assignment
105 © Ellis Cohen Mapping M:N Relationship from Bridge Contractor Project hrsPerWeek cno … pno … Tool toolid Assignment Asns cno pno hrsPerWeek Projs pno … Contrs cno … Uses cno pno toolid Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) uses Tools toolid … This represents a 3- way combination of contractors, projects & tools *
106 © Ellis Cohen SQL for M:N Relationship from Bridge Asns cno pno hrsPerWeek Projs pno … Contrs cno … Uses cno pno toolid Tools toolid … CREATE TABLE Uses( cnonumber(5), pnonumber(6), toolidnumber(5) references Tools on delete cascade, primary key( cno, pno, toolid ), foreign key( cno, pno ) references Asns on delete cascade );
107 © Ellis Cohen Simplifying Requirements Tool toolid used Contractor Project hrsPerWeek cno … pno … Assignment 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
108 © Ellis Cohen Way Weak Bridge Relationship Contractor Project cno … pno … Tool toolid Uses Projs pno … Contrs cno … Uses cno pno toolid Visual Conceptual Model (Crow Magnum) Visual Relational Model (Relational Schema) Tools toolid …
109 © Ellis Cohen SQL for 3-Way Weak Bridge Relationship CREATE TABLE Uses( cnonumber(5) references Contrs on delete cascade, pnonumber(6) references Projs on delete cascade, toolidnumber(5) references Tools on delete cascade, primary key( cno, pno, toolid ); Projs pno … Contrs cno … Uses cno pno toolid Tools toolid …