90 likes | 116 Views
Learn a simple algorithm to translate Entity-Relationship schemas to relational models with examples and key concepts.
E N D
Simple Algorithm • Entity type E Relation E’ • Attribute of E Attribute of E’ • Key for E Primary Key for E’ cs3431
Simple Algorithm • For relationship type R between E1, E2, …, En • Create separate relation R’ • Attributes of R’ are : • primary keys of E1, E2, …, En and attributes of R • Primary Key for R’ is defined as: • Primary key for R’ = primary keys for E1, E2, …, En • Foreign Keys: • Define “appropriate” foreign keys from R’ to E1, E2, …, En cs3431
Simple algorithm: Example 1 Person (pNumber, pName) Dept (dNumber, dName) WorksFor (pNumber, dNumber, years) PRIMARY KEY (Person) = <pNumber> PRIMARY KEY (Dept) = <dNumber> PRIMARY KEY (WorksFor) = <pNumber, dNumber> FOREIGN KEY WorksFor (pNumber) REFERENCES Person (pNumber) FOREIGN KEY WorksFor (dNumber) REFERENCES Dept (dNumber) cs3431
Simple Algorithm: Example 2 Supplier (sName, sLoc) Consumer (cName, cLoc) Product (pName, pNumber) Supply (supplier, consumer, product, price, qty) PRIMARY Key (Supplier) = <sName> PRIMARY Key (Consumer) = <cName> PRIMARY Key (Product) = <pName> PRIMARY Key (Supply) = <supplier, consumer, product> FOREIGN KEY Supply (supplier) REFERENCES Supplier (sName) FOREIGN KEY Supply (consumer) REFERENCES Consumer (cName) FOREIGN KEY Supply (product) REFERENCES Product (pName) cs3431
Simple Algorithm: Example 3 Part (pName, pNumber) Contains (superPart, subPart, quantity) PRIMARY KEY (Part) = <pNumber> PRIMARY KEY (Contains) = <subPart,superPart> FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) cs3431
Refinement for Simple Mapping • Primary Key for R’ is defined as: • If the maximum cardinality of any Ei is 1, primary key for R’ = primary key for Ei cs3431
Simple Algorithm: Example 3 Part (pName, pNumber) Contains (superPart, subPart, quantity) PRIMARY KEY (Part) = <pNumber> PRIMARY KEY (Contains) = <subPart> FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) cs3431
Summary So Far • Simple algorithm covers base case • Idea: “Each relationship type = separate relation” NEXT: • Let’s consider constraints • Let’s reduce number of relations cs3431