You are on page 1of 39

# How to translate ER Model to Relational Model

Review - Concepts
Relational Model is made up of tables A row of table A column of table A table Cardinality Degree = a relational instance/tuple = an attribute = a schema/relation = number of rows = number of columns

Review - Example
Attribute
Cardinality = tuple/relational instance

Major CS EE

## GPA 2.8 3.6

2 4 Degree

A Schema / Relation

## Reduction to Relation Schemas

Primary keys allow entity sets and relationship sets to be expressed uniformly as relation schemas that represent the contents of the database. A database which conforms to an E-R diagram can be represented by a collection of schemas. For each entity set and relationship set there is a unique schema that is assigned the name of the corresponding entity set or relationship set. Each schema has a number of columns (generally corresponding to attributes), which have unique names.

## From ER Model to Relational Model

So how do we convert an ER diagram into a table?? Simple!!
Basic Ideas: Build a table for each entity set Build a table for each relationship set if necessary (more on this later) Make a column in the table for each attribute in the entity set Indivisibility Rule and Ordering Rule Primary Key

## Representing Entity Sets as Schemas

A strong entity set reduces to a schema with the same attributes. Attributes becomes column of the tables( degree) Entities will represent rows of the table (cardinality)

## Example Strong Entity Set

SID Name SSN Name Student

Professor Dept

Major

GPA

Dept Math CS

## Representation of Weak Entity Set

Weak Entity Set Cannot exists alone To build a table/schema for weak entity set Construct a table with one column for each attribute in the weak entity set Remember to include discriminator Augment one extra column on the right side of the table, put in there the primary key of the Strong Entity Set (the entity set that the weak entity set is depending on) Primary Key of the weak entity set = Discriminator + foreign key

## Example Weak Entity Set

Loan_id amount Payment_no Date Loan

Loan_payment

payment Payment_amount

Payment_n o 1 2

## Representation of Relationship Set

--This is a little more complicated
Unary/Binary Relationship set
Depends on the cardinality and participation of the relationship Two possible approaches

## N-ary (multiple) Relationship set

Primary Key Issue

Identifying Relationship
No relational model representation necessary

## Representing Relationship Set

Unary/Binary Relationship
For one-to-one relationship sets, either side can be chosen to act as the many side That is, extra attribute can be added to either of the tables corresponding to the two entity sets. If participation is partial on the many side, replacing a schema by an extra attribute in the schema corresponding to the many side could result in null values

## Representing Relationship Set

Unary/Binary Relationship
For one-to-one relationship w/out total participation Build a table with two columns, one column for each participating entity sets primary key. Add successive columns, one for each descriptive attributes of the relationship set (if any).

## Example One-to-One Relationship Set

Degree SID Name ID Code

Student

study

Major

Major

GPA

Maj_ID Co 07 05

## Representing Relationship Set

Unary/Binary Relationship
For one-to-one relationship with one entity set having total participation Augment one extra column on the right side of the table of the entity set with total participation, put in there the primary key of the entity set without complete participation as per to the relationship.

## Example One-to-One Relationship Set

Condition SID Name 1:1 Relationship Have Laptop S/N #

Student

Major

GPA

Brand

SID 9999

Name Bart

Major Economy

GPA -4.0

LP_S/N 123-456

Hav_Cond Own

8888

Lisa

Physics

4.0

567-890

Loan

## Representing Relationship Set

Unary/Binary Relationship
Many-to-one and one-to-many relationship sets that are

total on the many-side can be represented by adding an extra attribute to the many side, containing the primary key of the one side
Example: Instead of creating a schema for relationship

set account_branch, add an attribute branch_name to the schema arising from entity set account

## Representing Relationship Set

Unary/Binary Relationship

Account_number

Balance

Branch Name

## Representing Relationship Set

Unary/Binary Relationship
For one-to-many relationship w/out total participation :
Same thing as one-to-one.

## Representing Relationship Set

Unary/Binary Relationship
For one-to-many/many-to-one relationship with one entity set having total participation on many side Augment one extra column on the right side of the table of the entity set on the many side, put in there the primary key of the entity set on the one side as per to the relationship

## Example Many-to-One Relationship Set

Semester SID Name N:1 Relationship SSN

Student

Professor

Major

GPA

Dept

Name

SID 9999

Name Bart

Major Economy

GPA -4.0

Pro_SSN 123-456

8888

Lisa

Physics

4.0

567-890

Fall 2005

## Representing Relationship Set

Unary/Binary Relationship
For many-to-many relationship Same thing as one-to-one relationship without total participation. Primary key of this new schema is the union of the foreign keys of both entity sets. No augmentation approach possible

## Representing Relationship Set

N-ary Relationship
Intuitively Simple Build a new table with as many columns as there are attributes for the union of the primary keys of all participating entity sets. Augment additional columns for descriptive attributes of the relationship set (if necessary) The primary key of this table is the union of all primary keys of entity sets that are on many side That is it, we are done.

P-Key1

## Example N-ary Relationship Set

D-Attribute E-Set 1 A-Key

P-Key2
E-Set 2 P-Key3

A relationship

Another Set

E-Set 3

P-Key1 9999

P-Key2 8888

P-Key3 7777

A-Key 6666

D-Attribute Yes

1234

5678

9012

3456

No

## Representing Relationship Set

Identifying Relationship
This is what you have to know You DONT have to build a table/schema for the identifying relationship set once you have built a table/schema for the corresponding weak entity set Reason: A special case of one-to-many with total participation Reduce Redundancy

## Representing Composite Attribute

Relational Model Indivisibility Rule Applies One column for each component attribute NO column for the composite attribute itself

SSN

Name

SSN

Name

Street

City

9999
8888

Dr. Smith
Dr. Lee

50 1st St.
1 B St.

Fake City
San Jose

Street

City

## Representing Multivalue Attribute

For each multivalue attribute in an entity set/relationship set Build a new relation schema with two columns One column for the primary keys of the entity set/relationship set that has the multivalue attribute Another column for the multivalue attributes. Each cell of this column holds only one value. So each value is represented as an unique tuple Primary key for this schema is the union of all attributes

## Example Multivalue attribute

SID Name Student

Children

The primary key for this table is Student_SID + Children, the union of all attributes

Major

GPA

Stud_SID 1234 SID 1234 5678 Name John Homer Major CS EE GPA 2.8 3.6 1234 5678 5678 5678

## Representing Class Hierarchy

Used to represent specialization via schema Two general approaches depending on disjointness and completeness

## Representing Specialization via Schemas

Method 1: Form a schema for the higher-level entity Form a schema for each lower-level entity set, include primary key of higher-level entity set and local attributes

Example
SID Status

SSN

Name

Person

Major

GPA

1234
5678

9999
8888

Full
Part

CS
EE

2.8
3.6

## Representing Specialization via Schemas

Drawback: getting information about, an employee requires accessing two relations, the one corresponding to the low-level schema and the one corresponding to the high-level schema

## Representing Class Hierarchy

Two general approaches depending on disjointness and completeness For disjoint AND complete mapping class hierarchy: DO NOT create a table for the super class entity set Create a table for each subclass entity set include all attributes of that subclass entity set and attributes of the superclass entity set

Method 2: Form a schema for each entity set with all local and inherited attributes

## Representing Specialization as Schemas (Cont.)

schema
person student faculty

attributes
ssn, name ssn, sid, name,status, gpa, major ssn, name, dept

If specialization is total, the schema for the generalized entity set (person) not required to store information

## Can be defined as a view relation containing union of specialization relations

But explicit schema may still be needed for foreign key constraints

Example

SSN

Name

person

SID

Major

GPA

Dept

SSN

Name

SID

Major

GPA

SSN

Name

Dept

1234
5678

John
Mary

9999
8888

CS
EE

2.8
3.6

1234
5678

Homer
Marge

C.S.
Math

## Schemas Corresponding to Aggregation

To represent aggregation, create a schema containing

primary key of the aggregated relationship, the primary key of the associated entity set any descriptive attributes

Representing Aggregation
Name SSN Name Student

Professor Dept

SID

SID

Code

1234
5678

04
08

## Schemas Corresponding to Aggregation (Cont.)

To represent aggregation manages between relationship works_on and

entity set manager, create a schema manages (employee_id, branch_name, title, manager_name)
Schema works_on is redundant provided we are willing to store null

Applause??
Thank you