1 / 123

Chapter 4 Logical Database Design and the Relational Model

Chapter 4 Logical Database Design and the Relational Model. Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane, WA 99258 chen@jepson.gonzaga.edu. Objectives. Define terms List five properties of relations

sheila
Télécharger la présentation

Chapter 4 Logical Database Design and the Relational Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4Logical Database Design andthe Relational Model Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane, WA 99258 chen@jepson.gonzaga.edu

  2. Objectives • Define terms • List five properties of relations • State two properties of candidate keys • Define first, second, and third normal form • Describe problems from merging relations • Transform E-R and EER diagrams to relations • Create tables with entity and relational integrity constraints • Use normalization to convert anomalous tables to well-structured relations

  3. Problem Solving for Modeling a Database Project User interview & Integrated Model ER or EER or OO ????? ????? ????? Normalization (3NF) IMPLEMENTATION Business Problem Study and Analyze w/Team

  4. Relation • Definition: A relation is a named, two-dimensional table of data • Table is made up of rows (records), and columns (attribute or field) • Not all tables qualify as relations • Requirements: • Every relation has a unique name. • Every attribute value is atomic (not multivalued, not composite) • Every row is unique (can’t have two rows with exactly the same values for all their fields) • Attributes (columns) in tables have unique names • The order of the columns is irrelevant • The order of the rows is irrelevant NOTE: all relations are in 1st Normal form

  5. Correspondence with ER Model • Relations (tables) correspond with entity types and with many-to-many relationship types • Rows correspond with entity instances and with many-to-many relationship instances • Columns correspond with attributes • NOTE: The word relation (in relational database) is NOT the same as the word relationship (in ER model)

  6. Key Fields • Keys are special fields that serve two main purposes: • Primary keys are unique identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique • Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parent relation (on the one side of the relationship) • Keys can be simple (a single field) or composite (more than one field) • Surrogate key • Keys usually are used as indexes to speed up the response to user queries (More on this in Ch. 5)

  7. Sample E-R Diagram (Figure 2-1)

  8. Primary Key Foreign Key (implements 1:N relationship between customer and order) Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product) Figure 4-3 Schema for four relations (Pine Valley Furniture Company)

  9. Fig. 4-3: Schema for four relations (Pine Valley Furniture) Graphical and Text Representations CUSTOMER(Customer_ID, Customer_name,Address, City,State,Zip) ORDER(Order_ID, Order_Date,Customer_ID) ORDER_LINE(Order_ID, Product_ID,Quantity) PRODUCT(Product_ID, Product_Description, Product_Finish,Unit_Price, On_Hand)

  10. EmpID Name DeptName Salary 100 Margaret Simpson Marketing 48,000 140 Allen Beeton Accounting 52,000 110 Chris Lucero Info. System 43,000 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 Fig. 4-1: EMPLOYEE1 Relation with sample data EMPLOYEE1

  11. EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X Java 8/12/200X Fig. 4-2: Eliminating multi-valued attributes (a) Table with repeating groups or multi-valued attributes (Un-Normalized) EMPLOYEE

  12. EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X Java 8/12/200X EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X Figure 4-2 (a) Table with repeating groups – how to “remove” them (and solve the problem) Figure 4-2 (b) EMPLOYEE2 relation

  13. EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X Fig. 4-2: Eliminating multi-valued attributes (b) EMPLOYEE2 Relation (Normalized) EMPLOYEE2

  14. Constraints • Domain Constraints • Allowable values for an attribute. • A domain definition contains: domain name, data type, size, meaning, and allowable values/range (if applicable). • Entity Integrity • No primary key attribute may be null. • Referential Integrity • A relationship between primary key and foreign key. • Operational Constraints • Business rules (see Chapter 3)

  15. Integrity Constraints • Referential Integrity – rule that states that any foreign key value (on the relation of the many side) MUST match a primary key value in the relation of the one side. (Or the foreign key can be null) • For example: Delete Rules • Restrict – don’t allow delete of “parent” side if related rows exist in “dependent” side • Cascade – automatically delete “dependent” side rows that correspond with the “parent” side row to be deleted e.g., DROP TABLE tablename CASCADE CONSTRAINTS; (p.110 of Oracle 11g) • Set-to-Null – set the foreign key in the dependent side to null if deleting from the parent side  not allowed for weak entities

  16. Fig. 4-5: Referential integrity constraints (Pine Valley Furniture) pk fk pk Referential integrity constraints are drawn via arrows from dependent to parent table cpk/pk fk fk pk

  17. Referential integrity constraints are implemented with foreign key to primary key references Figure 4-6 SQL table definitions

  18. Referential Integrity (Addition and Deletion) PK FK

  19. Well-Structured Relations • A well-structured relation contains minimal redundancy and allows users to insert, modify, and delete the rows in a table without errors or inconsistencies. • The following anomalies should be removed for a well-structured relation: • Insertion Anomaly • Deletion Anomaly • Modification Anomaly

  20. EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X Is EMPLOYEE2 a Well-Structured relation? EMPLOYEE2

  21. EMPLOYEE2 EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X

  22. Is EMPLOYEE2 a Well-Structured relation? No/Yes WHY?

  23. Insertion Anomaly: Inserting a new row, the user must supply values for both EmpID (PK) and CourseTitle (CK and FK). This is an (insertion) anomaly, since the user should be able to enter employee data without knowing (supplying) course (title) data. EMPLOYEE2 EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X

  24. Deletion Anomaly: Deleting the employee number 140, it results in losing not only the employee’s information but also the course had an offering that completed on that date. EMPLOYEE2 EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X

  25. Modification Anomaly: If the employee number 100 gets a salary increase, we must record the increase in each of the rows for that employee (two occurences); otherwise the data will be inconsistent. EMPLOYEE2 EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X

  26. EmpID Name DeptName Salary Course Date Title Completed 100 Margaret Simpson Marketing 48,000 SPSS 6/19/200X 100 Margaret Simpson Marketing 48,000 Surveys 10/7/200X 140 Allen Beeton Accounting 52,000 Tax Acc 12/8/200X 110 Chris Lucero Info. System 43,000 SPSS 1/12/200X 110 Chris Lucero Info. System 43,000 C++ 4/22/200X 190 Lorenzo Davis Finance 55,000 150 Susan Martin Marketing 42,000 SPSS 6/16/200X 150 Susan Martin Marketing 42,000 Java 8/12/200X EMPLOYEE1 EMP_COURSE EmpID Name DeptName Salary 100 Margaret Simpson Marketing 48,000 140 Allen Beet Accounting 52,000 110 Chris Lucero Info. System 43,000 190 Lorenzo Davis Finance 55,000 150 Sususan Martin Marketing 42,000 EmpIDCourse Date Title Completed 100 SPSS 6/19/200X 100 Surveys 10/7/200X 140 Tax Acc 12/8/200X 110 SPSS 1/12/200X 110 C++ 4/22/200X 150 SPSS 6/19/200X 150 Java 8/12/200X ?? Fig. 4-7: EMP_COURSE: Normalized Relations from EMPLOYEE2 EMPLOYEE2 Is there any ano-maly?

  27. User interview & Integrated Model ER or EER or OO Relations Transformation (Seven Cases) Transformation to Relations ????? Normalization (3NF) IMPLEMENTATION Problem Solving for Modeling a Database Project Business Problem Study and Analyze w/Team

  28. Seven Cases of Transforming EE-R Diagrams into Relations 1. Map Regular Entities 2. Map Weak Entities 3. Map Binary Relationships 4. Map Associative Entities 5. Map Unary Relationships 6. Map Ternary (and n-ary) Relationships 7. Map Supertype/Subtype Relationships

  29. Transforming EE-R Diagrams into Relations 1. Map Regular Entities to Relations • E-R attributes map directly onto the relation (Fig. 4-8) • Composite attributes: Use only their simple, component attributes (Fig. 4-9). • Multi-valued Attribute : Becomes a separate relation with a foreign key taken from the superior entity (Fig. 4-10).

  30. Fig. 4-8: Mapping the regular entity CUSTOMER (a) CUSTOMER entity type (b) CUSTOMER relation [Same name on relation and entity type]

  31. Figure 4-9 Mapping a composite attribute (a) CUSTOMER entity type with composite attribute (b) CUSTOMER relation with address detail [Use only their simple, component attributes]

  32. EMPLOYEE EmployeeName EmployeeAddress EmployeeID EmployeeID Skill Fig. 4-10: Mapping an entity with a multivalued attribute (a) Employee entity type with multivalued attribute [Two relations created with one containing all of the attributes except the multi-valued attribute, and the second one contains the pk (on the first one) and the multi-valued attribute] Multivalued attribute becomes a separate relation with foreign key (b) Mapping a multivalued attribute One–to–many relationship between original entity and new relation

  33. Break ! (Ch. 4 - Part I) In class exercise #1-I (a) (p.193), apply Figure 2-8. HW # 1-I (c), apply Figure 2-11.a Fig. 3-8: Figure 2-8

  34. User interview & Integrated Model ER or EER or OO Relations Transformation (Seven Cases) Transformation to Relations ????? Normalization (3NF) IMPLEMENTATION Problem Solving for Modeling a Database Project Business Problem Study and Analyze w/Team

  35. Seven Cases of Transforming EE-R Diagrams into Relations 1. Map Regular Entities 2. Map Weak Entities 3. Map Binary Relationships 4. Map Associative Entities 5. Map Unary Relationships 6. Map Ternary (and n-ary) Relationships 7. Map Supertype/Subtype Relationships

  36. Transforming EER Diagrams into Relations 2. Mapping Weak Entities • Becomes a separate relation with a foreign key taken from the superior entity • Primary key composed of: • Partial identifier of weak entity • Primary key of identifying relation (strong entity) (Fig. 4-11)

  37. Fig. 4-11: Example of mapping a weak entity (a) Weak entity DEPENDENT Fig. 4-11: (b) Relations resulting from weak entity [Becomes a separate relation with a foreign key taken from the superior entity] PK NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity CPK FK Question: Why need all FOUR attributes to be a CPK?

  38. Transforming EE-R Diagrams Into Relations 3. Map Binary Relationships • One-to-Many - Primary key on the one side becomes a foreign key on the many side (Fig. 4-12). • Many-to-Many - Create a new relation with the primary keys of the two entities as its primary key (Fig. 4-13). • One-to-One - Primary key on the mandatory side becomes a foreign key on the optional side (Fig. 4-14).

  39. Fig. 4-12: Example of mapping a 1:M relationship (a) Relationship between customers and orders Note the mandatory one [Primary key on the one side becomes a foreign key on the many side] Fig. 4-12: (b) Mapping the relationship Again, no null value in the foreign key…this is because of the mandatory minimum cardinality Foreign key

  40. Figure 4-13 Example of mapping an M:N relationship a) Completes relationship (M:N) The Completes relationship will need to become a separate relation

  41. Composite primary key (cpk) Figure 4-13 Example of mapping an M:N relationship (cont.) b) Three resulting relations New intersection relation Foreign key Foreign key

  42. mandatory optional Figure 4-14 Example of mapping a binary 1:1 relationship a) In_charge relationship (1:1) Often in 1:1 relationships, one direction is optional.

  43. Fig. 4-14: (b) Resulting relations mandatory Same domain as Nurse_ID PK optional FK [Primary key on the mandatory side becomes a foreign key on the optional side]

  44. Transforming EE-R Diagrams Into Relations 4. Map Associative Entities • Identifier Not Assigned • Default primary key for the association relation is composed of the primary keys of the two entities (as in M:N relationship) (Fig. 4-15) • Identifier Assigned • It is natural and familiar to end-users. • Default identifier may not be unique. (Fig. 4-16).

  45. Figure 4-15 Example of mapping an associative entity B A a) An associative entity A B A

  46. Composite primary key formed from the two foreign keys Figure 4-15 Example of mapping an associative entity (cont.) b) Three resulting relations [Default primary key for the association relation is the primary keys of the two entities] PK cpk fk fk PK

  47. [Default primary key for the association relation is assigned] Figure 4-16: Mapping an associative entity (a) Associative entity (SHIPMENT) (b) Three resulting relations Primary key differs from foreign keys

  48. Transforming EE-R Diagrams Into Relations 5. Map Unary Relationships • One-to-Many • Recursive foreign key in the same relation (Fig. 4-17). • A recursive FK is a FK in a relation that references the PK values of that same relation. It must have the same domain as the PK. • Many-to-Many - Bill-of-materials: Two relations: • One for the entity type. • One for an associative relation in which the primary key has two attributes, both taken from the primary key of the entity. (Fig. 4-18).

  49. EMPLOYEE EmployeeID EmployeeName ManagerID EmployeeDateOfBirth Figure 4-17 Mapping a unary 1:N relationship A recursive FK is a FK in a relation that references the PK values of that same relation. It must have the same domain as the PK. (a) EMPLOYEE entity with unary relationship (b) EMPLOYEE relation with recursive foreign key

More Related