Lecture 4: The E/R Model Lecture 4 Today’s Lecture 1.E/R Basics: Entities & Relations ACTIVITY: Crayon time! 2.E/R Design considerations ACTIVITY: Crayon.

Slides:



Advertisements
Similar presentations
Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Advertisements

Information Systems Chapter 4 Relational Databases Modelling.
Lecture 6: Design Constraints and Functional Dependencies January 21st, 2004.
Text-Book Chapters (7 and 8) Entity-Relationship Model
1 Lecture 04 Entity/Relationship Modelling. 2 Outline E/R model (Chapter 5) From E/R diagrams to relational schemas (Chapter 5) Constraints in SQL (Chapter.
Lecture #2 October 5 th, 2000 Conceptual Modeling Administration: –HW1 available –Details on projects –Exam date –XML comment.
1 Lecture 3: Database Modeling (continued) April 5, 2002.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Entity-Relationship Model Database Management Systems I Alex Coman, Winter.
Lecture 9: Conceptual Database Design January 27 th, 2003.
Multiplicity in E/R Diagrams
1 Lecture 4: Database Modeling (end) The Relational Data Model April 8, 2002.
CS411 Database Systems Kazuhiro Minami
Database Design April 3, Projects, More Details Goal: build a DB application. (almost) anything goes. Groups of 3-4. End of week 2: groups formed.
Entity / Relationship Diagrams Objects entities Classes entity sets Attributes are like in ODL. Relationships: like in ODL except - not associated with.
CS411 Database Systems Kazuhiro Minami 02: The Entity-Relationship Model.
1 ER Modeling BUAD/American University Entity Relationship (ER) Modeling.
E/R Diagrams and Functional Dependencies. Modeling Subclasses The world is inherently hierarchical. Some entities are special cases of others We need.
Lecture 08: E/R Diagrams and Functional Dependencies.
Lecture 2: E/R Diagrams and the Relational Model Thursday, January 4, 2001.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Design Issues Mapping.
Conceptual Database Design. Building an Application with a DBMS Requirements modeling (conceptual, pictures) –Decide what entities should be part of the.
1 Introduction to Database Systems CSE 444 Lecture 07 E/R Diagrams October 10, 2007.
Tallahassee, Florida, 2015 COP4710 Database Systems E-R Model Fall 2015.
Entity Relationship Diagram (2)
Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002.
ITTelkom Entity Relationship Diagram (1) CS2343 Perancangan Basisdata Relasional.
UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar.
Lecture 3: Conceptual Database Design and Schema Design April 12 th, 2004.
COMP 430 Intro. to Database Systems Entity-Relationship Diagram Basics Slides use ideas from Chris Ré.
COMP 430 Intro. to Database Systems ER Design Considerations Slides use ideas from Chris Ré.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Mapping Constraints Keys.
ER Diagrams and Relational Model CS 174a (Winter 2015)
COP Introduction to Database Structures
Contents Design Process Modeling Constraints E-R Diagram Design Issues
Comp 1100 Entity-Relationship (ER) Model
Database Management Systems
Lecture 5: Conceptual Database Design
Modeling Constraints Extracting constraints is what modeling is all about. But how do we express them? Examples: Keys: social security number uniquely.
Relational Database Design by ER- and EER-to- Relational Mapping
Entity Relationship Model
COP4710 Database Systems E-R Model.
Database Design Oct. 3, 2001.
Database Design Why do we need it? Consider issues such as:
Entity-Relationship Model
Chapter 2: Entity-Relationship Model
Tables and Their Characteristics
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Lecture 4 Lecture 4: The E/R Model.
Chapter 7: Entity-Relationship Model
02: ER Model Ch 4.1 – 4.4 Optionally, begin with Ch 2.1.
Conceptual Database Design
Cse 344 May 14th – Entities.
Lecture 4: Database Modeling (continued)
Overview of Entity‐Relationship Model
Module 8 – Database Design Using the E-R Model
Cse 344 May 11th – Entities.
Lecture 06 Data Modeling: E/R Diagrams
name category name price makes Company Product stockprice buys employs
The Entity-Relationship Model
Functional Dependencies
CMPT 354: Database System I
Chapter 6: Entity-Relationship Model
Lecture 8: The E/R Model I
Lecture 9: The E/R Model II
Lecture 10: The E/R Model III
Lecture 06: SQL Monday, October 11, 2004.
CS4433 Database Systems E-R Model.
Lecture 08: E/R Diagrams and Functional Dependencies
Chapter 6b: Database Design Using the E-R Model
Presentation transcript:

Lecture 4: The E/R Model Lecture 4

Today’s Lecture 1.E/R Basics: Entities & Relations ACTIVITY: Crayon time! 2.E/R Design considerations ACTIVITY: Crayon time pt. II 3.Advanced E/R Concepts ACTIVITY: E/R Translation 2 Lecture 4

1. E/R Basics: Entities & Relations 3 Lecture 4 > Section 1

What you will learn about in this section 1.High-level motivation for the E/R model 2.Entities 3.Relations 4.ACTIVITY: Crayon Time! Drawing E/R diagrams 4 Lecture 4 > Section 1

5 Database Design Database design: Why do we need it? Agree on structure of the database before deciding on a particular implementation Consider issues such as: What entities to model How entities are related What constraints exist in the domain How to achieve good designs Several formalisms exist We discuss one flavor of E/R diagrams This the first project Lecture 4 > Section 1 > Motivation

Database Design Process 1.Requirements analysis What is going to be stored? How is it going to be used? What are we going to do with the data? Who should access the data? 6 Technical and non- technical people are involved Lecture 4 > Section 1 > Motivation 1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

2. Conceptual Design A high-level description of the database Sufficiently precise that technical people can understand it But, not so precise that non-technical people can’t participate 7 This is where E/R fits in. Lecture 4 > Section 1 > Motivation Database Design Process 1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

8 Lecture 4 > Section 1 > Motivation Database Design Process 1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc. 3. More: Logical Database Design Physical Database Design Security Design

9 This process is iterated many times E/R is a visual syntax for DB design which is precise enough for technical points, but abstracted enough for non-technical people Lecture 4 > Section 1 > Motivation Database Design Process 1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc. Makes Product name category price Company name E/R Model & Diagrams used

Interlude: Impact of the ER model The E/R model is one of the most cited articles in Computer Science “The Entity-Relationship model – toward a unified view of data” Peter Chen, 1976 Used by companies big and small You’ll know it soon enough 10 Lecture 4 > Section 1 > Motivation

Entities and Entity Sets Entities & entity sets are the primitive unit of the E/R model Entities are the individual objects, which are members of entity sets Ex: A specific person or product Entity sets are the classes or types of objects in our model Ex: Person, Product These are what is shown in E/R diagrams - as rectangles Entity sets represent the sets of all possible entities 11 Product Lecture 4 > Section 1 > Entities Person These represent entity sets

Entities and Entity Sets An entity set has attributes Represented by ovals attached to an entity set 12 Product name category price Shapes are important. Colors are not. Lecture 4 > Section 1 > Entities

Entities vs. Entity Sets Example: 13 Product name category price Lecture 4 > Section 1 > Entities Entity Set Product Name: Xbox Category: Total Multimedia System Price: $250 Name: My Little Pony Doll Category: Toy Price: $25 Entity Entity Attribute Entities are not explicitly represented in E/R diagrams!

Keys A key is a minimal set of attributes that uniquely identifies an entity. 14 Product name category price Denote elements of the primary key by underlining. Here, {name, category} is not a key (it is not minimal). If it were, what would it mean? Lecture 4 > Section 1 > Entities The E/R model forces us to designate a single primary key, though there may be multiple candidate keys

The R in E/R: Relationships A relationship is between two entities 15 Product name category price Company nameMakes Lecture 4 > Section 1 > Relationships

16 makes buys employs Product namecategory price Person address namessn Company stockprice name Lecture 4 > Section 1 > Relationships

17 What is a Relationship? A mathematical definition: Let A, B be sets A={1,2,3}, B={a,b,c,d} Lecture 4 > Section 1 > Relationships a b c d A= B=

18 What is a Relationship? A mathematical definition: Let A, B be sets A={1,2,3}, B={a,b,c,d} A x B (the cross-product) is the set of all pairs (a,b) A  B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), (3,a), (3,b), (3,c), (3,d)} a b c d A= B= Lecture 4 > Section 1 > Relationships

19 What is a Relationship? A mathematical definition: Let A, B be sets A={1,2,3}, B={a,b,c,d}, A x B (the cross-product) is the set of all pairs (a,b) A  B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), (3,a), (3,b), (3,c), (3,d)} We define a relationship to be a subset of A x B R = {(1,a), (2,c), (2,d), (3,b)} a b c d A= B= Lecture 4 > Section 1 > Relationships

20 What is a Relationship? A mathematical definition: Let A, B be sets A x B (the cross-product) is the set of all pairs A relationship is a subset of A x B Makes is relationship- it is a subset of Product  Company: a b c d A= B= makes Company Product Lecture 4 > Section 1 > Relationships

What is a Relationship? 21 Makes Product name category price Company name A relationship between entity sets P and C is a subset of all possible pairs of entities in P and C, with tuples uniquely identified by P and C’s keys Lecture 4 > Section 1 > Relationships

What is a Relationship? 22 Lecture 4 > Section 1 > Relationships namecategoryprice GizmoElectronics$9.99 GizmoLiteElectronics$7.50 GadgetToys$5.50 name GizmoWorks GadgetCorp Product Company Makes Product name category price Company name A relationship between entity sets P and C is a subset of all possible pairs of entities in P and C, with tuples uniquely identified by P and C’s keys

What is a Relationship? 23 Lecture 4 > Section 1 > Relationships namecategoryprice GizmoElectronics$9.99 GizmoLiteElectronics$7.50 GadgetToys$5.50 name GizmoWorks GadgetCorp Product Company C.nameP.nameP.categoryP.price GizmoWorksGizmoElectronics$9.99 GizmoWorksGizmoLiteElectronics$7.50 GizmoWorksGadgetToys$5.50 GadgetCorpGizmoElectronics$9.99 GadgetCorpGizmoLiteElectronics$7.50 GadgetCorpGadgetToys$5.50 Makes Product name category price Company name A relationship between entity sets P and C is a subset of all possible pairs of entities in P and C, with tuples uniquely identified by P and C’s keys

What is a Relationship? 24 Lecture 4 > Section 1 > Relationships namecategoryprice GizmoElectronics$9.99 GizmoLiteElectronics$7.50 GadgetToys$5.50 name GizmoWorks GadgetCorp Product Company C.nameP.nameP.categoryP.price GizmoWorksGizmoElectronics$9.99 GizmoWorksGizmoLiteElectronics$7.50 GizmoWorksGadgetToys$5.50 GadgetCorpGizmoElectronics$9.99 GadgetCorpGizmoLiteElectronics$7.50 GadgetCorpGadgetToys$5.50 C.nameP.name GizmoWorksGizmo GizmoWorksGizmoLite GadgetCorpGadget Makes Product name category price Company name A relationship between entity sets P and C is a subset of all possible pairs of entities in P and C, with tuples uniquely identified by P and C’s keys

What is a Relationship? There can only be one relationship for every unique combination of entities This also means that the relationship is uniquely determined by the keys of its entities Example: the “key” for Makes (to right) is {Product.name, Company.name} 25 Lecture 4 > Section 1 > Relationships Why does this make sense? This follows from our mathematical definition of a relationship- it’s a SET! Makes Product name category price Company name since

26 Product name category price Company nameMakessince Relationships may have attributes as well. For example: “since” records when company started making a product Note: “since” is implicitly unique per pair here! Why? Note #2: Why not “how long”? Lecture 4 > Section 1 > Relationships Relationships and Attributes

Decision: Relationship vs. Entity? Q: What does this say? A: A person can only buy a specific product once (on one date) 27 Lecture 4 > Section 1 > Relationships Purchased Product name category price Person name date Modeling something as a relationship makes it unique; what if not appropriate?

Decision: Relationship vs. Entity? What about this way? Now we can have multiple purchases per product, person pair! 28 Product name category price Person name date Purchase quantityPID# ProductOfBuyerOf We can always use a new entity instead of a relationship. For example, to permit multiple instances of each entity combination! Lecture 4 > Section 1 > Relationships

ACTIVITY: E/R Diagrams Pt. I 29 Lecture 4 > Section 1 > ACTIVITY

Draw an E/R diagram for football 30 Lecture 4 > Section 1 > ACTIVITY Teams play each other in Games. Each pair of teams can play each other multiple times Players belong to Teams (assume no trades / changes). A Play will contain either a Pass from one player to another, or a Run by one player Use the following simplified model of a football season (concepts to include are underlined): A Game is made up of Plays that result in a yardage gain/loss, and potentially a touchdown

2. E/R Design Considerations 31 Lecture 4 > Section 2

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 32 Lecture 4 > Section 2

33 Multiplicity of E/R Relationships Indicated using arrows Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way abcdabcd One-to-one: abcdabcd Many-to-one: abcdabcd One-to-many: abcdabcd Many-to-many: X -> Y means there exists a function mapping from X to Y (recall the definition of a function)

34 address namessn Person buys makes employs Company Product namecategory stockprice name price What does this say? Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way

35 Multi-way Relationships How do we model a purchase relationship between buyers, products and stores? Purchase Product Person Store NB: Can still model as a mathematical set (how?) Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way

36 Q: What does the arrow mean ? Arrows in Multiway Relationships Purchase Product Person Store Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way

37 Q: What does the arrow mean ? Arrows in Multiway Relationships Purchase Product Person Store Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way

38 Q: How do we say that every person shops in at most one store ? Arrows in Multiway Relationships A: Cannot. This is the best approximation. (Why only approximation ?) Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way Product Person Store Purchase

39 Converting Multi-way Relationships to Binary Purchase Person Store Product StoreOf ProductOf BuyerOf date Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way From what we had on previous slide to this - what did we do?

40 Converting Multi-way Relationships to New Entity + Binary Relationships Purchase Person Store Product StoreOf ProductOf BuyerOf date Side note: What arrows should be added here? Are these correct? Lecture 4 > Section 2 > Relationships- Multiplicity & Multi-way

Decision: Multi-way or New Entity + Binary? 41 Should we use a single multi-way relationship or a new entity with binary relations? Purchase Person Store Product StoreOf ProductOf BuyerOf date Purchase Product Person Store Lecture 4 > Section 2 > Design decisions Multi-way Relationship Entity + Binary

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

Decision: Multi-way or New Entity + Binary? 43 Lecture 4 > Section 2 > Design decisions (A) Multi-way Relationship (B) Entity + Binary (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” We can add more-fine- grained constraints here! Purchase Person Store Product StoreOf ProductOf BuyerOf date Purchase Product Person Store

Decision: Multi-way or New Entity + Binary? 44 Lecture 4 > Section 2 > Design decisions (A) Multi-way Relationship (B) Entity + Binary (A) is useful when a relationship really is between multiple entities - Ex: A three-party legal contract Purchase Person Store Product StoreOf ProductOf BuyerOf date Purchase Product Person Store

45 3. Design Principles Purchase ProductPerson What’s wrong with these examples? President PersonCountry Lecture 4 > Section 2 > Design decisions

46 Design Principles: What’s Wrong? Purchase Product Store date personName personAddr Lecture 4 > Section 2 > Design decisions

47 Design Principles: What’s Wrong? Purchase Product Person Store date Dates Lecture 4 > Section 2 > Design decisions

Examples: Entity vs. Attribute 48 Should address (A) be an attribute? Employee Addr 1 Addr 2 Address Street Addr ZIP Employee AddrOf Lecture 4 > Section 2 > Design decisions Or (B) be an entity?

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

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

From E/R Diagrams to Relational Schema Key concept: Both Entity sets and Relationships become relations (tables in RDBMS) 51 Lecture 4 > Section 2 > Conversion to SQL

From E/R Diagrams to Relational Schema namepricecategory Gizmo199.99Camera Gizmo219.99Edible 52 Product price category name Product An entity set becomes a relation (multiset of tuples / table) –Each tuple is one entity –Each tuple is composed of the entity’s attributes, and has the same primary key Lecture 4 > Section 2 > Conversion to SQL

From E/R Diagrams to Relational Schema namepricecategory Gizmo199.99Camera Gizmo219.99Edible 53 Product price category name Product Lecture 4 > Section 2 > Conversion to SQL CREATE TABLE Product( name CHAR(50) PRIMARY KEY, price DOUBLE, category VARCHAR(30) ) CREATE TABLE Product( name CHAR(50) PRIMARY KEY, price DOUBLE, category VARCHAR(30) )

From E/R Diagrams to Relational Schema namefirstnamelastnamedate Gizmo1BobJoe01/01/15 Gizmo2JoeBob01/03/15 Gizmo1JoeBobSmith01/05/15 54 Purchased A relation between entity sets A 1, …, A N also becomes a multiset of tuples / a table –Each row/tuple is one relation, i.e. one unique combination of entities (a 1,…,a N ) –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 Purchased Product name category price Person firstname date lastname Lecture 4 > Section 2 > Conversion to SQL

From E/R Diagrams to Relational Schema namefirstnamelastnamedate Gizmo1BobJoe01/01/15 Gizmo2JoeBob01/03/15 Gizmo1JoeBobSmith01/05/15 55 Purchased Product name category price Person firstname date lastname Lecture 4 > Section 2 > Conversion to SQL 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 ) 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 )

From E/R Diagram to Relational Schema 56 Purchased Product name category price Person firstname date lastname Store name address How do we represent this as a relational schema? Lecture 4 > Section 2 > Conversion to SQL

ACTIVITY: E/R Diagrams Pt. II 57 Lecture 4 > Section 2 > ACTIVITY

Add arrows to your E/R diagram! 58 Lecture 4 > Section 2 > ACTIVITY 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 linked to a specific Game and Play Also make sure to add (new concepts underlined): Players have a weight which changes in on vs. off-season

[If time]: write a CREATE TABLE statement for Teams and Games 59 Lecture 4 > Section 2 > ACTIVITY Fun for you! Create StrengthsOfMine(Player,Strength) Table. (Find Von Miller’s strength by watching this informational video )video

[If time]: Make sure you’ve represented the season accurately! 60 Lecture 4 > Section 2 > ACTIVITY

[If time]: Can you write queries to: 61 Lecture 4 > Section 2 > ACTIVITY Calculate W/L percentage? Calculate average game outcome? Calculate HIGHEST and LOWEST ranked teams? Calculate the WORST team in the 2014 NFL season if bye weeks did not exist? New! Calculate only team with suspended QB for first four games.

3. Advanced E/R Concepts 62 Lecture 4 > Section 3

What you will learn about in this section 1.Subclasses & connection to OO 2.Constraints 3.Weak entity sets 4.ACTIVITY: Crayon Time! Drawing E/R diagrams Pt. III 63 Lecture 4 > Section 3

64 Modeling Subclasses Some objects in a class may be special, i.e. worthy of their own class Define a new class? But what if we want to maintain connection to current class? Better: define a subclass Ex: Products Software products Educational products Lecture 4 > Section 3 > Subclasses & OO We can define subclasses in E/R!

65 Product name price Software Product platforms Educational Product ageGroup isA Lecture 4 > Section 3 > Subclasses & OO Modeling Subclasses Child subclasses contain all the attributes of all of their parent classes plus the new attributes shown attached to them in the E/R diagram

66 Understanding Subclasses Think in terms of records; ex: Product SoftwareProduct EducationalProduct name price name price platforms name price ageGroup Lecture 4 > Section 3 > Subclasses & OO Child subclasses contain all the attributes of all of their parent classes plus the new attributes shown attached to them in the E/R diagram Product name price isA Educational ProductSoftware Product ageGroupplatforms

67 Think like tables… Product name price isA Educational ProductSoftware Product ageGroupplatforms namepricecategory Gizmo99gadget Camera49photo Toy39gadget nameplatforms Gizmounix nameageGroup Gizmotodler Toyretired Product Sw.Product Ed.Product Lecture 4 > Section 3 > Subclasses & OO

68 OO: Classes are disjoint (same for Java, C++) Difference between OO and E/R inheritance p1 p2 p3 sp1 sp2 ep1 ep2 ep3 Product SoftwareProduct EducationalProduct Lecture 4 > Section 3 > Subclasses & OO OO = Object Oriented. E.g. classes as fundamental building block, etc…

69 E/R: entity sets overlap Difference between OO and E/R inheritance SoftwareProduct EducationalProduct p1 p2 p3 sp1 sp2 ep1 ep2 ep3 Product Lecture 4 > Section 3 > Subclasses & OO

70 No need for multiple inheritance in E/R SoftwareProduct EducationalProduct p1 p2 p3 sp1 sp2 ep1 ep2 ep3 Product esp1 esp2 We have three entity sets, but four different kinds of objects Difference between OO and E/R inheritance Lecture 4 > Section 3 > Subclasses & OO

IsA Review If we declare A IsA B then every A is a B We use IsA to Add descriptive attributes to a subclass To identify entities that participate in a relationship No need for multiple inheritance 71 Lecture 4 > Section 3 > Subclasses & OO

72 Modeling UnionTypes With Subclasses Suppose each piece of furniture is owned either by a person, or by a company. How do we represent this? Lecture 4 > Section 3 > Subclasses & OO FurniturePiecePerson Company

73 Modeling Union Types with Subclasses Say: each piece of furniture is owned either by a person, or by a company Solution 1. Acceptable, but imperfect (What’s wrong ?) FurniturePiecePerson Company ownedByPersonownedByComp Lecture 4 > Section 3 > Subclasses & OO

74 Modeling Union Types with Subclasses Solution 2: better (though more laborious) FurniturePiece Person Company ownedBy Owner isa Lecture 4 > Section 3 > Subclasses & OO What is happening here?

75 Constraints in E/R Diagrams Finding constraints is part of the E/R modeling process. Commonly used constraints are: Keys: Implicit constraints on uniqueness of entities Ex: An SSN uniquely identifies a person Single-value constraints: Ex: a person can have only one father Referential integrity constraints: Referenced entities must exist Ex: if you work for a company, it must exist in the database Other constraints: Ex: peoples’ ages are between 0 and 150 Lecture 4 > Section 3 > Constraints Recall FOREIG N KEYs!

Participation Constraints: Partial v. Total 76 makes ProductCompany Are there products made by no company? Companies that don’t make a product? makes ProductCompany Bold line indicates total participation (i.e. here: all products are made by a company) Lecture 4 > Section 3 > Constraints

77 Keys in E/R Diagrams address namessn Person Product namecategory price Underline keys: Lecture 4 > Section 3 > Constraints Note: no formal way to specify multiple keys in E/R diagrams…

78 Single Value Constraints makes v. s. Lecture 4 > Section 3 > Constraints See previous section!

79 Referential Integrity Constraints CompanyProduct makes CompanyProduct makes Each product made by at most one company. Some products made by no company? Each product made by exactly one company. Lecture 4 > Section 3 > Constraints

80 Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. Entity sets are weak when their key comes from other classes to which they are related. “Football team” v. “The Stanford Football team” (E.g., Berkeley has a football team too, sort of) name University Team affiliation numbersport Lecture 4 > Section 3 > Weak Entity Sets

81 Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. Entity sets are weak when their key comes from other classes to which they are related. University Team affiliation numbersportname Lecture 4 > Section 3 > Weak Entity Sets number is a partial key. (denote with dashed underline). University is called the identifying owner. Participation in affiliation must be total. Why?

E/R Summary 82 E/R diagrams are a visual syntax that allows technical and non-technical people to talk For conceptual design Basic constructs: entity, relationship, and attributes A good design is faithful to the constraints of the application, but not overzealous Lecture 4 > Summary