Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 430 Intro. to Database Systems ER Implementation as Tables Slides use ideas from Chris Ré.

Similar presentations


Presentation on theme: "COMP 430 Intro. to Database Systems ER Implementation as Tables Slides use ideas from Chris Ré."— Presentation transcript:

1 COMP 430 Intro. to Database Systems ER Implementation as Tables Slides use ideas from Chris Ré.

2 Book Writes Publishes GoesOnLoan Borrower Author ID address A Solution to Library Activity Publisher Loan Borrows name ISBN title num_copies address name card_num date Chapter PartOf title ID Writing  is_self_pub

3 ER implementation overview Author IDname Author IDname …… product_idstate_iddealer_id ……… Concession Except, sometimes optimize away this table: Common cases of one-to-many One-to-one Concession Dealer Product State Primary keys = keys of entity sets on a “many” side of relation.

4 Arity & partiticipation: Common cases

5 Many-to-many Product IDname …… Person IDname …… CREATE TABLE Product ( … PRIMARY KEY (ID) ); CREATE TABLE Person ( … PRIMARY KEY (ID) ); Purchase ProductPerson name ID name ID Purchase prod_IDperson_ID …… CREATE TABLE Purchase ( … PRIMARY KEY (prod_ID, person_ID), FOREIGN KEY (prod_ID) REFERENCES Product (ID), FOREIGN KEY (person_ID) REFERENCES Person (ID) );

6 Many-to-many Purchase ProductPerson name ID name ID Purchase prod_IDperson_ID …… CREATE TABLE Purchase ( … PRIMARY KEY (prod_ID, person_ID), FOREIGN KEY (prod_ID) REFERENCES Product (ID), FOREIGN KEY (person_ID) REFERENCES Person (ID) ); Primary key fields also NOT NULL. Doesn’t that force total participation? No. E.g., a Product never purchased simply never appears in Purchase table.

7 N-ary many-many Purchase Product Person Store Junction table’s primary key = combination of primary keys of all n tables.

8 One-to-many / many-to-one Book Publishes IDaddress Publisher name title num_copies is_self_pub ISBN Publisher IDnameaddress ……… Book ISBNtitlenum_copiesis_self_pubpub_IDpub_date …………… CREATE TABLE Publisher ( … PRIMARY KEY (ID) ); CREATE TABLE Book ( … PRIMARY KEY (ISBN), FOREIGN KEY (pub_ID) REFERENCES Publisher (ID) ); date Publishes table optimized away.

9 One-to-many / many-to-one Book Publishes IDaddress Publisher name title num_copies is_self_pub ISBN Publisher IDnameaddress ……… Way to remember where the additional field is placed: Book has one publisher, so it can be a field. Publisher has many books, so it can’t be a field. date Book ISBNtitlenum_copiesis_self_pubpub_IDpub_date ……………

10 One-to-many with total participation Product Makes IDaddress Company name ID Company IDnameaddress ……… Product IDnameco_ID ……… CREATE TABLE Company ( … PRIMARY KEY (ID) ); CREATE TABLE Product ( … co_ID VARCHAR(50) NOT NULL, PRIMARY KEY (ID), FOREIGN KEY (co_ID) REFERENCES Company (ID) ); Related item must have a value.

11 One-to-many with weak entity Team Affiliation University namesport ID University name … Team univ_nameIDsport ……… CREATE TABLE University ( … PRIMARY KEY (name) ); CREATE TABLE Team ( … PRIMARY KEY (univ_name, ID), FOREIGN KEY (univ_name) REFERENCES University (name) ); Related item must have a value.

12 Activity: Implement three similar ideas Product name category price Person p_id Purchased date Product name category price Person p_id date Purchase quantitypurch_id ProductOfBuyerOf Product name category price Person p_id date Purchase quantity ProductOfBuyerOf Each results in a Purchase junction table. But what primary keys does it have?

13 One-to-many with total participation Concession p_idstate_idd_iddate ………… CREATE TABLE Concession ( … PRIMARY KEY (p_id, state_id), FOREIGN KEY (p_id) REFERENCES Product (p_id), FOREIGN KEY (state_id) REFERENCES State (state_id), FOREIGN KEY (d_id) REFERENCES Dealer (d_id) ); Concession Dealer Product State p_id state_id d_id date

14 One-to-one with same key Player ContactInfoStatistics name IDpointsIDaddress Player IDnameaddresspoints ………… Usually indicates a poor design. Combine into one entity set.

15 One-to-one with different keys Chair FacultyDept name emp_IDnamedept_IDoffice Faculty emp_IDnameofficedept_id ………… Sometimes indicates a poor design. Dept dept_IDnamechair_id ……… CREATE TABLE Faculty ( … PRIMARY KEY (emp_ID), FOREIGN KEY (dept_ID) REFERENCES Dept (dept_ID) ); CREATE TABLE Department ( … PRIMARY KEY (dept_ID), FOREIGN KEY (chair_ID) REFERENCES Faculty (emp_ID) ); Same optimization as for one-to-many.

16 Parallel one-to-many/one relationships Vehicle Owns IDaddress Person name modelID Services Person IDnameaddress ……… Vehicle IDmodelowner_IDmechanic_ID ……… CREATE TABLE Person ( … PRIMARY KEY (ID) ); CREATE TABLE Vehicle ( … PRIMARY KEY (ID), FOREIGN KEY (owner_ID) REFERENCES Person (owner_ID), FOREIGN KEY (mechanic_ID) REFERENCES Person (owner_ID) ); Same idea as before, for each relationship.

17 Arity & partiticipation: Some less common cases

18 Many-to-many with total participation Purchase ProductPerson name ID name ID Purchase prod_IDperson_ID …… CREATE TABLE Purchase ( … PRIMARY KEY (prod_ID, person_ID), FOREIGN KEY (prod_ID) REFERENCES Product (ID), FOREIGN KEY (person_ID) REFERENCES Person (ID) ); Can enforce with a CHECK constraint. See later.

19 Many-to-many with total participation Purchase ProductPerson name ID name ID Classic chicken-and-egg problem: No Product can be created without a related Person. No Person can be created without a related Product.

20 One-to-many with total participation Product Makes IDaddress Company name ID Company IDnameaddress ……… Product IDname …… CREATE TABLE Company ( … PRIMARY KEY (ID) ); CREATE TABLE Product ( … PRIMARY KEY (ID) ); Makes co_IDprod_ID …… CREATE TABLE Makes ( … PRIMARY KEY (co_ID), FOREIGN KEY (co_ID) REFERENCES Company (ID), FOREIGN KEY (prod_ID) REFERENCES Product (ID) ); Can use CHECK constraint.

21 Subclasses, superclasess, unions

22 Subclasses – Three strategies Product name price Software Product platforms Educational Product age_group   Dealing with sub- & superclasses is one of relational database’s weaknesses.

23 Subclasses – Strategy 1 Product name price Software Product platforms Educational Product age_group   Product(name, price) SoftwareProduct(name, platforms) EducationalProduct(name, age_group) + Semantically, most accurate representation. - Typically leads to lots of joins between sub- & superclass

24 Subclasses – Strategy 2 HourlyEmployee(ID, name, rate) SalaryEmployee(ID, name, salary) + Avoids joins between sub- & superclasses. -Requires disjoint subclasses. -Poor if superclass is directly related to other entity sets. Employee ID name HourlyEmployee rate SalaryEmployee salary D  

25 Subclasses – Strategy 3 Product name price Software Product platforms Educational Product age_group   Product(name, price, platforms, age_group) Use NULL when attribute not applicable. + Avoids joins between sub- & superclasses. - Joins between subclass & other entity sets now involve more data.

26 Book Writes Publishes GoesOnLoan Borrower Author ID address Activity: Implemen t as Schemas Publisher Loan Borrows name ISBN title num_copies address name card_num date Chapter PartOf title ID Writing  is_self_pub


Download ppt "COMP 430 Intro. to Database Systems ER Implementation as Tables Slides use ideas from Chris Ré."

Similar presentations


Ads by Google