CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a...

Preview:

Citation preview

CS 3630 Database Design and Implementation

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

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

4

How Many Possible Relations/Mappings between A and B

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

R A B

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

6

Total or Partial

y = f(x) = x2

.1.22-24..

.1.44

4-416.

Total

Domain Real Numbers

Range Real Numbers

Partial

7

Total or Partial

y = f(x) = x2

.1.22-24..

.1.44

4

16.

Total

Domain Real Numbers

RangeNon-negative Real Numbers

Total

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?

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!

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.

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}

12

Student Taking Course Student Course

S9

S2

S4

S5

C1

C2

C3

C5

Partial or Total?Partial or Total?

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!

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

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)}

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

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

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.

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.

20

Database Schema

Collection of Relation Schemas

Database definition

21

Database Instance

Relation instances (Tables)

Actual records

Store Data

Update Data

Retrieve Data

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

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

Chapter 3

The Relational Model

3.2 Terminology

24

Recommended