![Page 1: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/1.jpg)
Lecture 08: E/R Diagrams and Functional Dependencies
![Page 2: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/2.jpg)
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
![Page 3: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/3.jpg)
Subclasses in E/R Diagrams
Age Groupplatforms
name category
price
Educational ProductSoftware Product
Product
isa isa
![Page 4: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/4.jpg)
Understanding Subclasses
• Think in terms of records:– Product
– SoftwareProduct
– EducationalProduct
Field1 Field2 Field4 Field5
Field1 Field2 Field3
Field1 Field2
![Page 5: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/5.jpg)
Subclasses to Relations
Name Price Category
Gizmo 99 gadget
Camera 49 photo
Toy 39 gadget
Name platforms
Gizmo Unix
Name Age Group
Gizmo toddler
Toy retired
Product
Sw.Product
Ed.Product
Age Groupplatforms
name category
price
Educational ProductSoftware Product
Product
isa isa
![Page 6: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/6.jpg)
Modeling Union Types with Subclasses
FurniturePiece
Person Company
Say: each piece of furniture is owned either by a person, or by a company
![Page 7: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/7.jpg)
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
ownedByPerson ownedByCompany
![Page 8: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/8.jpg)
Modeling Union Types with Subclasses
Solution 2: better!
isa
FurniturePiece
Person CompanyownedBy
Owner
isa
Will this always work? (hint: key?)
![Page 9: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/9.jpg)
Constraints in E/R DiagramsFinding 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: peoples’ ages are between 0 and 150.
![Page 10: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/10.jpg)
Keys in E/R Diagrams
address name ssn
Person
No formal way of specifying multiplekeys in E/R diagrams
Underline:name category
price
Product
![Page 11: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/11.jpg)
Single Value Constraints
vs.
makes
makes
![Page 12: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/12.jpg)
Referential Integrity Constraints
CompanyProduct makes
CompanyProduct makes
![Page 13: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/13.jpg)
Other Constraints
CompanyProduct makes<100
What does this mean ?
![Page 14: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/14.jpg)
Weak Entity SetsEntity sets are weak when their key comes from otherclasses to which they are related.
UniversityTeam affiliation
numbersport name
![Page 15: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/15.jpg)
Handling Weak Entity Sets
Convert to a relational schema (in class)
UniversityTeam affiliation
numbersport name
![Page 16: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/16.jpg)
The Relational Data Model
![Page 17: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/17.jpg)
The Relational Data Model
DataModeling
Relational Schema
Physicalstorage
E/R diagrams Tables: column names: attributes rows: tuples
Complexfile organizationand index structures.
![Page 18: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/18.jpg)
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 namesTable name or relation name
Products:
![Page 19: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/19.jpg)
First Normal Form (1NF)• A database schema is in First Normal Form
if all tables are flat
Name GPA Courses
Alice 3.8
Bob 3.7
Carol 3.9
Math
DB
OS
DB
OS
Math
OS
Student Name GPA
Alice 3.8
Bob 3.7
Carol 3.9
Student
Course
Math
DB
OS
Student Course
Alice Math
Carol Math
Alice DB
Bob DB
Alice OS
Carol OS
Takes Course
![Page 20: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/20.jpg)
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
![Page 21: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/21.jpg)
Functional DependenciesDefinition: If two tuples agree on the attributes
then they must also agree on the attributesB1, B2, … Bm
Formally:
A1 , A2 , … An
A1 , A2 , … An→B1, B2, … Bm
![Page 22: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/22.jpg)
Examples
• EmpID → Name, Phone, Position• Position → Phone• but Phone → Position
EmpID Name Phone PositionE0045 John Smith 2376 HRE1847 Zheng Li 2376 QAE3123 Lucy Larsen 1264 DeveloperE9923 Zheng Li 1264 Developer
![Page 23: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/23.jpg)
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
A BX1 Y1X2 Y2… …
![Page 24: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/24.jpg)
Example
EmpID Name Phone PositionE0045 John Smith 2376 HRE1847 Zheng Li 2376 QAE3123 Lucy Larsen 1264 DeveloperE9923 Zheng Li 1264 Developer
![Page 25: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/25.jpg)
Typical Examples of FDs
Product: manufacturer, mpn→ price, name
Person: ssn → name, age
Company: name → stockprice, president
![Page 26: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/26.jpg)
Formal definition of a key
• A key is a set of attributes A1, ..., An s.t. for any other attribute B, A1, ..., An → 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
![Page 27: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/27.jpg)
Examples of Keys• Product(name, price, category, color)
name, category → pricecategory → color
Keys are: {name, category} and all supersets
• Enrollment(student, address, course, room, time)student → addressroom, time → coursestudent, course → room, time
Keys are: [in class]
![Page 28: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/28.jpg)
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.
address name ssn
Person Person(address, name, ssn)
![Page 29: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/29.jpg)
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
PersonbuysProduct
name
pricename ssndate
![Page 30: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/30.jpg)
Finding the KeysExcept: 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
![Page 31: Lecture 08: E/R Diagrams and Functional Dependencies](https://reader036.vdocument.in/reader036/viewer/2022070501/56816957550346895de10553/html5/thumbnails/31.jpg)
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)