• No results found

Converting E-R Diagrams to Relational Model. Winter Lecture 17

N/A
N/A
Protected

Academic year: 2021

Share "Converting E-R Diagrams to Relational Model. Winter Lecture 17"

Copied!
30
0
0

Full text

(1)

Converting E-R Diagrams

to Relational Model

Winter 2006-2007

Lecture 17

(2)

E-R Diagrams

• Need to convert E-R model diagrams to an

implementation schema

• Easy to map E-R diagrams to relational model,

and then to SQL

– Significant overlap between E-R model and relational model

– Biggest difference is E-R composite/multivalued attributes, vs. relational model atomic attributes

• Three components of conversion process:

– Specify schema of relation itself – Specify primary key on the relation

(3)

Strong Entity-Sets

• Strong entity-set

E

with attributes

a

1

,

a

2

, …,

a

n

– Assume simple, single-valued attributes for now

• Create a relational schema with same name

E

,

and same attributes

a

1

,

a

2

, …,

a

n

• Primary key of relational schema is same as

primary key of entity-set

– No foreign key references for strong entity-sets

• Every entity in

E

represented by a tuple in

corresponding relation

(4)

Entity-Set Examples

• Geocache location E-R diagram:

– Entity-set named

location

• Convert to relation schema:

location(latitude, longitude, description, last_visited)

location latitude

longitude

last_visited description

(5)

Entity-Set Examples (2)

• E-R diagram for customers and loans:

• Convert

customer

and

loan

entity-sets:

customer

(

cust_id

,

name

,

street_address

,

city

)

loan

(

loan_id

,

amount

)

customer cust_id name street_address city borrower loan loan_id amount access_date

(6)

Relationship-Sets

• Relationship-set

R

– Assume all participating sets are strong entity-sets, for now

a1, a2, …, am is the union of all participating entity-sets’ primary key attributes

b1, b2, …, bn are descriptive attributes on R (if any)

• Relational schema for

R

is:

– {a1, a2, …, am} ( {b1, b2, …, bn}

• {

a

1

,

a

2

, …,

a

m

} is a superkey, but not necessarily

a candidate key

(7)

Relationship-Set Primary Keys

• For binary relationship-sets:

– e.g. between strong entity-sets

A

and

B

– If many-to-many mapping, union of all

entity-set primary keys becomes primary key of

relationship-set

primary_key(A) ( primary_key(B)

– If one-to-one mapping, either entity-set’s

primary key is acceptable

primary_key(A), or primary_key(B)

(8)

Relationship-Set Primary Keys (2)

• For many-to-one or one-to-many mappings:

– e.g. between strong entity-sets A and B

– Primary key of entity-set on “many” side is primary key of relationship

• Example: relationship

R

between

A

and

B

– One-to-many mapping, with B on “many” side

– Schema contains primary_key(A) ( primary_key(B), plus any descriptive attributes on R

(9)

Relationship-Set Foreign Keys

• Relationship-sets associate entities in entity-sets

– Need foreign key constraints on relation schema for R

• For each entity-set

E

i

participating in

R

:

– Relation schema for R has a foreign-key constraint on

Ei relation, for primary_key(Ei) attributes

• Relation schema notation doesn’t provide a

mechanism for indicating foreign key constraints

– Don’t forget about foreign keys and candidate keys! – Can specify both foreign key constraints, and

(10)

Relationship-Set Example

• Relation schema for

borrower

:

– Primary key of customer is cust_id

– Primary key of loan is loan_id

– Descriptive attribute access_date

borrower mapping cardinality is many-to-many

borrower(cust_id, loan_id, access_date)

customer cust_id name street_address city borrower loan loan_id amount access_date

(11)

Relationship-Set Example (2)

• Relation schema for

employee

entity-set:

employee(employee_id, name)

• Relation schema for

works_for

:

– One-to-many mapping from manager to worker

– “Many” side is used for primary key

works_for(employee_id, manager_id)

employee works_for

manager

worker

employee_id name

(12)

N-ary Relationship Primary Keys

• For degree > 2 relationship-sets:

– If no arrows (“many-to-many” mapping),

relationship-set primary key is union of all

participating entity-sets’ primary keys

– If one arrow (“one-to-many” mapping),

relationship-set primary key is union of

primary keys of entity-sets without an arrow

– Don’t allow more than one arrow for

(13)

N-ary Relationship-Set Example

• Entity-set schemas:

job(title, level)

employee(employee_id, employee_name)

branch(branch_name, branch_city, assets)

• Relationship-set schema:

– Primary key includes entity-sets on non-arrow links

works_on(employee_id, branch_name, title)

employee branch job works_on title level branch_name assets branch_city employee_id employee_name

(14)

Weak Entity-Sets

• Weak entity-sets depend on at least one strong

entity-set

– Identifying entity-set, or owner entity-set

– Relationship between the two called the identifying relationship

• Weak entity-set

A

owned by strong entity-set

B

– Attributes of A are {a1, a2, …, am} – primary_key(B) = {b1, b2, …, bn}

– Relational schema for A: {a1,a2,…,am} ( {b1,b2,…,bn} – Primary key of A is discriminator(A) ( primary_key(B) – A has foreign key constraint on primary_key(B), to B

(15)

Identifying Relationship?

• Identifying relationship is many-to-one,

with no descriptive attributes

• Relational schema for weak entity-set

includes primary key for strong entity-set

– Foreign key constraint imposed, too

• No need to create relational schema for

identifying relationship

– Would be redundant to weak entity-set’s

relational schema!

(16)

Weak Entity-Set Example

account

schema:

account(account_number, balance)

check

schema:

– Discriminator is check_number

– Primary key for check is:

(account_number, check_number)

check(account_number, check_number,

check_date, recipient, amount, memo)

check check_number amount check_date recipient memo account account_number balance check_txn

(17)

Weak Entity-Set Example (2)

• Schemas for strong entity-sets:

student(username)

assignment(shortname, due_date, url)

• Schema for

submission

weak entity-set:

– Discriminator is version

– Both student and assignment are owners!

submission(username, shortname, version,

submit_date, data)

student assignment

username shortname due_date url submit_date data

submission

submit complete version

(18)

Schema Combination

• Relationship between weak entity-set and strong

entity-set doesn’t need represented separately

– Many-to-one relationship

– Weak entity-set has total participation

– Weak entity-set’s schema includes representation of identifying relationship

• Can apply technique to other relationship-sets

with many-to-one mapping

– Entity-sets A and B, with relationship-set AB

– Many-to-one mapping

(19)

Schema Combination (2)

• Entity-sets

A

and

B

, relationship-set

AB

– Many-to-one mapping

A’s participation in AB is total

• Generates relation schemas

A

,

B

,

AB

– Primary key of AB is primary_key(A)

• (A is on “many” side of mapping)

AB has foreign key constraints on both A and B

• Combine

A

and

AB

relation schemas

– Primary key of combined schema still primary_key(A) – Only need one foreign-key constraint, to B

(20)

Schema Combination (3)

• If

A

’s participation in

AB

is partial, can still

combine schemas

– Need to store null values for primary_key(B) attributes when an entity in A maps to no entity in B

• If

AB

is one-to-one mapping:

– Can also combine schemas in this case

– Could incorporate AB into schema for A, or schema for B

– When relationship-set is combined into an entity-set, the entity-set’s primary key doesn’t change!

(21)

Schema-Combination Example

• Manager to worker mapping is one-to-many

• Relation schemas were:

employee(employee_id, name)

works_for(employee_id, manager_id)

• Could combine into:

employee(employee_id, name, manager_id)

– Need to store null for employees with no manager

employee works_for

manager

worker

employee_id name

(22)

Schema Combination Example (2)

• One-to-one mapping between customers, loans

customer(cust_id, name, street_address, city)

loan(loan_id, amount)

borrower(cust_id, loan_id)

borrower could also use loan_id for primary key

• Could combine

borrower

schema into

customer

or

loan

schema

– Does it matter which one you choose?

customer cust_id name street_address city borrower loan loan_id amount

(23)

Schema Combination Example (3)

• Participation of

loan

in

borrower

will be total

– Combining borrower into customer would require null

values for customers without loans

• Better to combine

borrower

into

loan

schema

customer(cust_id, name, street_address, city)

loan(loan_id, cust_id, amount) – No null values! customer cust_id name street_address city borrower loan loan_id amount

(24)

Schema Combination Example (4)

• Schema:

customer(cust_id, name, street_address, city)

loan(loan_id, cust_id, amount)

• What if, after a while, we wanted to change the

mapping cardinality?

– Change to schema would be significant

– Would need to migrate existing data to new schema

customer cust_id name street_address city borrower loan loan_id amount

(25)

Schema Combination Notes

• Benefits of schema combination:

– Eliminate a foreign-key constraint, and

associated performance impact

• Constraint enforcement

• Extra join operations in queries

– Reduce storage requirements

• Drawbacks of schema combination:

– May necessitate use of

null

values

– Makes it harder to change mapping cardinality

constraints in the future

(26)

Composite Attributes

• Relational model doesn’t handle

composite attributes

• When mapping E-R composite attributes

to relation schema:

– Each component attribute maps to a separate

attribute in relation schema

– In relation schema, simply can’t refer to

composite as a whole

– (Can adjust this mapping for databases that

support composite types)

(27)

Composite Attribute Example

• Customers with addresses

• Each component of

address

becomes a

separate attribute

customer(cust_id, name, street, city, state, zipcode)

customer cust_id name street city zipcode state address

(28)

Multivalued Attributes

• Multivalued attributes require a separate relation

schema

– No such thing as a multivalued attribute in relational model

• For multivalued attribute

M

in entity-set

E

– Create a relation schema R to store M, with attribute

A corresponding to M

A is single-valued version of M

– Attributes of R are: A ( primary_key(E)

– Primary key of R includes all attributes of R

• Each value in M for entity e must be unique

– Foreign key constraint from R to E, on primary_key(E) attributes

(29)

Multivalued Attribute Example

• Customers with

multiple addresses

• Create separate relation to store each address

customer(cust_id, name)

cust_addrs(cust_id, street, city, state, zipcode)

– Large primary keys aren’t ideal – tend to be costly

customer cust_id name street city zipcode state address

(30)

Review

• Can map E-R model schemas to relational

model schemas very easily

– Mapping process is straightforward and unambiguous

• Some flexibility in optimizing relation schemas

– Mapping cardinalities, etc.

• Some E-R concepts are more expensive

– Multivalued attributes (especially composite ones)

• Next time:

– Explore a few more advanced E-R concepts – Generalization/specialization, and aggregation

References

Related documents

It thus defines a salinity gradient, ranging from fresh/brackish water, consisting of the waters of the PC and PB piezometers and the waters of the first three surface samples of

In Models 2 and 3, we investigate the impact of board gender diversity on the extent of earnings management by considering the combined effect of female

In the current study, characteristics of primary pulmonary LELC were systematically investigated, compared with pulmonary squamous carcinoma, including the predispositions, clinical

These procedures should be used as part of a larger compliance program in a health care organization or health care setting to detect unauthorized access to electronic protected

Name and address of owner(s) Date purchased and purchase price Mortgage amount Date loan was issued Name of borrower(s) Present value of property Insurance Income. Real

RUS Financial and Operating Report Electric Distribution - Investments Revision Date 2014 UNITED STATES DEPARTMENT OF AGRICULTURE. RURAL UTILITIES SERVICE FINANCIAL AND

Bill, I don't know what kind of alife vou'llhavc with this catamount.. - thicker than porkerpine

 A história do acampamento é a seguinte.Agentes da Estrela de Aço, ainda leais à Sinter, estavam procurando por ela quanto acamparam aqui na noite anterior à chegada dos PJs.Como

As recorded by Joe Williams and Count

Note: On the main Loan Status screen, LOL User can click on any of the headings to sort the loan list by SONYMA (Reservation) #, Lender Loan #, Borrower Name, Co- Borrower

He puts a small bait fish, hooked through the head, on each hook, sets the drag on the reels and feeds out fifty to seventy-five feet of line.. Then he attends to the rods on

 Database design in E-R model usually converted to design in the relational model (coming up next) which is used for storage and processing. Relational Model

Loan Amount # Approved Credit Limit Current Interest Rate Outstanding Loan Tenor Monthly Instalment Name of Joint Borrower (where applicable). Creditor ^ Type of Facility*

In TORDB, we propose a method of maintaining the reference type (REF) as collection in the ‘one’ side with varying Time period. This typical example is adapted to

원격전이가 없는 갑상선 내부의 여포암(intrathyroidal follicular carcinoma)의 경우 림프절 전이는 거의 없는 것으로 알려져 있지만, 갑상선 외부 연부조직의 침범(extrathyroidal

to represent aggregation manages between relationship works-on and entity set manager, create a table. manages(employee-id, branch-name,

Given the join dependency *((loan number, branch name), (loan number, customer name), (loan number, amount)), Loan info schema is not in PJNF.. To put Loan info schema into PJNF ,

Then the study sample was split by gender, and secondary analy- ses were conducted to determine if race was a significant factor for men in the LACCD, and if so, what similarities

Croix Regional Family Health Center Princeton, ME

1. Sun: When Sun is placed in this Nakshatra the native will be a furious person. His head will be elongated. He will speak vehemently or harshly. His teeth will be ugly

The annual loan amount will be set such that at the life expectancy of the borrower, the sum of all of the annual loans plus capitalized interest will not exceed the

By signing this application, you are consenting to Community Futures East Kootenay (CFEK) for the purposes of making a credit decision, to collect, use and disclose your personal

Borrower shall execute a promissory note for the amount of the City loan secured by a deed of trust to the property with the City of Livermore as beneficiary. Failure by borrower