Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

21
Week 7-8 DBMS ER-Relational Mapping

Transcript of Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Page 1: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Week 7-8

DBMSER-Relational

Mapping

Page 2: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER-Relational Mapping

Page 3: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER to Relational Mapping…• In the Database Design process, we firstly derive a conceptual

model (ER Diagram)

• This model needs to be mapped to the relational model in order to be implemented using a relational DBMS (RDBMS)

• This section discusses the rules that can be used for this process…

Page 4: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Model Mapping • Moving from Conceptual (ER) to lower level

Logical Model (Relational)

• ER is independent of the details of the implementation (relational, network or OO)_

• Logical model begins to introduce issues specific to implementations (as realtional tables)

• Any such conversion is called a “schema mapping”

Page 5: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Core Concepts (Review)

Entity Any object about which data is stored

Relation 2-d table to implement storage of data abut entities

Attribute A property of an entity; stored as a column in a table

Entity Instance

A row (tuple) in a table (relation)

A Key The use of an attribute to identify specific instances within a table

Page 6: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Core Concepts (Review)

Candidate Key

Any attribute that uniquely identifies each row in a table

Primary Key An attribute selected from the candidate keys to be used to uniquely identify each row

Composite Key

A primary key tat is made up of two or more attributes

Foreign Key An attribute that is not itself a key but is a PK in another table; to join tables in relational databases

Page 7: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER to Relational Mapping… (contd.)

ER Model Relational Model

– Entity (strong) Relation

– For example,

ARTIST

ARTIST

Page 8: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER Model Relational Model

– Simple Attributes Attributes

– For example,

name

ARTIST

ER to Relational Mapping… (contd.)

name

ARTIST

Page 9: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER Model Relational Model

– Primary Key Primary Key

– For example,

ARTIST

ER to Relational Mapping… (contd.)

name

ARTIST

name

Page 10: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER Model Relational Model

– Composite attributes Set of simple atomic attributes

– For example, EMPLOYEE

fullname

surname

firstname

ER to Relational Mapping… (contd.)

firstname

EMPLOYEE

surname

Page 11: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Rules to Convert (ER – Tables)

• For 1:1 Cardinality, all attributes should be merged into single table

• 1:N ; post identifier (PK) from one side as an attribute into the many side

• N:M ; create a new table and post identifiers from each of the linked entities as attributes in the table

Page 12: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Conversion

Page 13: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Conversion

FK

Page 14: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Conversion

FK

FK

Page 15: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER Model Relational Model

– Multivalued attribute Relation & Foreign Key

– For example,

ER to Relational Mapping… (contd.)

ART-OBJECT

materialsid

id

ART-OBJECT

idmaterialMATERIALS

Page 16: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER Model Relational Model– N-ary relationship “Relationship” relation and n

foreign keys

– For example,

pkC

pkApkB

pkD

pkDpkCpkBpkA

A B

R

C D

A

C

B

D

r

ER to Relational Mapping… (contd.)

Page 17: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER to Relational Mapping… (contd.)ER Model Relational Model

Weak Entity Relations and combination of partial and primary keys

PolicyEMPLOYEE

name ageid

DEPENDENTS1 N

Page 18: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

ER to Relational Mapping… (contd.)

id

EMPLOYEE

agenameid

DEPENDENTS

Page 19: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Mapping - SummaryER Model Relational Model

Entity (strong) -> RelationSimple Attributes -> AttributesPrimary Key -> Primary KeyComposite attributes -> Set of simple attributes1:1 or 1:N relationship -> Foreign keys M:N relationship -> Relation and foreign keysMultivalued attribute -> Relation and foreign keyN-ary relationship -> Relation and n foreign keysWeak Entity -> Relation and combination of

-> primary and partial keys

Page 20: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

Your Turn

Page 21: Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.

• Use the rules introduced to build a set of tables

• Have two 1:N relations between Branch & Film

• Examine the link between Customer and Film copy