cs 3630 database design and implementation. 2 mathematical relation a mathematical relation is a...

24
CS 3630 Database Design and Implementation

Upload: roy-barrett

Post on 12-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

CS 3630 Database Design and Implementation

Page 2: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

2

Mathematical Relation

A mathematical relation is a subset of a Cartesian Product.

A1 A2 A3 … An = {(x1, x2, x3, …, xn): xi Ai}

R A1 A2 A3 … An

Page 3: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

3

Mapping and Relation

R1 = {(1, 2), (1, 5) , (2, 5)}

= {(x, y) | x is in A, y is in B, x < y and x <= 2}

A relationship between A and B

1

2

3

4

2

5

Page 4: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

4

How Many Possible Relations/Mappings between A and B

A = {1, 2, 3, 4}B = {2, 5}

R A B

Page 5: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

5

Functions

A function is a mapping

y = f(x)

x in a set Domain

y in a set Range

Mapping from Domain to Range

Page 6: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

6

Total or Partial

y = f(x) = x2

.1.22-24..

.1.44

4-416.

Total

Domain Real Numbers

Range Real Numbers

Partial

Page 7: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

7

Total or Partial

y = f(x) = x2

.1.22-24..

.1.44

4

16.

Total

Domain Real Numbers

RangeNon-negative Real Numbers

Total

Page 8: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

8

Functions

A function is a mapping

y = f(x)

x in a set Domain

y in a set Range

One-to-one?

One-to-Many?

Many-to-one?

Page 9: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

9

Functions

y = f(x)

For the same value of x, the value of y always the same.

y1 = f(3)

u = 2

y2 = f(u + 1)

Both y1 and y2 must have the same value!

It CANNOT be one-to-many!

Page 10: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

10

Functions

But for different values of x, the value of y could be the same.

y = f(x) = x2

f(2) = f(-2)

It’s not one-to-one!

It could be many-to-one.

Page 11: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

11

Relation Example

Student = {s: s is a student registered at UWP

this semester}

Course = {c: c is a course offered at UWP this

semester}

Page 12: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

12

Student Taking Course Student Course

S9

S2

S4

S5

C1

C2

C3

C5

Partial or Total?Partial or Total?

Page 13: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

13

ExampleS = {s | s is a student registered at UWP this semester}C = {c | c is a course offered at UWP this semester}

S C: All possible registrations

Registration = {(s, c): (s, c) S C and s is taking c}

Partial for both S and C

Many-to-many

It can be updated!

Page 14: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

14

TerminologyA = {1, 2, 3, 4}B = {2, 5}

R1 = {(1, 2), (1, 5), (2, 5)} R1 A B

Tuple: (1, 2), (1, 5) and (2, 5)Cardinality: 3Domain: A for the 1st component B for the 2nd component

Page 15: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

15

Using Tables for Relations

Att1 Att2 Att3

1 5 x

5 10 w

4 2 x

3 5 y

R = {(1, 5, x), (4, 2, x), (3, 5, y), (5, 10, w)}

Page 16: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

16

Database Relations (Tables)

Relation (Table)

Attribute (Field, column)

Tuple (Record, row)

Domain for each field

Degree (number of fields): 3

Cardinality (number of records): 4

Att1 Att2 Att3

1 5 x

5 10 w

4 2 x

3 5 y

Page 17: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

17

Table Instance

A subset of the Cartesian Product No duplicatesUpdate Tuples can be modified New tuples can be inserted Old tuples can be deletedDifferent instance after update

Student Course

S2 C2

S3 C2

S2 C5

Page 18: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

18

Relation Schema

A named relation defined by a set of attribute and domain pairs.

R (Att1:A1, Att2:A2, Att3:A3)

R (Att1, Att2, Att3)Domain: Att1: A1 Att2: A2 Att3: A3

The relation schema represents the Cartesian Product A1 A2 A3

Same table schema, different table instances.

Page 19: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

19

Relational Model

• What is a Relational Database?

A collection of relations (tables)

• Relational Database:

A collection of normalized relations (tables) with distinct relation/table names.

Page 20: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

20

Database Schema

Collection of Relation Schemas

Database definition

Page 21: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

21

Database Instance

Relation instances (Tables)

Actual records

Store Data

Update Data

Retrieve Data

Page 22: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

22

Properties of Relations

• Each relation has a distinct name (within the database)

• Each attribute has a distinct name (within the relation)

• Each tuple is distinct• Each cell contains exactly one atomic

(single) value• The order of attributes has no significance• The order of tuples has no significance

Page 23: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

23

Database UWP

Student(ID, Name, Phone, Address…)Course(ID, Title, Department, Credits…)Faculty(ID, Name, Department, Phone, Address…)Registration (Student, Course) Other possible fields?Schedule (Faculty, Course) Other possible fields?Other tables

Page 24: CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An

Chapter 3

The Relational Model

3.2 Terminology

24