M.P. Johnson, DBMS, Stern/NYU, Spring 20051 C20.0046: Database Management Systems Lecture #5 Matthew P. Johnson Stern School of Business, NYU Spring, 2005.

Slides:



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

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Information Systems Chapter 4 Relational Databases Modelling.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 6 A First Course in Database Systems.
Functional Dependencies - Example
Lecture 6: Design Constraints and Functional Dependencies January 21st, 2004.
Design Principles: Faithfulness
Design Principles: Faithfulness
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.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #2 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Functional Dependencies Definition: If two tuples agree on the attributes A, A, … A 12n then they must also agree on the attributes B, B, … B 12m Formally:
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #3 Matthew P. Johnson Stern School of Business, NYU Spring, 2005.
1 Introduction to Database Systems CSE 444 Lectures 8 & 9 Database Design April 16 & 18, 2008.
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
A four-way Relationship
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #2 Matthew P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #2 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #3 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #3 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Lecture 06 The Relational Data Model. 2 Outline Relational Data Model Functional Dependencies FDs in ER Logical Schema Design Reading Chapter 8.
From E/R Diagrams to Relations. The Relational Data Model Database Model (E/R) Relational Schema Physical storage Diagrams (E/R) Tables: row names: attributes.
The Entity-Relationship Data Model
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #7 Matthew P. Johnson Stern School of Business, NYU Spring,
1 Lecture 3: Database Modeling (continued) April 5, 2002.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #4 M.P. Johnson Stern School of Business, NYU Spring, 2008.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #5 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #4 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #4 Matthew P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #6 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Lecture 9: Conceptual Database Design January 27 th, 2003.
Functional Dependencies and Relational Schema Design.
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.
1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 ORCL1 Oracle 8i: SQL & PL/SQL Session #2 Matthew P. Johnson CISDD, CUNY Fall, 2004.
Matthew P. Johnson, OCL5, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #1 Matthew P. Johnson CISDD, CUNY June, 2005.
CS411 Database Systems Kazuhiro Minami 02: The Entity-Relationship Model.
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.
ER Data Models Ctd. CSET 3300.
Conversion E/R to Relational CIS 4301 Lecture Notes Lecture 6 - 1/31/2006.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #2 Matthew P. Johnson CISDD, CUNY January, 2005.
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.
© D. Wong Ch. 2 Entity-Relationship Data Model (continue)  Data models  Entity-Relationship diagrams  Design Principles  Modeling of constraints.
Entity-Relationship Model
Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002.
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 2 Introduction to a First Course in Database Systems.
© D. Wong Ch. 3 (continued)  Database design problems  Functional Dependency  Keys of relations  Decompositions based on Functional Dependency.
1 Database Systems Lecture #4 Yan Pan School of Software, SYSU 2011.
CS 157B Database Systems Dr. T Y Lin. Updates 1.Red color denotes updated data (ppt) 2.Class participation will be part of “extra” credits to to “quiz.
Lecture 3: Conceptual Database Design and Schema Design April 12 th, 2004.
CPSC 603 Database Systems Lecturer: Laurie Webster II, M.S.S.E., M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 4 Introduction to a First Course in Database Systems.
The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
1 Lecture 08: E/R Diagrams and Functional Dependencies Friday, January 21, 2005.
Functional Dependencies Zaki Malik September 25, 2008.
CS411 Database Systems Kazuhiro Minami 04: Relational Schema Design.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #2 Matthew P. Johnson CISDD, CUNY June, 2005.
© D. Wong Ch. 3 (part 1)  Relational Model basics  From E/R diagram to Relations.
High-level Database Models Prof. Yin-Fu Huang CSIE, NYUST Chapter 4.
Modeling: Entity-Relationship Diagrams
ER Diagrams and Relational Model CS 174a (Winter 2015)
Lecture 5: Conceptual Database Design
Conceptual Database Design
Lecture 06 Data Modeling: E/R Diagrams
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #5 Matthew P. Johnson Stern School of Business, NYU Spring, 2005

M.P. Johnson, DBMS, Stern/NYU, Spring Converting weak ESs – differences Atts of Crew Rel are:  attributes of Crew  key attributes of supporting ESs CrewUnit-ofStudio StudioName Crew_ID address C2Miramax C1Disney C1Miramax Crew_IDStudioName Crew Supporting relships may be omitted (why?)

M.P. Johnson, DBMS, Stern/NYU, Spring Weak entity sets - relationships CrewStudio StudioName Crew_ID address Insurance IName Address th Av.NYBlueCross th Av.NYAetna AddressIName Insurance Subscribes Unit-of

M.P. Johnson, DBMS, Stern/NYU, Spring Weak entity sets - relationships Non-supporting relationships for weak ESs are converted  keys include entire weak ES key C21 C22 C21 Crew_ID Aetna BlueCross Aetna Insurer Universal Disney Universal StudioName Subscribes

M.P. Johnson, DBMS, Stern/NYU, Spring Conversion example Video store rental example, plus some atts Q: Conversion to relations? Rental VideoStore Customer Movie date year MName address Cname MID

M.P. Johnson, DBMS, Stern/NYU, Spring Conversion example, continued Resulting binary-relationship version Q: Conversion to relations? Rental Customer Store Movie StoreOf MovieOf BuyerOf date year MName address Cname MID

M.P. Johnson, DBMS, Stern/NYU, Spring Converting inheritance hierarchies No best way Several non-ideal methods:  E/R-style: each ES  relation  OO-style: each possible “object”  relation  nulls-style: each rooted hierarchy  relation non-applicable fields filled in with nulls Pros & cons  for each method, exist situations favoring it

M.P. Johnson, DBMS, Stern/NYU, Spring Converting inheritance hierarchies Movies Cartoons Murder- Mysteries isa Voices Weapon stars length titleyear Lion King Component

M.P. Johnson, DBMS, Stern/NYU, Spring Inheritance: E/R-style conversion Each ES  relation Root entity set: Movies(title, year, length) Lion King Year length Roger Rabbit Scream Star Wars Title Knife1990R. Rabbit 1988 Year Knife murderWeapon Scream Title Subclass: MurderMysteries(title, year, murderWeapon) Subclass: Cartoons(title, year) 1993Lion King 1990 Year Roger Rabbit Title

M.P. Johnson, DBMS, Stern/NYU, Spring Subclasses: object-oriented approach Every possible “subtree” (what’s this?): 1. Movies 2. Movies + Cartoons 3. Movies + Murder-Mysteries 4. Movies + Cartoons + Murder-Mysteries TitleYearlength Star Wars TitleYearlengthMurder-Weapon Scream Knife TitleYearlength Lion King TitleYearlengthMurder-Weapon Roger Rabbit Knife

M.P. Johnson, DBMS, Stern/NYU, Spring Subclasses: nulls approach One relation for entire hierarchy Any non-applicable fields are NULL Q: How do we know if a movie is a MM? Q: How do we know if a movie is a cartoon? TitleYearlengthMurder-Weapon Star Wars NULL Lion King NULL Scream Knife Roger Rabbit Knife

M.P. Johnson, DBMS, Stern/NYU, Spring Subclasses methods: considerations 1. Query time ~ number of tables accessed in query  nulls: best, since each entity  single row  multi-node questions: “Find 1999 films with length > 150 mins” E/R: just Movies, so fast OO: Movies AND cartoons, so slow  single-node questions: “Find weapons in >150-min. cartoons” E/R: Movies, Cartoons AND MMs, so slow OO: just MoviesCMM, so fast 2. Number of relations per entity set  nulls: just one, so very few  E/R: one per ES, so more  OO: exponential in #ESs, so very many (how many?) 3. Number/size of rows per entity  nulls: one “long” row per entity But all non non-applicables become null  OO: one (all-relevant) row per entity, so smallest  E/R: several (tho all relevant) rows per entity but keys are duplicated

M.P. Johnson, DBMS, Stern/NYU, Spring E/R-style & quasi-redundancy Name and year of Roger Rabbit were listed in three different rows (in different tables) Suppose title changes (“Roger”  “Roget”)   must change all three places Q: Is this redundancy? A: No!  name and year are independent  multiple movies may have same name Real redundancy reqs. dependency two rows agree on SSN  must agree on rest  conflicting hair colors in these rows is an error two rows agree on movie title  may still disagree  conflicting years may be correct – or may not be can forbid first case but not second Better: introduce “movie-id” key att

M.P. Johnson, DBMS, Stern/NYU, Spring Combined isa/weak example Exercise  Convert from E/R to R, by E/R, OO and nulls courses Lab- courses Depts #machines room cnumber givenBy dname chair isa platform cname

M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Last time: relational model This time: 1. Functional dependencies  Keys and superkeys in terms of FDs  Finding keys for relations 2. Rules for combining FDs Next time: anomalies & normalization

M.P. Johnson, DBMS, Stern/NYU, Spring Next topic: Functional dependencies FDs are constraints  part of the schema  can’t tell from particular relation instances  FD may hold for some instances “accidentally” Finding all FDs is part of DB design  Used in normalization

M.P. Johnson, DBMS, Stern/NYU, Spring Functional dependencies Definition: Notation: Read: A i functionally determines B j If two tuples agree on the attributes A 1, A 2, …, A n then they must also agree on the attributes B 1, B 2, …, B m A 1, A 2, …, A n  B 1, B 2, …, B m

M.P. Johnson, DBMS, Stern/NYU, Spring Typical Examples of FDs Product  name  price, manufacturer Person  ssn  name, age  father’s/husband’s-name  last-name  zipcode  state  phone  state (notwithstanding inter-state area codes) Company  name  stockprice, president  symbol  name  name  symbol

M.P. Johnson, DBMS, Stern/NYU, Spring To check A  B, erase all other columns; for each rows t 1, t 2 i.e., check if remaining relation is many-one  no “divergences”  i.e., if A  B is a well-defined function  thus, functional dependency Functional dependencies BmBm...B1B1 AmAm A1A1 t1t1 t2t2 if t 1, t 2 agree here then t 1, t 2 agree here

M.P. Johnson, DBMS, Stern/NYU, Spring FDs Example Product(name, category, color, department, price) name  color category  department color, category  price name  color category  department color, category  price Consider these FDs: What do they say ?

M.P. Johnson, DBMS, Stern/NYU, Spring FDs Example FDs are constraints: On some instances they hold On others they don’t namecategorycolordepartmentprice GizmoGadgetGreenToys49 TweakerGadgetGreenToys99 Does this instance satisfy all the FDs ? name  color category  department color, category  price name  color category  department color, category  price

M.P. Johnson, DBMS, Stern/NYU, Spring FDs Example namecategorycolordepartmentprice GizmoGadgetGreenToys49 TweakerGadgetBlackToys99 GizmoStationaryGreen Office- supp. 59 What about this one ? name  color category  department color, category  price name  color category  department color, category  price

M.P. Johnson, DBMS, Stern/NYU, Spring Q: Is Position  Phone an FD here? A: It is for this instance, but no, presumably not in general Others FDs? EmpID  Name, Phone, Position but Phone  Position Recognizing FDs EmpIDNamePhonePosition E0045Smith1234Clerk E1847John9876Salesrep E1111Smith9876Salesrep E9999Mary1234Lawyer

M.P. Johnson, DBMS, Stern/NYU, Spring Keys of relations {A 1 A 2 A 3 …A n } is a key for relation R if 1. A 1 A 2 A 3 …A n functionally determine all other attributes Usual notation: A 1 A 2 A 3 …A n  B 1 B 2 …B k rels = sets  distinct rows can’t agree on all A i 2. A 1 A 2 A 3 …A n is minimal No proper subset of A 1 A 2 A 3 …A n functionally determines all other attributes of R Primary key: chosen if there are several possible keys

M.P. Johnson, DBMS, Stern/NYU, Spring Keys example Relation: Student(Name, Address, DoB, , Credits) Which (/why) of the following are keys?  SSN  Name, Address (on reasonable assumptions)  Name, SSN  , SSN  NB: minimal != smallest

M.P. Johnson, DBMS, Stern/NYU, Spring Superkeys A set of attributes that contains a key Satisfies first condition:  functionally determines every other attribute in the relation Might not satisfy the second condition: minimality  may be possible to peel away some attributes from the superkey  keys are superkeys key are special case of superkey  superkey set is superset of key set name;ssn is a superkey but not a key

M.P. Johnson, DBMS, Stern/NYU, Spring Discovering keys for relations Relation  entity set  Key of relation = (minimized) key of entity set Relation  binary relationship  Many-many: union of keys of both entity sets  Many(M)-one(O): only key of M (Why?)  One-one: key of either entity set (but not both!)

M.P. Johnson, DBMS, Stern/NYU, Spring Example – entity sets Key of entity set = (minimized) key of relation Student(Name, Address, DoB, SSN, , Credits) Student Name Address DoB SSN Credits

M.P. Johnson, DBMS, Stern/NYU, Spring Example – many-many Many-many key: union of both ES keys StudentEnrolls Course SSNCredits CourseID Name Enrolls(SSN,CourseID)

M.P. Johnson, DBMS, Stern/NYU, Spring Example – many-one Key of the many ES but not of the one ES  keys from both would be non-minimal CourseMeetsIn Room CourseIDName RoomNo Capacity MeetsIn(CourseID,RoomNo)

M.P. Johnson, DBMS, Stern/NYU, Spring Example – one-one Keys of both ESs included in relation Key is key of either ES (but not both!) HusbandsMarriedWives SSNName SSN Name Married(HSSN, WSSN) or Married(HSSN, WSSN)

M.P. Johnson, DBMS, Stern/NYU, Spring Discovering keys: multiway Multiway relationships:  Multiple ways – may not be obvious  R:F,G,H  E is many-one  E’s key is included but not part of key Recall that relship atts are implicitly many-one CourseEnrolls Student CourseID Name SSN NameSection RoomNo Capacity Enrolls(CourseID,SSN,RoomNo)

M.P. Johnson, DBMS, Stern/NYU, Spring Example Exercise Relation relating particles in a box to locations and velocities InPosition(id,x,y,z,vx,vy,vz) Q: What FDs hold? Q: What are the keys?

M.P. Johnson, DBMS, Stern/NYU, Spring Next time Do reading on web Finish project part 1 Office hours now