Relational Algebra The mathematical foundation for SQL Basic operators select project union ...
-
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 ...
Relational AlgebraRelational Algebra
The mathematical foundation for SQL
Basic operators select
project
union
intersect
set difference
cross product (i.e. Cartesian product)
rename
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: =, , >, . <.
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?
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
Question 2Question 2
Relation r: A B C
10
20
30
40
1
1
1
2
What is the result of A (r) ?
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)
Question 3Question 3
Relations r, s:
What is the result of r s?
A B
1
3
1
A B
1
3
rs
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)
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
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)
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
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.
Question 6Question 6
Relations r, s:
What is the result of r x s?
A B
1
2
C D
10102010
E
XXYYr
s
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.
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
16
Relation: StudentsRelation: Students
JonesSmithCurryLinda
Fugitive
sid
0102030405
sname
CACAORWARI
state
Students
columns
records
17
Relation : CoursesRelation : Courses
CS105CS440CS320CS480
cid
C++DatabasesIC Design
AI Research
cname
YesNoYesNo
required
courses
columns
records
18
Relation: EnrollmentRelation: Enrollment
CS105CS320CS440CS105CS440CS105CS320CS440CS480CS320CS440CS480
cid
010101020203030303040404
sid
555344133445
grade
enrollment
columns
records
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 ))
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) )
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
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)))
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