CS34311 Translating ER Schema to Relational Model
cs34312 Basic Mapping So Far Simple algorithms covers base case Idea: Each entity type = separate relation Each relationship type = separate relation
cs34313 Simple Algorithm: Example 3 PRIMARY KEY (Part) = PRIMARY KEY (Contains) = FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) Part (pName, pNumber) Contains (superPart, subPart, quantity)
cs34314 Next Let’s consider constraints Let’s reduce number of relations
cs34315 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
cs34316 Simple Algorithm: Example 3 PRIMARY KEY (Part) = PRIMARY KEY (Contains) = FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) Part (pName, pNumber) Contains (superPart, subPart, quantity)
cs34317 Decreasing the Number of Relations Technique 1 If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E.
cs34318 Example 1 If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E.
cs34319 Example 1 Student (sNumber, sName, advisor, years) Professor (pNumber, pName) PRIMARY KEY (Student) = PRIMARY KEY (Professor) = FOREIGN KEY Student (advisor) REFERENCES Professor (pNumber) Question: Will Student.advisor attribute ever be NULL ? Answer: No !
cs Example 2 Person (pNumber, pName, dept, years) Dept (dNumber, dName) PRIMARY KEY (Person) = PRIMARY KEY (Dept) = FOREIGN KEY Person (dept) REFERENCES Dept (dNumber) What about NULL attributes ? Dept and years may be null for a person
cs Remember the Simple Algorithm: Example 3 PRIMARY KEY (Part) = PRIMARY KEY (Contains) = FOREIGN KEY Contains (superPart) REFERENCES Part (pNumber) FOREIGN KEY Contains (subPart) REFERENCES Part (pNumber) Part (pName, pNumber) Contains (superPart, subPart, quantity)
cs Example 3 Part (pNumber, pname, superPart, quantity) PRIMARY KEY (Part) = FOREIGN KEY Part (superPart) REFERENCES Part (pNumber) Note: superPart indicates the superpart of a part, and it may be null
cs Decreasing the Number of Relations Summary of Technique 1 If the relationship type R contains an entity type, say E, whose maximum cardinality is 1, then R may be represented as attributes of E. If cardinality of E is (1, 1), then no “new nulls” added If cardinality of E is (0, 1) then may add “new nulls”
cs Decreasing the number of Relations Technique 2 If relationship type R between E1 and E2 is one-to-one [1:1], and the cardinality of E1 or of E2 is (1, 1), then we can combine everything into 1 relation
cs Decreasing the number of Relations Technique 2 - Method Details Let us assume the cardinality of E1 in R is (1, 1). Then we create one relation for entity E2 And, we move all attributes of E1 and for R to be attributes of E2.
cs Example 1 Student-BIG (sNumber, sName, pNumber, pName, years) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Note: pNumber, pName, and years can be null for students with no advisor
cs Example 2 Student (sNumber, sName, pNumber, pName, years) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Note: pNumber cannot be null for any student.
cs Decreasing the Number of Relations Technique 2 If relationship type R between E1 and E2 is one-to-one [1:1], and the cardinality of E1 or of E2 is (1, 1), then we can combine everything into 1 relation Not always recommended! Thus use with care ! While very compact, semantically may not be clearest choice !
cs ER Model: Complex Attributes Composite Attribute: address Student sNamesumer sAge statestreet address city
cs Mapping details Composite attribute in ER Include an attribute for every component of the composite attribute.
cs ER Model: Complex Attributes Multivalued Attribute: major
cs Mapping details Multi-valued attribute in ER We need a separate relation for any multi-valued attribute. Identify appropriate attributes, keys and foreign key constraints.
cs Other details Composite attribute in ER Include an attribute for every component of the composite attribute. Multi-valued attribute in ER We need a separate relation for any multi-valued attribute. Identify appropriate attributes, keys and foreign key constraints.
cs Example: Composite and Multi-valued attributes in ER Student (sNumber, sName, sAge, street, city, state) PRIMARY KEY (Student) = StudentMajor (sNumber, major) PRIMARY KEY (StudentMajor) = FOREIGN KEY StudentMajor (sNumber) REFERENCES Student (sNumber)
cs Summary Simple algorithms covers base case Refinements : Reduce number of relations Refinements: Consider constraints (not NULL) Consider other ER constructs like complex and multi-valued attributes