Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 08: E/R Diagrams and Functional Dependencies.

Similar presentations


Presentation on theme: "Lecture 08: E/R Diagrams and Functional Dependencies."— Presentation transcript:

1 Lecture 08: E/R Diagrams and Functional Dependencies

2 Modeling Subclasses The world is inherently hierarchical. Some entities are special cases of others We need a notion of subclass. This is supported naturally in object-oriented formalisms. Products Software products Educational products

3 Subclasses in E/R Diagrams Age Groupplatforms name category price Educational ProductSoftware Product Product isa

4 Understanding Subclasses Think in terms of records: –Product –SoftwareProduct –EducationalProduct Field1Field2Field4Field5 Field1Field2Field3 Field1Field2

5 Subclasses to Relations NamePriceCategory Gizmo99gadget Camera49photo Toy39gadget Nameplatforms GizmoUnix NameAge Group Gizmotoddler Toyretired Product Sw.Product Ed.Product Age Groupplatforms name category price Educational ProductSoftware Product Product isa

6 Modeling Union Types with Subclasses FurniturePiece Person Company Say: each piece of furniture is owned either by a person, or by a company

7 Modeling Union Types with Subclasses Say: each piece of furniture is owned either by a person, or by a company Solution 1. Acceptable, imperfect (What’s wrong ?) FurniturePiecePerson Company ownedByPersonownedByCompany

8 Modeling Union Types with Subclasses Solution 2: better! isa FurniturePiece Person Company ownedBy Owner isa Will this always work? (hint: key?)

9 Constraints in E/R Diagrams Finding constraints is part of the modeling process. Commonly used constraints: Keys: social security number uniquely identifies a person. Single-value constraints: a person can have only one father. Referential integrity constraints: if you work for a company, it must exist in the database. Other constraints: every team has at most 10 members.

10 Keys in E/R Diagrams addressnamessn Person No formal way of specifying multiple keys in E/R diagrams Underline: name category price Product

11 Single Value Constraints vs. makes

12 Referential Integrity Constraints CompanyProduct makes CompanyProduct makes

13 Other Constraints CompanyProduct makes <100 What does this mean ?

14 Weak Entity Sets Entity sets are weak when their key comes from other classes to which they are related. University Team affiliation numbersportname

15 Handling Weak Entity Sets Convert to a relational schema (in class) University Team affiliation numbersportname

16 The Relational Data Model

17 Data Modeling Data Modeling Relational Schema Relational Schema Physical storage Physical storage E/R diagramsTables: column names: attributes rows: tuples Complex file organization and index structures.

18 Recalling The Terminology Name Price Category Manufacturer gizmo $19.99 gadgets GizmoWorks Power gizmo $29.99 gadgets GizmoWorks SingleTouch $149.99 photography Canon MultiTouch $203.99 household Hitachi Tuples or rows or records Attribute names or field names Table name or relation name Products:

19 First Normal Form (1NF) A database schema is in First Normal Form if all tables are flat NameGPACourses Alice3.8 Bob3.7 Carol3.9 Math DB OS DB OS Math OS Student NameGPA Alice3.8 Bob3.7 Carol3.9 Student Course Math DB OS StudentCourse AliceMath CarolMath AliceDB BobDB AliceOS CarolOS Takes Course

20 Functional Dependencies A form of constraint –hence, a part of the schema Finding them is part of the database design Also used in normalizing the relations

21 Functional Dependencies Definition: If two tuples agree on the attributes then they must also agree on the attributes B 1, B 2, … B m Formally: A 1, A 2, … A n A 1, A 2, … A n →B 1, B 2, … B m

22 Examples EmpID → Name, Phone, Position Position → Phone but Phone → Position EmpIDNamePhonePosition E0045John Smith2376HR E1847Zheng Li2376QA E3123Lucy Larsen1264Developer E9923Zheng Li1264Developer

23 In General To check if A → B holds in R, erase all other columns check if the remaining relation is many-one (called functional in mathematics) Note: A functional dependency is a constraint defined regardless of a particular DB instance AB X1Y1 X2Y2 ……

24 Example EmpIDNamePhonePosition E0045John Smith2376HR E1847Zheng Li2376QA E3123Lucy Larsen1264Developer E9923Zheng Li1264Developer

25 Typical Examples of FDs Product:manufacturer, mpn→ price, name Person:ssn → name, age Company:name → stockprice, president

26 Formal definition of a key A key is a set of attributes A 1,..., A n s.t. for any other attribute B, A 1,..., A n → B A minimal key is a set of attributes which is a key and for which no subset is a key Note: book calls them superkey and key

27 Examples of Keys Product(name, price, category, color) name, category → price category → color Keys are: {name, category} and all supersets Enrollment(student, address, course, room, time) student → address room, time → course student, course → room, time Keys are: [in class]

28 Finding the Keys of a Relation Given a relation constructed from an E/R diagram, what is its key? Rules: 1. If the relation comes from an entity set, the key of the relation is the set of attributes which is the key of the entity set. addressnamessn Person Person(address, name, ssn)

29 Finding the Keys buys(name, ssn, date) Rules: 2. If the relation comes from a many-many relationship, the key of the relation is the set of all attribute keys in the relations corresponding to the entity sets Person buys Product name price namessn date

30 Finding the Keys Except: if there is an arrow from the relationship to E, then we don’t need the key of E as part of the relation key. Purchase(name, sname, ssn, card-no) Purchase Product Person Store Payment Method name card-no ssn sname

31 Finding the Keys More rules: Many-one, one-many, one-one relationships Multi-way relationships Weak entity sets (Try to find them yourself, or check book)


Download ppt "Lecture 08: E/R Diagrams and Functional Dependencies."

Similar presentations


Ads by Google