Want to consider: foundations of database operations
Types of Operations
Query: mapping of a database into a relation
Update: mapping between databases
Origin of Operations
From set theory
Specifically developed for relational databases
The Approach
Relational algebra:
A procedural language
Based on algebraic concepts
Algebra:
Studies quantities
Refers to relations and properties by symbols
4.2 Set Theory Operations
Union ()
Intersection ()
Difference ()
Defined only for relations over identical attributes
Student
Name
Number
Ben
3412
Dan
1234
Nel
2341
Instructor
Name
Number
Don
3412
Nel
2341
Administrator
Name
Num
Don
3412
Ron
4123
StudentInstructor
Name
Number
Ben
3412
Dan
1234
Don
3412
Nel
2341
StudentInstructor
Name
Number
Nel
2341
StudentInstructor
Name
Number
Ben
3412
Dan
1234
The above expressions are undefined, if ‘Administrator’ replaces ‘Instructor’
4.3 Renaming ()
The relation name from
to :
The attributes to ,...,:
Both:
Use
Facilitates set operations.
Name
Number
Don
3412
Ron
4123
Student
Name
Number
Ben
3412
Dan
1234
Nel
2341
Don
3412
Ron
4123
4.4 Assignments ()
The renaming operation under alternative notation
Student
vs.
Student
Temp
4.5 Selection
The condition must be a propositional formula
Clauses
of the operations
on attributes
Boolean operations
AND, OR, NOT
Student
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Sex
Dan
1234
M
An attribute with unordered domain allows only the comparison operations
.
4.6 Projection ()
The selection operation chooses tuples (rows of tables)
The projection operation chooses attributes (columns of tables)
Student
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Ben
3412
Dan
1234
Nel
2341
Identical tuples collapse into a single tuple
4.7 Cross Join ()
Also called Cartesian product and cross product. Combines the attributes of the given relations, in all possible
ways.
Instructor
Name
Number
Don
3412
Nel
2341
Administrator
Adm
Num
Dean
3412
Secretary
4123
InstructorAdministrator
Name
Number
Adm
Num
Don
3412
Dean
3412
Don
3412
Secretary
4123
Nel
2341
Dean
3412
Nel
2341
Secretary
4123
Application
Combine related information with the cross join operation
InstructorAdministrator
Name
Number
Adm
Num
Don
3412
Dean
3412
Don
3412
Secretary
4123
Nel
2341
Dean
3412
Nel
2341
Secretary
4123
Extract tuples with the selection operation
(InstructorAdministrator)
Name
Number
Adm
Num
Don
3412
Dean
3412
Weed out attributes with the projection operation
Name
Number
Adm
Don
3412
Dean
4.8 Join ()
Instructor
Name
Number
Don
3412
Nel
2341
Administrator
Adm
Num
Dean
3412
Secretary
4123
InstructorAdministrator
Name
Number
Adm
Num
Don
3412
Dean
3412
Cross product operation restricted to combinations which satisfy the stated conditions
Cross product includes all possible combinations of tuples
Join = cross product + selection
Called equi-join if only relational operations ‘=’ are in use.
Application
Variant of the previous example:
Temp Instructor
Administrator
Result
4.9 Natural Join (RS)
Natural-join = equi-join + projection
The join checks for equality of values of common attributes
The projection collapses together the common attributes
Instructor
Name
Number
Don
3412
Nel
2341
Administrator
Adm
Number
Dean
3412
Secretary
4123
InstructorAdministrator
Name
Number
Adm
Don
3412
Dean
When the operands have no attribute in common, the natural join operations reduces to the join cross (i.e.,
Cartesian product) operation
4.10 Outer Joins
A join operation is complete, if all the tuples of the operands contribute to the result.
Tuples not participating in the result are said to be dangling
Outer join operations are variants of the join operations in which the dangling tuples are appended with NULL
fields. They can be categorized into left, right, and full outer joins.
Instructor⟗ Administrator
Name
Number
Adm
Don
3412
Dean
Nel
2341
NULL
NULL
4123
Secretary
Instructor⟕ Administrator
Name
Number
Adm
Don
3412
Dean
Nel
2341
NULL
4.11 Division ()
equals
maximal which
satisfies
R
A
B
a
1
a
2
a
3
b
2
b
3
b
4
c
1
c
2
S1
B
1
2
RS1
A
a
c
Visited
Name
City
Ben
Columbus
Dan
Cincinnati
Dan
Columbus
Nel
Cleveland
Nel
Columbus
city visited by every one?
4.12 A Complete Set of Operations
A subset of the relational algebra operations is complete, if it can express any relational algebra operation
is
complete for relational algebra
The extra operations are provided for convenience
The outer join operations, and the following operations, can’t be expressed in terms of the basic relational algebra
operations
Example:
4.13 Global Aggregate Functions
Aggregate Functions ()
Mathematical functions on collections of values from the database: SUM, AVERAGE, MAXIMUM,
COUNT
Student
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Count
3
Grouping ()
of tuples in a relation by the value of some of their attributes