Sie sind auf Seite 1von 41

Database Management Systems II

Lecture 2

Last Lecture

Introduction to unit Database Design Process Conceptual Modeling

Enhanced ER Model

Any questions???

Class exercise? Practical 1? Tutorial 1?

This Lecture

Logical Database Design


Relational Model E-ER Relational Mapping

Relational Model - Review

The basic construct of the relational model is the relation.

A relation consists of

relation schema relation instance

Relational Model Review (contd.)

Relation Schema = relation name + {name of each field: domain of each field} COURSE (course_no: integer, title: string, credits: integer, year: integer, semester: integer)

Relation Instance = set of tuples

Relational Model Review (contd.)

Example

Relational Model Review (contd.)

A relational database is a collection of relations with distinct relation names

Integrity constraint is a rule (or check) that is enforced by the DBMS to ensure that incorrect data is not stored in the database

Relational Model Review (contd.)

Types of Integrity Constraints:


Domain constraints (domain of a field) Key Constraints (UNIQUE & PRIMARY KEY) Foreign Key Constraints (FOREIGN KEY) Table Constraints (constraints within tables) Assertions (constraints between multiple tables)

Relational Model Review (contd.)

Domain constraints: specify that each attribute A must be an atomic value from the domain of A. Atomic value: That is, the value is non divisible into components

Therefore, relational model doesnt allow multivalued attributes or composite attributes!

Relational database provides data types to specify valid domains.

Relational Model Review (contd.)


Key Constraints (UNIQUE & PRIMARY KEY) The minimal set of attributes that uniquely identify a tuple is called the key of a relation There may be multiple candidate keys in a relation, of which one is chosen as the primary key. Primary key is underlined

Relational Model Review (contd.)


VEHICLE
License Number Engine Make Serial Number Model Year

32-3789
17-2489 54-5680 WP-1268

12342
22495 54356 24322

Toyota
Nissan Toyota Mitsubishi

Corolla
Sunny Vannette Galant

2000
1989 1990 2001

Candidate keys, Super keys, Primary Key

Relational Model Review (contd.)

Foreign Key Constraints

We can designate an attribute(s) as a foreign key(s) Foreign key(s) attribute(s) always refer to attribute(s) of the same or another entity Foreign keys enforce referential integrity constraints

Relational Model Review (contd.)


Foreign key attributes in R1 referring to R2 have the following rules: The FK attributes in R1 have the same domain(s) as the primary key attributes if R2 The value of FK in tuple t1 in R1 must reference an existing PK value in tuple t2 of R2 We can diagrammatically display the foreign keys by drawing an arrow from the foreign key to the primary key

Relational Model Review (contd.)

Referential Integrity Constraints displayed in the relational schema

Relational Model Review (contd.)

Many relational DBMSs support most of the constraints described above. However, many applications require more advanced features for specifying integrity constraints.

Table Constraints (constraints within tables)

Balance of the account should be greater than 0 In SQL, CHECK constraint can be used

Assertions (constraints between multiple tables)

For example: An employees salary should not exceed his/her managers salary Such constraints can be using specific language features such as triggers and assertions

Logical Database Design Revisited

Logical Database Design is the process of converting the E-ER Model to the Relational Model There are a set of conversion rules that can be applied

E-ER to Relational Mapping (Review)


E-ER Model Model
Entity
For example, STUDENT

Relational

Relation

STUDENT

E-ER to Relational Mapping (Review contd.)


E-ER Model Model Simple Attributes For example,
STUDENT name
STUDENT

Relational Attributes

name

E-ER to Relational Mapping (Review contd.)


E-ER Model Model Primary Key For example,
STUDENT name
STUDENT

Relational Primary Key

name

E-ER to Relational Mapping (Review contd.)


E-ER Model Composite attributes Relational Model Set of simple attributes

For example,
EMPLOYEE
fullname

surname
firstname

EMPLOYEE

surfirstname name

E-ER to Relational Mapping (Review contd.)


E-ER Model 1:1 relationship Relational Model Foreign key relationship
EMPLOYEE

For example,
EMPLOYEE
a has id ADDRESS

id

ADDRESS

eid

E-ER to Relational Mapping (Review contd.)


Usually, if there is a 1:1 relationship R from entity A to B and if B is in total participation with A on R then the foreign key is placed in B
A

For example,
A R B
B

E-ER to Relational Mapping (Review contd.)


If A and B are both in total participation with R then A & B are collapsed as 1 table For example,
A R B
AB

E-ER to Relational Mapping (Review contd.)


E-ER Model 1:N Relational Model Foreign key relationship
ARTIST

For example,
ARTIST
yr_ created id creates OBJECTS

id

OBJECT

id

yr_created

E-ER to Relational Mapping (Review contd.)


E-ER Model M:N
ARTIST

name
CREATES

Relational Model Relationship relation and two foreign keys


name id Date created id

ARTIST
DateCreated

name creates

id

ART-OBJECT
ART_OBJECT

E-ER to Relational Mapping (Review contd.)


E-ER Model Multivalued attribute Relational Model Relation & Foreign Key

For example,
STUDENT

id

STUDENT
MIDDLE_NAMES

id

middle-name

id

middle names

E-ER to Relational Mapping (Review contd.)


E-ER Model N-ary relationship Relational Model Relationship relation and n foreign keys
A

For example, pkA


R B

pkB

A r C

pkA pkB

pkC pkD
D

pkC

pkD

E-ER to Relational Mapping (contd.)


E-ER Model Subclass/superclass Relational Model Multi relation options: - Option 1 - Option 2 Single relation options: - Option 3 - Option 4

E-ER to Relational Mapping (contd.)


k
a1

a2
C
ISA

an

S1

S2

Sm

C is a super class with primary key, PK(C) = k Attributes of C = Attrs(C) = {k, a1, , an} S1, S2, , Sm are subclasses of C

E-ER to Relational Mapping (contd.)


Option 1:

Create a relation L for superclass C such that Attrs(L) = {k, a1, a2, , an} and PK(L) = k For each Si, create a relation Li such that Attrs(Li) = {k} U Attrs(Si) and PK(Li) = k

E-ER to Relational Mapping (contd.)

Relation L for class C Relation Li for each subclass Si Foreign key link from PK(Li) to PK(L) EQUIJOIN between Li and L to obtain all attributes of Li Option 1 works for all constraints disjoint, overlapping, total and partial.

E-ER to Relational Mapping (contd.)


Option 2:

For each subclass Si, create a new relation Li such that Attrs(Li) = Attrs(C) U Attrs(Li) and PK(Li) = k

E-ER to Relational Mapping (contd.)

The ISA relationship must be total (i.e. subclasses must cover the super class) If the subclasses are not disjoint, we keep multiple copies of Cs attributes values To obtain all entities in superclass C, we need to perform a full outer join with all relations Li

E-ER to Relational Mapping (contd.)


Option 3:

Create a single relation L with Attrs(L) = Attrs(C) U Attrs(S1) U U Attrs(Sm) U {t} such that PK(L) = k where t is type specifying which subclass the entity belongs if any. The specialization/generalization relationship must be disjoint

E-ER to Relational Mapping (contd.)

Good if subclasses have few attributes

No EQUIJOIN or OUTER JOIN operation needed If subclasses have a lot of attributes, potential for many null values

E-ER to Relational Mapping (contd.)


Option 4:

Create a single relation L with Attrs(L) = Attrs(C) U Attrs(S1) U U Attrs(Sm) U {t1, t2, , tm} such that PK(L) = k where ti is a Boolean attribute indicating whether each tuple belongs to subclass Si. This relation allows overlapping constraints for specialization/generalization relationship

E-ER to Relational Mapping (contd.)

When having multilevel specialization (or generalization) hierarchy, we do not need to follow the same mapping option. We can use different mapping options for different parts of the hierarchy

E-ER to Relational Mapping (contd.)

Aggregation Relationship has M:N cardinality


Entity A R1 Entity B

R2

Entity C B

R1

PKA
R2

PKA PKB
PKB PKc

PKB
C

PKA

PKC

E-ER to Relational Mapping (contd.)

Relationship has 1:M cardinality


R1 Entity B

Entity A

R2

Entity C B

PKA
R2

PKB PKA
C

PKB PKc

PKC

Summary

Relational Model Logical Database Design

Enhanced ER Model Relational Model

Practicals and Tutes will cover exercises in these topics

Das könnte Ihnen auch gefallen