Understanding E/R Model Considerations and Relationships

Slide Note
Embed
Share

Explore the E/R model considerations and relationships like multiplicity, multi-way, conversion to SQL, and more. Learn about modeling purchase relationships and the significance of arrows in multi-way relationships. Understand the challenges in expressing constraints like every person shopping at most one store and converting multi-way relationships to binary form.


Uploaded on Sep 14, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Lecture 9: The E/R Model II Lecture and activity contents are based on what Prof Chris R used in his CS 145 in the fall 2016 term with permission.

  2. 2. E/R Design Considerations 2

  3. What you will learn about in this section 1. Relationships cont d: multiplicity, multi-way 2. Design considerations 3. Conversion to SQL 4. ACTIVITY: Crayon Time! Drawing E/R diagrams Pt. II 3

  4. Multiplicity of E/R Relationships a b c d 1 2 3 One-to-one: Indicated using arrows a b c d 1 2 3 Many-to-one: X -> Y means there exists a there exists a function mapping function mapping from X to Y from X to Y (recall the definition of a function) a b c d 1 2 3 One-to-many: a b c d 1 2 3 Many-to-many: 4

  5. name category name price makes Company Product stockprice What does this say? buys employs Person name ssn address 5

  6. Multi-way Relationships How do we model a purchase relationship between buyers, products and stores? Product Purchase Store Person NB: Can still model as a mathematical set (how?) 6

  7. Arrows in Multiway Relationships Q: What does the arrow mean ? Product Store Purchase Person 7

  8. Arrows in Multiway Relationships Q: What does the arrow mean ? Product Store Purchase Person 8

  9. Arrows in Multiway Relationships Q: How do we say that every person shops in at most one store ? Product Store Purchase Person A: Cannot. This is the best approximation. (Why only approximation ?) 9

  10. Converting Multi-way Relationships to Binary From what we had on previous slide to this - what did we do? date Product ProductOf Purchase Store StoreOf BuyerOf Person 10

  11. Converting Multi-way Relationships to New Entity + Binary Relationships Side note: What arrows should be added here? Are these correct? ProductOf Product date Purchase StoreOf Store BuyerOf Person 11

  12. Decision: Multi-way or New Entity + Binary? Entity + Binary Multi-way Relationship ProductOf Product Product date Store Purchase Purchase StoreOf Store BuyerOf Person Person Should we use a single m multi ulti- -way relationship way relationship or a new entity with binary relations? binary relations? new entity with 12

  13. Decision: Multi-way or New Entity + Binary? (B) Entity + Binary (A) Multi-way Relationship ProductOf Product Product date Store Purchase Purchase StoreOf Store BuyerOf Person Person Multiple purchases per (product, store, person) combo possible here! Covered earlier: (B) is useful if we want to have multiple instances of the relationship per entity combination 13

  14. Decision: Multi-way or New Entity + Binary? (B) Entity + Binary (A) Multi-way Relationship ProductOf Product Product date Store Purchase Purchase StoreOf Store BuyerOf Person Person We can add more-fine- grained constraints here! (B) is also useful when we want to add details (constraints or attributes) to the relationship - A person who shops in only one store - How long a person has been shopping at a store 14

  15. Decision: Multi-way or New Entity + Binary? (B) Entity + Binary (A) Multi-way Relationship ProductOf Product Product date Store Purchase Purchase StoreOf Store BuyerOf Person Person (A) is useful when a relationship really is between multiple entities - Ex: A three-party legal contract 15

  16. 3. Design Principles What s wrong with these examples? Product Purchase Person President Country Person 16

  17. Design Principles: What s Wrong? date Product Purchase Store personAddr personName 17

  18. Design Principles: What s Wrong? date Dates Product Purchase Store Person 18

  19. Examples: Entity vs. Attribute Should address (A) be an attribute? Or (B) be an entity? Street Addr ZIP Addr 1 Addr 2 Address AddrOf Employee Employee 19

  20. Examples: Entity vs. Attribute Should address (A) be an attribute? How do we handle employees with multiple addresses here? Addr 1 Addr 2 How do we handle addresses where internal structure of the address (e.g. zip code, state) is useful? Employee 20

  21. Examples: Entity vs. Attribute Or (B) be an entity? Should address (A) be an attribute? Street Addr ZIP Addr 1 Address Addr 2 AddrOf Employee Employee In general, when we want to record several values, we choose new entity 21

  22. From E/R Diagrams to Relational Schema Key concept: Both Entity sets and Relationships become relations (tables in RDBMS) 22

  23. From E/R Diagrams to Relational Schema price category An entity set becomes a relation (multiset of tuples / table) name Product Each tuple is one entity Product Each tuple is composed of the entity s attributes, and has the same primary key name price category Gizmo1 99.99 Camera Gizmo2 19.99 Edible 23

  24. From E/R Diagrams to Relational Schema price category name Product CREATE TABLE Product( name CHAR(50) PRIMARY KEY, price DOUBLE, category VARCHAR(30) ) Product name price category Gizmo1 99.99 Camera Gizmo2 19.99 Edible 24

  25. From E/R Diagrams to Relational Schema date firstname lastname name A relation between entity sets A1, , ANalso becomes a multiset of tuples / a table category price Product Person Purchased Each row/tuple is one relation, i.e. one unique combination of entities (a1, ,aN) Purchased Each row/tuple is composed of the union of the entity sets keys has the entities primary keys as foreign keys has the union of the entity sets keys as primary key name firstname lastname date Gizmo1 Bob Joe 01/01/15 Gizmo2 Joe Bob 01/03/15 Gizmo1 JoeBob Smith 01/05/15 25

  26. From E/R Diagrams to Relational Schema date firstname lastname CREATE TABLE Purchased( name CHAR(50), firstname CHAR(50), lastname CHAR(50), date DATE, PRIMARY KEY (name, firstname, lastname), FOREIGN KEY (name) REFERENCES Product, FOREIGN KEY (firstname, lastname) REFERENCES Person ) name category price Product Purchased Person Purchased name firstname lastname date Gizmo1 Bob Joe 01/01/15 Gizmo2 Joe Bob 01/03/15 Gizmo1 JoeBob Smith 01/05/15 26

  27. From E/R Diagram to Relational Schema How do we represent this as a relational schema? date firstname lastname name category price Product Purchased Person name address Store 27

  28. ACTIVITY: E/R Diagrams Part II 28

  29. Add arrows to your E/R diagram! Also make sure to add (new concepts underlined): (new concepts underlined): A player can only belong to one team, a play can only be in one game, a pass/run..? Players can achieve a Personal Record Personal Record linked to a specific Game and Play Players have a weight weight which changes in on vs. off-season 29

Related


More Related Content