CS 377 Database Systems Relational Data Model Department of - - PowerPoint PPT Presentation

cs 377 database systems
SMART_READER_LITE
LIVE PREVIEW

CS 377 Database Systems Relational Data Model Department of - - PowerPoint PPT Presentation

CS 377 Database Systems Relational Data Model Department of Mathematics and Computer Science Emory University 1 Outline Relational Model Concepts Relational Model Constraints Relational Database and


slide-1
SLIDE 1

CS 377 Database Systems

1

Relational Data Model

  • Department of Mathematics and Computer Science

Emory University

slide-2
SLIDE 2

Outline

Relational Model Concepts Relational Model Constraints Relational Database and operations

2

slide-3
SLIDE 3
  • First formal database model

Introduced by Codd in "A Relational Model for

Large Shared Data Banks," Communications of the ACM, June 1970.

First commercial implementations available in

3

First commercial implementations available in

early 1980s

Based on the concept of a mathematical relation

and has theoretical basis in set theory and first/

  • rder predicate logic.

Other models: hierarchical model, network model

slide-4
SLIDE 4
  • RELATION: A table of values

A relation may be thought of as a . A relation may alternately be thought of as a . Each row represents a fact that corresponds to a real/world or

!.

Table name and column names help interpret the meaning of the values

4

slide-5
SLIDE 5

"

The table is called a relation, a row is a tuple, a column header is an attribute

  • Made up of a relation name R and a set of #

(or arity) of a relation is the number of attributes of its relational schema E.g. STUDENT (Name, SSN, HomePhone, Address, OfficePhone, Age, GPA)

Each # has a that defines the possible values of the

attribute by a data/type or a format

5

attribute by a data/type or a format

E.g. The domain of SSN is the set of 9 digit numbers defined as: ddd/dd/dddd

where each d is a decimal digit.

slide-6
SLIDE 6

"

A (or relation state) r of the relation schema

is a set of tuples r = {t1, t2, ..., tm}

A ! t is an ordered set of n values t =<v1, v2, ..., vn>, where

each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or a special NULL value

E.g. <“Benjamin Bayer", 305/61/2435, 373/1616, “2918 Bluebonnet Lane”, null,

19, 3.21> is a tuple belonging to the STUDENT relation.

6

19, 3.21> is a tuple belonging to the STUDENT relation.

slide-7
SLIDE 7

Mathematical Definitions

A relation () is a of degree on the domains

dom(1), dom(2), ..., dom(), which is a #of the $ !of the domains that define R:

The Cartesian product is the direct product of the sets of values of all

domains:

The total number of tuples in the Cartesian product is:

7

$ reflects only the valid tuples that represent a

particular state

slide-8
SLIDE 8

$%$$

Ordering of tuples in a relation r(R)

  • A relation is a set of tuples which are ordered

Ordering of attributes

  • The attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> are an

list in our definition

  • Alternative definition: tuple considered as a set of (<attribute>, <value>) pairs,

where each pair gives the value of the mapping from an attribute to a value from dom()

Values in a tuple

8

Values in a tuple

  • All values are considered (flat relational model with first normal form

assumption) – what about multi/valued attributes and composite attributes?

  • A special value is used to represent values that are unknown or inapplicable to

certain tuples.

slide-9
SLIDE 9

DEFINITION SUMMARY

Informal Terms Formal Terms Table Relation Column Attribute Row Tuple

9

Row Tuple Values in a column Domain Table Definition Relation Schema

slide-10
SLIDE 10

Relational Model Notation

Relation schema of degree : (, , ..., Relation names: , , Relations: , , Tuples: , , in a relation (): , , ..., is the

10

in a relation (): , , ..., is the

value corresponding to attribute

Component values of tuples:

[] and refer to the value in for attribute [, , ..., !] and (, , ..., !) refer to the subtuple

  • f values , , ..., !from corresponding to the

attributes specified in the list

slide-11
SLIDE 11

Outline

Relational Model Concepts Relational Model Constraints Relational Database and operations

11

slide-12
SLIDE 12

Relational Model Constraints

Constraints

Restrictions on the actual values in a database state

&# or !

Inherent in the data model E.g. no duplicate tuples

12

&# or '!

Can be directly expressed in schemas of the data model

!!&#or or #

  • Cannot be directly expressed in schemas, expressed and enforced by

application program

E.g. the max. no. of hours per employee for all projects he or she

works on is 56 hrs per week

slide-13
SLIDE 13

Schema/based constraints

Domain constraints Key constraints Entity integrity constraints Referential integrity constraints

13

Referential integrity constraints

slide-14
SLIDE 14

Domain Constraints

The value of each attribute A must be an atomic value

from the domain dom(A)

Typical data types associated with domains:

Numeric data types for integers and real numbers

Characters

14

Characters Booleans Fixed/length strings Variable/length strings Date, time, timestamp Money Other special data types

slide-15
SLIDE 15

Key Constraints

No two tuples can have the same combination of

values for all their attributes.

!(

No two distinct tuples in any state of can have the

15

No two distinct tuples in any state of can have the

same value for SK

)

Superkey of R Removing any attribute from " leaves a set of

attributes " that is not a superkey of any more

slide-16
SLIDE 16

Key Constraints and Constraints

  • n NULL Values (cont’d.)

Key satisfies two properties:

Two distinct tuples in any state of relation cannot have

identical values for (all) attributes in key

Minimal superkey

16

Cannot remove any attributes and still have uniqueness

constraint in above condition hold

slide-17
SLIDE 17

Key Constraints and Constraints

  • n NULL Values (cont’d.)

$(

Relation schema may have more than one key

*(of the relation

Designated among candidate keys

17

Designated among candidate keys Underline attribute

Other candidate keys are designated as +(

slide-18
SLIDE 18

Key Constraints and Constraints on NULL Values (cont’d.)

18

slide-19
SLIDE 19

)$

  • !( of R: A set of attributes SK of R such that no two tuples

# will have the same value for SK.

  • For any distinct tuples t1 and t2 in r(R), t1[SK] ≠

≠ ≠ ≠ t2[SK].

  • {Licence_number}, {License_number, Make}, {Engine_serial_number, Make}
  • ) 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.

  • Key1 = {License_number}, Key2 = {Engine_serieal_number}
  • Is {Engine_serial_number, Make} a key?

19

  • Is {Engine_serial_number, Make} a key?
  • If a relation has keys, each is called a (, and
  • ne is chosen arbitrarily to be the !(. The primary key

attributes are .

slide-20
SLIDE 20
  • : The #$#% PK of each relation

schema R cannot have null values in any tuple of r(R). t[PK] ≠ ≠ ≠ ≠ null for any tuple t in r(R)

Primary key values are used to &# the individual tuples. Note: Other attributes of R may be similarly constrained to disallow

null values, even though they are not members of the primary key.

20

null values, even though they are not members of the primary key.

slide-21
SLIDE 21
  • : a tuple in one relation that refers to another

relation must refer to an existing tuple in that relation.

Formally

A set of attributes FK in relation schema R1 is a ( of R1 that references relation R2 if:

FK have the same domains as the primary key attributes PK of R2

21

The value of FK in the current state of R1 can be either:

(1) a value of PK in the current state of R2: t1[FK] = t2[PK]. (2) a null

R1 is the and R2 is the . A tuple t1 in R1 is said to a tuple t2 in R2 if t1[FK] = t2[PK]. A constraint can be displayed in a relational

database schema as a directed arc from R1.FK to R2.

slide-22
SLIDE 22
  • .-

22

slide-23
SLIDE 23

23

slide-24
SLIDE 24

24

slide-25
SLIDE 25

25

slide-26
SLIDE 26

Outline

Relational Model Concepts Relational Model Constraints Relational Database and operations

26

slide-27
SLIDE 27

Relational Databases and Relational Database Schemas

#

Set of relation schemas {1, 2, ..., } Set of integrity constraints IC

#

Set of relation states '({ , , ..., }

27

Set of relation states '({, , ..., } Each is a state of such that the relation states satisfy integrity

constraints specified in IC

/

Does not obey all the integrity constraints Satisfies all the constraints in the defined set of integrity constraints

IC

slide-28
SLIDE 28

Operations in a Relational Database

Basic operations that change the states of relations in

the database:

Insert Delete

28

Update (or Modify)

slide-29
SLIDE 29

The Insert Operation

Provides a list of attribute values for a new tuple t

to be inserted into a relation R

Can violate any of the four types of constraints

Default option is to reject the insertion

29

slide-30
SLIDE 30

The Delete Operation

Can violate only referential integrity

If tuple being deleted is referenced by foreign keys from other tuples, e.g.

delete a tuple from department

Reject the deletion $Propagate the deletion by deleting tuples that reference the deleted

tuple

  • r Modify the referencing attribute values that cause the

violation

30

violation

slide-31
SLIDE 31

The Update Operation

Necessary to specify a condition on attributes of

relation

Select the tuple (or tuples) to be modified

If attributes to be updated not part of a primary key

31

If attributes to be updated not part of a primary key

nor of a foreign key

Usually causes no problems

Updating a primary/foreign key

Similar issues as with Insert/Delete

slide-32
SLIDE 32

In/Class Exercise

Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course: STUDENT(SSN, Name, Major, Bdate) COURSE(Course#, Cname, Dept)

32

COURSE(Course#, Cname, Dept) ENROLL(SSN, Course#, Quarter, Grade) BOOK_ADOPTION(Course#, Quarter, Book_ISBN) TEXT(Book_ISBN, Book_Title, Publisher, Author) ! ( .