21
1 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

1 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

Embed Size (px)

Citation preview

1

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

2

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

3

How Many Possible Relations/Mappings between A and B

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

R A B

4

Functions

Function is a mapping, but not one-to-one!

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 have the same value!

It’s NOT one-to-many!

5

Functions (II)

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

y = f(x) = x2

f(2) = f(-2)

It could be many-to-one.

6

Functions (III) Many-to-One

Total / Partial (Total)

X1X2X3X4

A2A4A6A8

7

Relation Example

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

this semester}

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

semester}

8

Student Taking Course Student Course

S9

S2

S4

S5

C1

C2

C3

C5

Partial or Total?Partial or Total?

9

Student Taking Course Student Course

S9

S2

S4

S5

C1

C2

C3

C5

PartialPartial

10

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!

11

Terminology

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

12

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

13

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

14

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

15

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.

16

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.

17

Database Schema

Collection of Relation Schemas

Database definition

18

Database Instance

Relation instances (Tables)

Actual records

Store Data

Update Data

Retrieve Data

19

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

20

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

21