Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 M:N Relationships & Bridge Classes These.

Similar presentations


Presentation on theme: "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 M:N Relationships & Bridge Classes These."— Presentation transcript:

1 1 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

2 2 © Ellis Cohen 2001-2008 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 3 © Ellis Cohen 2001-2008 M:N Relationships

4 4 © Ellis Cohen 2001-2008 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 5 © Ellis Cohen 2001-2008 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 6 © 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

7 7 © 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 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 8 © Ellis Cohen 2001-2008 M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN... 7654MARTIN... 7698BLAKE... 7839KING... 7844TURNER... 7986STERN... 2618… pno pname … 2621… 2622…

9 9 © Ellis Cohen 2001-2008 Relationships  Tables Emps empno ename address 7499ALLEN... 7654MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno Asns 76542621 76982618 76982622 78442618 78442622 79862622 Composite primary key 2618… 2621… 2622… Projs pno pname … Bridge Table EmployeeProject assigned to empno … pno …

10 10 © Ellis Cohen 2001-2008 Tables & Relational Schema Emps empno ename address 7499ALLEN... 7654MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno Asns 76542621 76982618 76982622 78442618 78442622 79862622 2618… 2621… 2622… Projs pno pname … Visual Relational Model (Relational Schema) empno pno Projs pno pname budget Emps empno ename Asns Note cascading deletes

11 11 © Ellis Cohen 2001-2008 M:N Relationships and Cascading Delete

12 12 © Ellis Cohen 2001-2008 M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN... 7654MARTIN... 7698BLAKE... 7839KING... 7844TURNER... 7986STERN... 2618… pno pname … 2621… 2622… If a project is deleted, what happens to its assignment links?

13 13 © Ellis Cohen 2001-2008 No Dangling Links empno ename address empnopno EmployeeProject assigned to 7499ALLEN... 7654MARTIN... 7698BLAKE... 7839KING... 7844TURNER... 7986STERN... 2621… 2622… Links that don't connect to an entity instance are not allowed!

14 14 © Ellis Cohen 2001-2008 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 15 © Ellis Cohen 2001-2008 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 16 © Ellis Cohen 2001-2008 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 17 © Ellis Cohen 2001-2008 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 18 © Ellis Cohen 2001-2008 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 19 © Ellis Cohen 2001-2008 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 20 © Ellis Cohen 2001-2008 Incorrect M:N Relational Mapping Explanation Projs pno pname budget empno Emps empno ename pno 1 2 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! EmployeeProject assigned to 1:1 relationship

21 21 © Ellis Cohen 2001-2008 Reflexive M:N Relationships Employee likes to work with What's the corresponding relational schema? empno ename

22 22 © Ellis Cohen 2001-2008 Reflexive M:N Mapping Employee likes to work with empno ename LikesWorkingWith emp1 emp2 Emps empno ename

23 23 © Ellis Cohen 2001-2008 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 24 © Ellis Cohen 2001-2008 Joins Involving M:N Relationships

25 25 © Ellis Cohen 2001-2008 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 26 © Ellis Cohen 2001-2008 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 1 4 3 2

27 27 © Ellis Cohen 2001-2008 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? 200..10005..1000 200..10000..1000 Employee Project Employee Project 12 Employee Project Employee Project 4 3

28 28 © Ellis Cohen 2001-2008 Bridge Entity Classes

29 29 © Ellis Cohen 2001-2008 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 30 © Ellis Cohen 2001-2008 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 31 © Ellis Cohen 2001-2008 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 32 © Ellis Cohen 2001-2008 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 33 © Ellis Cohen 2001-2008 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 34 © Ellis Cohen 2001-2008 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 35 © Ellis Cohen 2001-2008 Bridge Class Attributes

36 36 © Ellis Cohen 2001-2008 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 37 © Ellis Cohen 2001-2008 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 38 © Ellis Cohen 2001-2008 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 39 © Ellis Cohen 2001-2008 Attributes Example Emps empno … 7499... 7654… 7698… 7839… 7844… 7986… asnid empno pno hrsPerWeek Asns 17499262112 2769826185 376982622 47844261822 5784426224 6798626221 2618… 2621… 2622… Projs pno …

40 40 © Ellis Cohen 2001-2008 Partial Associations

41 41 © Ellis Cohen 2001-2008 M:N Related Instances empno ename address empnopno EmployeeProject assigned to 7499ALLEN... 7654MARTIN... 7698BLAKE... 7839KING... 7844TURNER... 7986STERN... 2621… 2622… Links that don't connect to an entity instance are not allowed!

42 42 © Ellis Cohen 2001-2008 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 43 © Ellis Cohen 2001-2008 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 44 © Ellis Cohen 2001-2008 Partial Association Example asnid pno empno job Asns 126137839MGR 226187698MGR 326187499DEV 426227844MGR 52622DEV 62622DEV Project 2622 needs two developers, but no employee has been assigned to those positions yet

45 45 © Ellis Cohen 2001-2008 Using Dependent & Identifying Relationships

46 46 © Ellis Cohen 2001-2008 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 47 © Ellis Cohen 2001-2008 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 48 © Ellis Cohen 2001-2008 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 49 © Ellis Cohen 2001-2008 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 50 © Ellis Cohen 2001-2008 Uniqueness

51 51 © Ellis Cohen 2001-2008 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 52 © Ellis Cohen 2001-2008 Multiple Assignments Example asnid pno empno hrsPerWeek job Asns 12613783920MGR 22618769840MGR 32618749940DEV 42622784440MGR 52622731630DEV 62622731610LIB 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 53 © Ellis Cohen 2001-2008 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 54 © Ellis Cohen 2001-2008 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 55 © Ellis Cohen 2001-2008 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 56 © Ellis Cohen 2001-2008 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 57 © Ellis Cohen 2001-2008 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 58 © Ellis Cohen 2001-2008 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 59 © Ellis Cohen 2001-2008 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 60 © Ellis Cohen 2001-2008 Weak Bridge Classes

61 61 © Ellis Cohen 2001-2008 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 62 © Ellis Cohen 2001-2008 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 63 © Ellis Cohen 2001-2008 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 64 © Ellis Cohen 2001-2008 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 65 © Ellis Cohen 2001-2008 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 66 © Ellis Cohen 2001-2008 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 67 © Ellis Cohen 2001-2008 Weak Bridge Classes with Attributes

68 68 © Ellis Cohen 2001-2008 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 69 © Ellis Cohen 2001-2008 Attribute Example Emps empno ename address 7499ALLEN... 7654MARTIN… 7698BLAKE… 7839KING… 7844TURNER… 7986STERN… empno pno hrsPerWeek Asns 7654262112 769826185 76982622 7844261822 784426224 798626221 2618… 2621… 2622… Projs pno …

70 70 © Ellis Cohen 2001-2008 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 71 © Ellis Cohen 2001-2008 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 72 © Ellis Cohen 2001-2008 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 73 © Ellis Cohen 2001-2008 UML Qualifying Relationships Employee Assignment PK empno hrsPerWeek pno 1 0..1 Project PK pno empno 1 0..1 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 74 © Ellis Cohen 2001-2008 Weak Bridge Classes with Participation and Cardinality

75 75 © Ellis Cohen 2001-2008 Bridge Entity Classes & M:N Participation EmployeeProject empnopno assigned to Identical EmployeeAssignment assigned via empno Project pno assigned via

76 76 © Ellis Cohen 2001-2008 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 77 © Ellis Cohen 2001-2008 Bridge Entity Classes & M:N Cardinality 0..4 EmployeeProject empnopno 3..20 0..4 3..20 assigned to Essentially Identical EmployeeAssignment empno Project pno assigned via

78 78 © Ellis Cohen 2001-2008 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 79 © Ellis Cohen 2001-2008 Modeling 1:M/M:N Related Relationships EquipmentResearcher manages permitted to use Which conceptual state constraint is also required? Why?

80 80 © Ellis Cohen 2001-2008 1: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 81 © Ellis Cohen 2001-2008 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 82 © Ellis Cohen 2001-2008 Bridge Class Representation EquipmentResearcher manages Permission Is it possible to eliminate the manages relationship?

83 83 © Ellis Cohen 2001-2008 Using Boolean Attributes could generalize this to accessLevel isManager Researcher Permission permitted via Equipment permitted via Which conceptual state constraint is also required?

84 84 © Ellis Cohen 2001-2008 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 85 © Ellis Cohen 2001-2008 1: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 86 © Ellis Cohen 2001-2008 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 87 © Ellis Cohen 2001-2008 Discriminated Weak Bridge Classes

88 88 © Ellis Cohen 2001-2008 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 89 © Ellis Cohen 2001-2008 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 90 © Ellis Cohen 2001-2008 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 91 © Ellis Cohen 2001-2008 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 92 © Ellis Cohen 2001-2008 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 93 © Ellis Cohen 2001-2008 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 94 © Ellis Cohen 2001-2008 Relationships with Bridge Entity Classes

95 95 © Ellis Cohen 2001-2008 1: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 96 © Ellis Cohen 2001-2008 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 97 © Ellis Cohen 2001-2008 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 98 © Ellis Cohen 2001-2008 UML Bridge Relationship * * UML Assignment hrsPerWeek Contractor PK cno Project PK pno Agency PK agencyid *

99 99 © Ellis Cohen 2001-2008 1: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 100 © Ellis Cohen 2001-2008 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 101 © Ellis Cohen 2001-2008 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 102 © Ellis Cohen 2001-2008 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 103 © Ellis Cohen 2001-2008 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 104 © Ellis Cohen 2001-2008 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 105 © Ellis Cohen 2001-2008 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 106 © Ellis Cohen 2001-2008 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 107 © Ellis Cohen 2001-2008 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 108 © Ellis Cohen 2001-2008 3-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 109 © Ellis Cohen 2001-2008 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 …


Download ppt "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 M:N Relationships & Bridge Classes These."

Similar presentations


Ads by Google