Download presentation
Presentation is loading. Please wait.
Published byJonah Franklin Modified over 8 years ago
1
1 Chapter 9 Chapter 9 Relational Database Design by ER- to-Relational Mapping
2
2Introduction Design a relational database schema Based on a conceptual schema design How to convert the basic ER model constructs into relations?
3
3 S(s1, …,, ) T(t1, …) Binary 1:1 relationship Either choose S and place the primary key of T as a foreign key in S. S R T r1 s1 t1 r1 FK 11
4
4 T(t1, …,, ) S(s1, …) Binary 1:1 relationship Or choose T and place the primary key of S as a foreign key in T. S R T r1 s1 t1 s1 r1 FK * It is always better to choose the entity with total participation 11
5
5 DEPARTMENTMANAGER manages Dname location No Name date This Department (Dname, location) Manager (No, Name, Dname, date) OR Department (Dname, location, no, date) Manager (No, Name) This is better Example: Binary 1:1 relationship 1 1
6
6 Binary 1:N relationship TS R 1 N t1 r1r2s1 S(s1, …, t1, r1, r2) T(t1, …) T(t1, ……, s1, r1, r2) S(s1, …) We place the FK in the entity which has cardinality 1
7
7 Binary M:N relationship E1 E2 R MN r1 e1 e2 E1(e1, …..) E2(e2, …..) R(e1, e2, r1, r2) Create a new relation R to represent R e1 is FK in R references E1 and e2 is FK in R references E2 Both form a PK for R
8
8 Multi-valued attributes E1 e1 K E1(K, …………) R(K, e1) K is a FK in R references E1 and K and e1 form a PK for R
9
9 EMPLOYEE SSN Phone EMPLOYEE(SSN, ………) PHONE(SSN, Phone#) Another solution, but above is better EMPLOYEE(SSN, …, phone1, phone2, phone3) Multi-valued attributes
10
10 For each n-ary relationship R, n > 2, create a new relation to represent R. The primary key of R will be the primary keys of all entity types that participate in R n-ary relationship R, n > 2,
11
11 E3E2 E1 R e1 e2 e3 E1(e1, …)E2(e2, …) E3(e3, …)R(e1, e2, e3, r1) r1 n-ary relationship R, n > 2,
12
12 Weak Entity Type E e1 e2e3 r W w1 w2 w3 w5 w4 E(e1, e2, e3, …) W(e1, w1, w2,w4, w5) e1 is a FK in W references E and both w1 and e1 form a PK of W
13
13 An Example (1) advises PROFFESOR SSN Name Rank 1m STUDENT SSN NameGPA
14
14 An Example (2) Two relations are sufficient: Professors Students Professors Students SSN Name Rank SSN Name GPA PSSN 123 Jack Prof. 456 John 3.4 123 234 Ann Prof. 567 Carl 3.2 123 345 Bob Prof. 678 Ken 3.5 345
15
15 Transform Binary Relationship (1) Case 1: one-to-many relationship: x = 1 and y = m (or x = (?, m) and y = (?, 1)) E(A, B), F(C, D, A) Relationship R is transformed to a foreign key Attribute A in E is a foreign key F E A B D R xy C
16
16 Transform Binary Relationship (2) Depts(Name, Location) Employees(SSN, Name, Age, Dept_name) Renaming is useful for understandability. work_inEmployees SSN Name Age m 1 Depts NameLocation
17
17 Transform Binary Relationship (3) Case 2: one-to-one relationship Case 2.1: x = (1, 1) and y = (1, 1) This one E(A, B), F(C, D, A) or This one E(A, B, C), F(C, D) F E A B D R xy C
18
18 Transform Binary Relationship (4) ==> Depts(Name, Location) Managers(SSN, Name, Age, Dept_name) Managers(SSN, Name, Age, Dept_name) ==> Depts(Name, Location, Manager_SSN) Managers(SSN, Name, Age) Managers(SSN, Name, Age) work_inManagers SSN Name Age (1, 1) Depts NameLocation
19
19 Transform Binary Relationship (5) Case 2.2: x = (0, 1) and y = (0, 1) (or x = 1 and y = 1, and both E and F are partial participation) (or x = 1 and y = 1, and both E and F are partial participation) Use the same transformation rule for Case 2.1.
20
20 Transform Binary Relationship (6) Case 2.3: x = (0, 1) and y = (1, 1) ===> E(A, B), F(C, D, A) The entity set with the total participation is transformed to a relation with a foreign key. F E A B D R xy C
21
21 Transform Binary Relationship (7) ==> Depts(Name, Location, Manager_SSN) Employees(SSN, Name, Age) Employees(SSN, Name, Age) Why not let Employees have the foreign key? managesEmployees SSN Name Age (0, 1)(1, 1) Depts NameLocation
22
22 Transform Binary Relationship (8) Case 2.4: x = (1, 1) and y = (0, 1) ===> E(A, B, C), F(C, D) F E A B D R xy C
23
23 Transform Binary Relationship (9) Case 3: many-to-many relationship: x = m and y = n x = m and y = n Case 3.1: R has no attribute ===> E(A, B), F(C, D), R(A, C) Transform the m-to-m relationship to a separate relation. R has two foreign keys. The key of R consists of the foreign keys
24
24 Transform Binary Relationship (10) ==> Students(SSN, Name, Age) Courses(Course#, Title) Courses(Course#, Title) Takes(SSN, Course#) Takes(SSN, Course#) Case 3.2: R has attribute Z ===> E(A, B), F(C, D), R(A, C, Z) takesStudents SSN Name Age m n Courses Course#Title
25
25 Transform Ternary Relationship ==> E1(A, B), E2(C, D), E3(G, H), R(A, C, G, Z) R(A, C, G, Z) E2 D R C E1 B A E3 H G Z
26
26 Transform Recursive Relationship (1) Create a shadow entity set and transform the unary relationship into a binary relationship. Apply the rules for transforming binary relationships. After the transformation, remove one redundant relation, or if there is no redundant relation, remove the relation with fewer attributes.
27
27 Transform Recursive Relationship (2) Courses(Course#, Title) Prereq(Course#, Prereq-Course#) prereq Courses TitleCourse# mn Courses TitleCourse# prereq m n Courses Title Course#
28
28 Transform Recursive Relationship (3) Persons(SSN, Name, Age, Spouse_SSN) married_to Persons Name SSN (0,1) mar_to (0,1) Age Persons Name SSN Age Persons (0,1)
29
29 Transform Recursive Relationship (4) Persons(SSN, Name, Age, Mother_SSN) mother_of Persons Name SSN 1 mother m child mo_of 1 Age Persons Name SSN Age Persons m
30
30 Transform Multi-valued Attribute (1) Create a separate relation for each multi-valued attribute. E_C.A should be defined to be a foreign key referencing E.A E B A C E(A, B), E_C(A, C)
31
31 Transform Multi-valued Attribute (2) ==> Books (ISBN, Title, Publisher) Book_Authors (ISBN, Author) Book_Authors (ISBN, Author) Define Book_Authors.ISBN as a foreign key referencing Books.ISBN Books Publisher ISBN Authors Title
32
32 Transform Composite Attribute (1) Method 1: Use only simple attributes and ignore the composite attribute. ==> ==> E B A C E(A, D, H, C) D H
33
33 Transform Composite Attribute (2) Method 2: Transform the composite attribute to a separate relation. ==> ==> E B A C E(A, C), E_B (A, D, H) D H
34
34 Transform Composite Attribute (3) An Example using method 2: An Example using method 2: Employees Picture SSNAge Employees (SSN, Name, Age, Salary) Emp_Pic (SSN, Bitmap, Format, Height, Width) Format Height Name Width Salary Bitmap
35
35 Transform Weak Entity Set ==> E (A, B, C), F(A, D, G, H) The key of F consists of the key of E and the partial key of F. F.A is a foreign key referencing E.A E B A C F G D H R 1m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.