relational dbms

17
2014/1/27 1 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc. Copyright © 2004 Ramez Elmasri and Shamkant Navathe Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-2 Relational Model Concepts The relational model of data is based on the concept of a Relation. A Relation is a mathematical concept based on the ideas of sets. The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

Upload: rcu

Post on 23-Jan-2023

1 views

Category:

Documents


0 download

TRANSCRIPT

2014/1/27

1

The Relational Data Model and

Relational Database Constraints

Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-2

Relational Model Concepts

The relational model of data is based on the concept of a Relation.

A Relation is a mathematical concept based on the ideas of sets.

The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

2014/1/27

2

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-3

Relational Model Concepts (Cont)

The model was first proposed by Dr. E.F. Codd of

IBM in 1970 in the following paper:

A Relational Model for Large Shared Data Banks,

Communications of the ACM, June 1970.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-4

An Example Relation: STUDENT

2014/1/27

3

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-5

Informal Definitions

RELATION: A table of values

– A relation may be thought of as a set of rows.

– A relation may alternately be thought of as a set of columns.

– Each row represents a fact that corresponds to a real-world entity or relationship.

– Each row has a value of an item or set of items that uniquely identifies that row in the table.

– Sometimes row-ids or sequential numbers are assigned to identify the rows in the table.

– Each column typically is called by its column name or column header or attribute name.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-6

Formal Definitions

Relation Schema: R (A1, A2, ......, An)

R is defined over attributes A1, A2, ......, An

A tuple is an ordered set of values

Each value is derived from an appropriate domain.

A relation may be regarded as a set of tuples (rows).

Columns in a table are also called attributes of the relation.

2014/1/27

4

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-7

Formal Definitions (Cont)

A domain has a logical definition.

– phone-number: the set of 10 digit phones numbers valid in

US.

A domain may have a data-type or a format defined

for it.

– phone-number: (ddd)-ddd-dddd, where each d is a decimal

digit.

An attribute decides the role played by the domain.

– phone-number: it is used to define attributes ‘HomePhone’

and ‘OfficePhone’ for the relation ‘EMPLOYEE’.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-8

Formal Definitions (Cont)

The relation is formed over the Cartesian product of the sets; each set has values from a domain; that domain is used in a specific role which is conveyed by the attribute name.

R: schema of the relation

r(R): a specific "value" or population of R (relation state).

R is also called the intension of a relation

r is also called the extension of a relation

Given R(A1, A2, .........., An),

r(R) dom (A1) × dom (A2) × ..., × dom(An)

2014/1/27

5

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-9

Definition Summary

Informal Terms Formal Terms

Table Relation

Column Attribute

Row Tuple

Values in a column Domain

Table Definition Schema of a Relation

Populated Table Extension

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-10

Characteristics of Relations

Ordering of tuples in a relation r(R): The tuples are not considered to be ordered, even though they appear to be in the tabular form.

Ordering of attributes in a relation schema R (and of values within each tuple): We will consider the attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> to be ordered.

– A more general alternative definition t=<(A1,v1), (A2,v2), ..., (An,vn)> of relation does not require this ordering.

Values in a tuple: All values are considered atomic (indivisible). A special NULL value is used to represent values that are unknown or inapplicable to certain tuples.

2014/1/27

6

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-11

Relational Database and

Relational Database Schemas

Relational Database Schema: A set of

relation schemas that belong to the same

database. S = {R1, R2, ..., Rn}

Relational Database State: A set of relation

states DB={r1, r2, …, rm} such that each ri is a

state of Ri and satisfies the integrity

constraints. (valid / invalid)

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-12

COMPANY Database Schema

2014/1/27

7

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-13

Relational Integrity Constraints

Constraints are conditions that must hold on

all valid relation instances:

1. Domain constraints

2. Key constraints

3. Entity integrity constraints

4. Referential integrity constraints

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-14

Domain Constraints

Domain constraints specify that within each

tuple, the value of each attribute A must be

an atomic value from the domain dom(A).

2014/1/27

8

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-15

Key Constraints

Superkey of R: A set of attributes S such that no two tuples in any valid relation instance r(R) will have the same value for S.

Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey.

If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-16

Entity Integrity

Entity Integrity: The primary key attributes

PK of each relation schema R cannot have

NULL values in any tuple of r(R).

Other attributes of R may be similarly

constrained to disallow null values, even

though they are not members of the primary

key.

2014/1/27

9

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-17

Referential Integrity

Used to specify a relationship among tuples in two relations: the referencing relation and the referenced relation.

Tuples in the referencing relation R1 have attributes FK (called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2.

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2.PK.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-18

Referential integrity constraints on

COMPANY

2014/1/27

10

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-19

Referential Integrity (Cont)

The value in the FK column(s) of the

referencing relation R1 can be either:

– a value of an existing value of the corresponding

PK in the referenced relation R2,, or

– NULL (the FK in R1 should not be a part of its

own primary key).

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-20

Other Types of Constraints

Semantic Integrity Constraints:

- Based on application semantics.

- A constraint specification language may have to be used to express these (SQL-99 allows TRIGGERS and ASSERTIONS).

Functional Dependency Constraints:

- The value X determines the value of Y.

Transition Constraints:

- Deals with state changes

2014/1/27

11

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-21

Update Operations on Relations

INSERT a tuple.

DELETE a tuple.

MODIFY a tuple.

Integrity constraints should not be violated by the update operations.

Several update operations may have to be grouped together.

Updates may propagate to cause other updates automatically.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 5-22

Update Operations on Relations (Cont)

In case of integrity violation, several actions

can be taken:

– Cancel the operation that causes the violation

(REJECT option)

– Perform the operation but inform the user of the

violation

– Trigger additional updates so the violation is

corrected (CASCADE option, SET NULL option)

– Execute a user-specified error-correction routine

2014/1/27

12

Relational Database Design by

ER-to-Relational Mapping

Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-24

Outline

ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types

Step 2: Mapping of Weak Entity Types

Step 3: Mapping of Binary 1:1 Relation Types

Step 4: Mapping of Binary 1:N Relationship Types.

Step 5: Mapping of Binary M:N Relationship Types.

Step 6: Mapping of Multivalued attributes.

Step 7: Mapping of N-ary Relationship Types.

2014/1/27

13

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-25

ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types

– For each regular (strong) entity type E in the ER schema,

create a relation R that includes all the simple attributes

of E.

– Choose one of the key attributes of E as the primary key

for R. If the chosen key of E is composite, the set of

simple attributes that form it will together form the

primary key of R.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-26

ER-to-Relational Mapping Algorithm (cont)

Step 2: Mapping of Weak Entity Types

– For each weak entity type W with owner entity type E, create a relation R and include all simple attributes (or simple components of composite attributes) of W as attributes of R.

– In addition, include as foreign key attributes of R the primary key attribute(s) of the relation(s) that correspond to the owner entity type(s).

– The primary key of R is the combination of the primary key(s) of E and the partial key of W.

2014/1/27

14

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-27

ER-to-Relational Mapping Algorithm (cont)

Step 3: Mapping of Binary 1:1 Relation Types

Identify the relations S and T that correspond to the entity types participating in R. There are three possible approaches:

(1) Foreign Key: Choose one of the relations S, and include a foreign key in S the primary key of T.

(2) Merged relation: Merge the two entity types and the relationship into a single relation.

(3) Cross-reference or relationship relation: Set up a third relation R for the purpose of cross-referencing the primary keys of the two relations S and T representing the entity types.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-28

ER-to-Relational Mapping Algorithm (cont)

Step 4: Mapping of Binary 1:N Relationship Types

– Identify the relation S that represent the participating entity type at the N-side of the relationship type.

– Include as foreign key in S the primary key of the relation T.

– Include any simple attributes of the 1:N relation type as attributes of S.

2014/1/27

15

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-29

ER-to-Relational Mapping Algorithm (cont)

Step 5: Mapping of Binary M:N Relationship Types

– For each regular binary M:N relationship type R, create a new relation S to represent R.

– Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types; their combination will form the primary key of S.

– Also include any simple attributes of the M:N relationship type (or simple components of composite attributes) as attributes of S.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-30

ER-to-Relational Mapping Algorithm (cont)

Step 6: Mapping of Multivalued attributes

– For each multivalued attribute A, create a new relation R.

– R will include an attribute corresponding to A, plus the primary key attribute K (as a foreign key in R) of the relation that represents the entity type of relationship type that has A as an attribute.

– The primary key of R is the combination of A and K. If the multivalued attribute is composite, we include its simple components.

2014/1/27

16

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-31

ER-to-Relational Mapping Algorithm (cont)

Step 7: Mapping of N-ary Relationship Types

– For each n-ary relationship type R, where n>2, create a new relationship S to represent R.

– Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.

– Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S.

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-32

Summary of Mapping constructs and

constraints

ER Model Relational Model

Entity type “Entity” relation

1:1 or 1:N relationship type Foreign key (or “relationship” relation)

M:N relationship type “Relationship” relation and two foreign keys

n-ary relationship type “Relationship” relation and n foreign keys

Simple attribute Attribute

Composite attribute Set of simple component attributes

Multivalued attribute Relation and foreign key

Key attribute Primary key

2014/1/27

17

Copyright © 2004 Ramez Elmasri and Shamkant Navathe

Elmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Chapter 7-33