26
CS3431: C-Term 2013 1 Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh [email protected]

Translating ER Schema to Relational Model

  • Upload
    fauna

  • View
    58

  • Download
    1

Embed Size (px)

DESCRIPTION

Translating ER Schema to Relational Model. Instructor: Mohamed Eltabakh [email protected]. First: Check Your Oracle Account. Design and Build Phases. Phase 1. Phase 2. Phase 3. ER Model & ERD. Build the database. Relational Model. Translating ER Schema to Relational Schema. - PowerPoint PPT Presentation

Citation preview

Page 1: Translating ER Schema to Relational Model

CS3431: C-Term 2013 1

Translating ER Schema to Relational Model

Instructor: Mohamed Eltabakh [email protected]

Page 2: Translating ER Schema to Relational Model

First: Check Your Oracle Account

cs3431 2

Page 3: Translating ER Schema to Relational Model

Design and Build Phases

3

ER Model & ERD

Phase 1 Phase 2 Phase 3

Relational Model

Build the database

Page 4: Translating ER Schema to Relational Model

Translating ER Schema to Relational Schema

Primary keys allow entity sets and relationship sets to be expressed uniformly as relational schemas

Generally, each relational schema will have Number of columns corresponding to the number of attributes

in ERD Column names that correspond to the attribute names

cs3431 4

Page 5: Translating ER Schema to Relational Model

cs3431 5

Basic Mapping

Simple algorithm covers base the cases, Idea: Each entity set separate relation Each relationship type separate relation

Define the primary keys as discussed in the ER Model

More optimizations will come …More optimizations will come …

Page 6: Translating ER Schema to Relational Model

Example 1

Loan (load_number, amount)

Customer (customer_id, customer_name, customer_street, customer_city)

Borrower (customer_id, load_number) – Many-to-Many Relationship

FOREIGN KEY Borrower (customer_id) REFERENCES Customer (customer_id)

FOREIGN KEY Borrower (loan_number) REFERENCES Loan (loan_number)cs3431 6

Page 7: Translating ER Schema to Relational Model

Example 2

Dept (dNumber, dName)

Course (cNumber, cName)

Offers (dNumber, cNumber) -- One-to-Many Relationship from Dept to Course

FOREIGN KEY Offers(dNumber) REFERENCES Dept(dNumber)

FOREIGN KEY Offers (cNumber) REFERENCES Course(cNumber)

cs3431 7

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)CourseCourseoffersoffers

Page 8: Translating ER Schema to Relational Model

Example 3

Product (pName, pNumber) Supplier (sName, sLoc) Consumer(cName, cLoc) Supply (sName, cName, pName, price, qty)

FOREIGN KEY Supply(sName) REFERENCES Supplier(sName) FOREIGN KEY Supply (pName) REFERENCES Product(pName) FOREIGN KEY Supply (cName) REFERENCES Consumer(cName)

cs3431 8

Page 9: Translating ER Schema to Relational Model

Example 4

Part(pNumber, pName) Contains(super_pNumber, sub_pNumber, quantity)

FOREIGN KEY Contains (super_pNumber) REFERENCES Part (pNumber) FOREIGN KEY Contains (sub_pNumber) REFERENCES Part (pNumber)

cs3431 9

Contains

Part

pName pNumber

Is-subpartsuperPart

quantity

(0, 1)(0, *)

Page 10: Translating ER Schema to Relational Model

Rule I: Weak Entity Sets Weak entity set does not have its own key

It must relate to the identifying entity set via a total, one-to-many relationship set from the identifying to the weak entity set

10

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)

A weak entity set is mapped to a relation with all its attributes + the key(s) of the identifying entity set(s)

Primary key of the new relation is the: Identifying key(s) from identifying entity set(s), Plus Discriminator of the weak entity set

Supporting relationship is not mapped

Page 11: Translating ER Schema to Relational Model

Example 5

Dept(dNumber, dName) Course(dNumber, cNumber, cName)

FOREIGN KEY Course(dNumber) REFERENCES Dept(dNumber)

cs3431 11

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)

Page 12: Translating ER Schema to Relational Model

Rule II: One-to-Many & Many-to-One Cardinalities

Many-to-one and one-to-many relationship sets can be represented by adding an extra attribute(s) to the “many” side, containing the primary key of the “one” side

This transferred primary key becomes a foreign key

The relationship itself is not mapped to the relational model Any attributes on the relationship go to the “Many” side If they are part of the key they will go to the “Many” side as part of the key.

cs3431 12

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)CourseCourseoffersoffers

Page 13: Translating ER Schema to Relational Model

Example 6

Dept (dNumber, dName)

Course (cNumber, term, dnumber, cName)

FOREIGN KEY Course(dNumber) REFERENCES Dept(dNumber)

cs3431 13

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)CourseCourseoffersoffers

termterm

Note: Course.dnumber is not part of a primary key unlike Example 5Note: Course.dnumber is not part of a primary key unlike Example 5

Page 14: Translating ER Schema to Relational Model

Example 7

Dept (dNumber, dName)

Course (cNumber, dnumber, cName)

FOREIGN KEY Course(dNumber) REFERENCES Dept(dNumber)

cs3431 14

Dept Offers Course

dNamedNumber cNamecNumber

(1, 1)(0, *)CourseCourseoffersoffers

Compare this with Example 6-- In Example 6: Course.dnumber can be null--In Example 7: Course.dnumber cannot be null

Compare this with Example 6-- In Example 6: Course.dnumber can be null--In Example 7: Course.dnumber cannot be null

Open head (one and must be one)

Page 15: Translating ER Schema to Relational Model

Rule III: One-to-One Cardinalities

One-to-one relationship sets can be represented by adding the primary key of either sides to the other side

This transferred primary key becomes a foreign key

The relationship itself is not mapped to the relational model Any attributes on the relationship go to the side receiving the transferred primary key

cs3431 15

PlayerPlayer

pNamepName

pIDpID

Storage areaStorage area

NumberNumber

LocationLocation

sizesize

ownsowns

StartDateStartDate

Page 16: Translating ER Schema to Relational Model

Example 8

Player(pID, pNumber)

StorageArea(Number, pID, startDate, Location, size)

FOREIGN KEY StorageArea(pID) REFERENCES Player(pID)

cs3431 16

PlayerPlayer

pNamepName

pIDpID

Storage areaStorage area

NumberNumber

LocationLocation

sizesize

ownsowns

StartDateStartDate

Page 17: Translating ER Schema to Relational Model

Rule IV: Many-to-Many Relationship

Each entity set maps to a relation The relationship also maps to a relation

Key of relationship = keys coming from both sides +

Any key of the relationship itself

17

Loan (load_number, amount)

Customer (customer_id, customer_name, customer_street, customer_city)

Borrower (customer_id, load_number, Date)

DateDate

Page 18: Translating ER Schema to Relational Model

cs3431 18

Rule V: Composite & Derived Attributes

Student

sNamesNum

sAge

statestreet

address

city

Mapping strategy (Composite): Include an attribute for every primitive component of the composite attribute in the entity

Mapping strategy (Derived): Mapped as is (enforced later using triggers)

Student(sNum, sName, sAge, street, city, state)

Page 19: Translating ER Schema to Relational Model

cs3431 19

Rule VI: Multi-valued Attributes

Student

sNamesNum

sAge

statestreet

address

city

Mapping strategy: • Represented as a relation by itself. • The primary key of that relation = Attribute + the primary key of the main

entity set

Student(sNum, sName, sAge, street, city, address)StudentMajor(sNum, major)

FOREIGN KEY StudentMajor (sNum) REFERENCES Student (sNum)

major

Page 20: Translating ER Schema to Relational Model

Rule VII: ISA Relationships

ISA is a one-to-one relationship BUT the sub-class entity sets inherit attributes from the super-class entity set That is why it does not follow the one-to-one rules

Basically many ways for the mapping depending on whether it is total vs. partial and overlapping vs. disjoint

Super-class key is always the primary key

20

Page 21: Translating ER Schema to Relational Model

21

ISA Relationship : Method 1 (Relation for each Entity Set)

Person(SSN, Name, DoB)Student(SSN, GPA, StartDate)Employee(SSN, Department, Salary)

In this design:•Each student has two records (one in Person, and one in Student) They complete each other

•Each employee has two records (one in Person, and one in Employee) They complete each other

FOREIGN KEY Student(SSN) REFERENCES Person(SSN)FOREIGN KEY Employee(SSN) REFERENCES Person(SSN)

Page 22: Translating ER Schema to Relational Model

cs3431 22

ISA Relationship : Method 2 (One Relation for All)

Person(SSN, Name, DoB, GPA, StartDate, Salary, Department)

In this design:•Any person will have only one record•But, there will be many null values

Page 23: Translating ER Schema to Relational Model

cs3431 23

ISA Relationship : Method 3 (Relations only for SubClasses)

• Good for total & disjoint type

• Cannot be used for partial (otherwise some entities will not fit in any relation)

• If the relationship is overlapping there will some redundancy

Student(SSN, Name, DoB, GPA, StartDate)Employee(SSN, Name, DoB, Department, Salary)

>> Create a relation for each subclass only (not the parent)

Page 24: Translating ER Schema to Relational Model

24

ISA Relationship : Method 4 (Relation for each combination)

Student(SSN, Name, DoB, GPA, StartDate)Employee(SSN, Name, DoB, Department, Salary)StudentEmp(SSN, Name, DoB, GPA, StartDate, Salary, Department)

In this design:•Any person will have only one record in only one of the tables

•Good for overlapping relationship

If relationship is total The above relations are enough

If relationship is partial we need a relation for “Person(SSN, Name, DoB)”

Page 25: Translating ER Schema to Relational Model

cs3431 25

Mapping from ER model to Relational model: Summary

Basic algorithm covers the main cases

Rule I : Weak Entity Sets

Rule II : One-to-Many Relationships

Rule III : One-to-One Relationships

Rule IV : Many-to-Many Relationships

Rule V: Composite & Derived Attributes

Rule VI : Multi-Valued Attributes

Rule VII : ISA Relationships

Page 26: Translating ER Schema to Relational Model

What about an Exercise

cs3431 26

Author(name, address, URL)Book(ISBN, title, year, price, publisher_Name)

WrittenBy(name, address, ISBN)Publisher(name, address, phone, URL)

Warehouse(code, phone, address)Stocks(ISBN, WH_code, number)

Shopping-Basket(basketID, email)

basketContains(ISBN, basketID, number)

Customer(email, name, address, phone)