Relational Algebra The mathematical foundation for SQL Basic operators select project union ...

23
Relational Algebra Relational Algebra The mathematical foundation for SQL Basic operators select project union intersect set difference cross product (i.e. Cartesian product) rename
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Relational Algebra The mathematical foundation for SQL Basic operators select project union ...

Page 1: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Relational AlgebraRelational Algebra

The mathematical foundation for SQL

Basic operators select

project

union

intersect

set difference

cross product (i.e. Cartesian product)

rename

Page 2: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Select OperationSelect Operation

Notation: p(r)

Select only the records satisfying the condition p

The condition p is composed of

one or more terms connected by : (and), (or), (not)each term is one of the following two forms:

<column> op <column>

<column> op <constant>

where op is one of: =, , >, . <.

Page 3: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 1Question 1

• Relation r A B C D

1

5

12

23

7

7

3

10

C>D A= B (r)

What is the result of the following operation?

Page 4: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Project OperationProject Operation

Notation:

A1, A2, …, Ak (r)

where A1, A2 are column names and r is a relation name.

The result is defined as a new relation of k columns obtained by erasing the columns that are not listed

Duplicate rows removed from result

Page 5: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 2Question 2

Relation r: A B C

10

20

30

40

1

1

1

2

What is the result of A (r) ?

Page 6: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Union OperationUnion Operation

Notation: r s

Meaning: The set composed of records appearing in r or s or both.

r s is well defined

if and only if they are compatible in the following sense:

1. r, s have the same number of columns

2. The corresponding columns have same types

(e.g., i-th column of r deals with the same type of values

as does the i-th column of s)

Page 7: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 3Question 3

Relations r, s:

What is the result of r s?

A B

1

3

1

A B

1

3

rs

Page 8: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Set Difference OperationSet Difference Operation

Notation r – s

Meaning: The set composed of records appearing in r but not in s.

r s is well defined if and only if they are compatible

in the following sense:

1. r, s have the same number of columns

2. The corresponding columns have same types

(e.g., i-th column of r deals with the same type of values

as does the i-th column of s)

Page 9: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 4Question 4

Relations r, s:

What is the result of r – s?

What is the result of r – (r – s)?

A B

1

3

1

A B

1

3

rs

Page 10: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Set-Intersection OperationSet-Intersection Operation

Notation: r s

Meaning: The set composed of records appearing both in r or s.

r s is well defined

if and only if they are compatible in the following sense:

1. r, s have the same number of columns

2. The corresponding columns have same types

(e.g., i-th column of r deals with the same type of values

as does the i-th column of s)

Page 11: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Questions 5Questions 5

Relation r, s:

What is the result of r s?

What is the result of r – (r- s)?

What is the result of s – (s- r)?

Are the three expressions above always equivalent to one another no matter what r and s are?

A B

121

A B

23

r s

Page 12: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Cross-Product OperationCross-Product Operation

Notation r x s

Defined as:

r x s = {t q | t is a record in r and q is a record ins}

In other words, r x s

Is the set of all possible pairs of records with one from r and

the other one from s to form a pair.

Page 13: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 6Question 6

Relations r, s:

What is the result of r x s?

A B

1

2

C D

10102010

E

XXYYr

s

Page 14: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Rename OperationRename Operation

Allows us to name a resulting relation and its columns.

x (E)

returns the relation E under the name X

If a relation E has arity n, then

x (A1, A2, …, An) (E)

returns the result of expression E under the name X, and with the

columns renamed to A1, A2, …., An.

Page 15: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Composing Expressions in Relational Composing Expressions in Relational AlgebraAlgebra

A basic expression in the relational algebra consists of : Constants or A table as a relation in the database

With any relational-algebra expressions E1 and E2 ,you can recursively compose more complex relational-algebra expressions as follow:

p (E1), P is a logic condition on columns in E1

s(E1), S is a list consisting of some of the columns in E1

E1 x E2

x (E1), x is the new name for the result of E1

E1 E2

E1 E2

E1 - E2

Page 16: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

16

Relation: StudentsRelation: Students

JonesSmithCurryLinda

Fugitive

sid

0102030405

sname

CACAORWARI

state

Students

columns

records

Page 17: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

17

Relation : CoursesRelation : Courses

CS105CS440CS320CS480

cid

C++DatabasesIC Design

AI Research

cname

YesNoYesNo

required

courses

columns

records

Page 18: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

18

Relation: EnrollmentRelation: Enrollment

CS105CS320CS440CS105CS440CS105CS320CS440CS480CS320CS440CS480

cid

010101020203030303040404

sid

555344133445

grade

enrollment

columns

records

Page 19: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 7Question 7

What are the results of the following relational-algebra expressions with respect to the three relations: courses, enrollment, and students

1. sid (grade<2 (enrollment))

2. cname (required=“Yes” (courses) )

3. sid ( enrollment.cid = courses.cid and required=“Yes” (

enrollment x courses ))

Page 20: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 8Question 8

What are the results of the following relational-algebra expressions with respect to the three relation instances: courses, enrollment, and students

1. sid (enrollment.grade<2 and enrollment.cid = courses.cid and

enrollment.required=“Yes” (enrollment x courses ) )

2. sid ( (enrollment.cid = courses.cid ( grade<2

(enrollment) x required=“Yes” (courses) ) ))

3. sid ( grade<2 (enrollment)x required=“Yes” (courses) )

Page 21: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 9Question 9

Assume that a student fails in a course if the grade is less than 2.

Identify what expressions (if any) in questions 7 and 8 can provide the following information (given the relation instances: courses, enrollment, and students)

:

1. The list of sids of students who fail in some required courses

2. The list of sids of students who fail in some courses

3. The list of sids of students who take some required courses

Page 22: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 10Question 10

What are the results of the following relational-algebra expressions with respect to the three relation instances: courses, enrollment, and students

1. courses.cname ( enrollment.sid =“04”

( enrollment.cid = courses.cid(enrollment x courses)))

2. cname(courses) –

courses.cname ( enrollment.sid =“04” (

enrollment.cid = courses.cid(enrollment x courses)))

3. cname(required=“Yes” (courses)) –

courses.cname ( enrollment.sid =“04” and courses.required=“Yes”

( enrollment.cid = courses.cid(enrollment x courses)))

Page 23: Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.

Question 11Question 11

Assume that a student fails in a course if the grade is less than 2.

Identify what expressions (if any) in question 10 can provide the following information (given the relation instances: courses, enrollment, and students)

:

1. The list of courses taken by the student with the sid ’04’

2. The list of courses that the student with the sid ’04’ hasn’t taken yet

3. The list of required courses that the student with the sid ’04’ hasn’t taken yet