Upload
buck-simmons
View
244
Download
1
Embed Size (px)
Citation preview
Advanced Database System
1
Chapter 1
Advanced Relational Algebra & SQL (Part1 )
Department of Computer Science, Umm Al-Qura University University year: 1435/1436
2
Outline
Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary Relational Operations Additional Relational Operations Examples of Queries in Relational Algebra
Relational Calculus Tuple Relational Calculus Domain Relational Calculus
Advanced Database System
Advanced Database System
3
Relational Algebra Overview
Relational algebra is the basic set of operations for the relational model
These operations enable a user to specify basic retrieval requests (or queries)
The result of an operation is a new relation, which may have been formed from one or more input relationsThis property makes the algebra “closed” (all objects in relational algebra are relations)
Advanced Database System
4
Relational Algebra Overview (continued)
The algebra operations thus produce new relations These can be further manipulated using
operations of the same algebra A sequence of relational algebra
operations forms a relational algebra expression The result of a relational algebra expression
is also a relation that represents the result of a database query (or retrieval request)
Advanced Database System
5
Operators
Basic operations: Selection ( (sigma)) Selects a subset of rows from relation. Projection ( (pi)) Deletes unwanted columns from relation. Cross-product ( (Cartesian product)) Allows us to combine
two relations. Set-difference ( (minus)) Tuples in relation.1, but not in
relation.2 Union ( ) Tuples in relation.1 and in relation. 2.
Additional operations: Intersection ( ) Join ( ) division, renaming
Since each operation returns a relation, operations can be composed.
Advanced Database System
6
Unary Relational Operations: SELECT
In general, the select operation is
denoted by <selection condition>(R) where the symbol (sigma) is used to denote the
select operator the selection condition is a Boolean
(conditional) expression specified on the attributes of relation R
tuples that make the condition true are selected
appear in the result of the operation tuples that make the condition false are
filtered out discarded from the result of the operation
Advanced Database System
7
Unary Relational Operations: SELECT
The SELECT operation (denoted by ) is used to select a subset of the tuples from a relation based on a selection condition.
Examples: Select the EMPLOYEE tuples whose department
number is 4: DNO = 4 (EMPLOYEE)
Select the employee tuples whose salary is greater than $30,000:
SALARY > 30,000 (EMPLOYEE)
Advanced Database System
8
Unary Relational Operations: PROJECT
The general form of the project operation is:
<attribute list>(R) (pi) is the symbol used to represent the
project operation <attribute list> is the desired list of
attributes from relation R. The project operation removes any
duplicate tuples This is because the result of the project
operation must be a set of tuples Mathematical sets do not allow duplicate
elements.
Advanced Database System
9
Unary Relational Operations: PROJECT (cont)
PROJECT Operation is denoted by (pi) This operation keeps certain columns
(attributes) from a relation and discards the other columns. PROJECT creates a vertical partitioning
The list of specified columns (attributes) is kept in each tuple
The other attributes in each tuple are discarded Example: To list each employee’s first and
last name and salary, the following is used:LNAME, FNAME,SALARY(EMPLOYEE
Advanced Database System
11
Binary Relational Operations: JOIN
JOIN Operation (denoted by ) The sequence of CARTESIAN PRODECT followed by
SELECT is used quite commonly to identify and select related tuples from two relations
A special operation, called JOIN combines this sequence into a single operation
This operation is very important for any relational database with more than a single relation, because it allows us combine related tuples from various relations
The general form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R <join condition>S where R and S can be any relations that result from
general relational algebra expressions.
Advanced Database System
12
Binary Relational Operations: JOIN (cont.)
Example: Suppose that we want to retrieve the name of the manager of each department. To get the manager’s name, we need to combine
each DEPARTMENT tuple with the EMPLOYEE tuple whose SSN value matches the MGRSSN value in the department tuple.
We do this by using the join operation.
DEPT_MGR DEPARTMENT MGRSSN=SSN EMPLOYEE MGRSSN=SSN is the join condition
Combines each department record with the employee who manages the department
The join condition can also be specified as DEPARTMENT.MGRSSN= EMPLOYEE.SSN
Advanced Database System
14
Natural Join Operator
Natural join is a dyadic operator that is written as R S where R and S are relations.
The result of the natural join is the set of all combinations of tuples in R and S that are equal on their common attribute names.
Advanced Database System
15
Natural Join Example
For an example, consider the tables Employee and Dept and their natural join:
16
Renaming
In relational algebra, a rename is a unary operation written as ρ a / b (R) where: a and b are attribute names R is a relation
Examples:1) ρemployee(Emp): Changes the name of Emp table to
employee.2) Renaming and Union
Advanced Database System
Advanced Database System
17
Cartesian Product
In mathematics, it is a set of all pairs of elements (x, y) such that x belongs to X and y to Y.
It defines a relation that is the concatenation of every tuple of relation R with every tuple of relation S.
Example:
Advanced Database System
18
Theta (θ) join
Theta joins combines tuples from different relations provided they satisfy the theta condition.
A Cartesian product with a condition applied: R1 ⋈θ R2 Example:
Advanced Database System
19
Semi join Operator
The semi join is joining similar to the natural join and written as R S where R and S are relations.
The result of the semi join is only the set of all tuples in R for which there is a tuple in S that is equal on their common attribute names.
Example: consider the tables Employee and Dept and their semi join:
Advanced Database System
20
Outer join Operator
Left outer joinR <R.primary_key = S.foreign_key> S
All rows from R are retained and unmatched rows of S are padded with NULL
Right outer joinR <R.primary_key = S.foreign_key> S All rows from S are retained and unmatched rows of R are padded with NULL
Advanced Database System
21
Right Outerjoin Example
For an example consider the tables Students and Courses and their right outer join:
Advanced Database System
22
Left Outer join Example
For an example consider the tables Students and Courses and their left outer join:
Advanced Database System
23
Union
Takes the set of rows in each table and combines them, eliminating duplicates
Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types
Advanced Database System
24
Set Operations-Union
Takes the set of rows in each table and combines them, eliminating duplicates
Participating relations must be compatible, ie have the same number of columns, and the same column names, domains, and data types
A B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a1 b1
a2 b2
a3 b3
R SR ᴜ S
Advanced Database System
25
Intersection
Takes the set of rows that are common to each relation
Participating relations must be compatible
A B
a1 b1
a2 b2
R
A B
a2 b2
a3 b3
S
A B
a2 b2
R ∩ S
Advanced Database System
26
Difference
Takes the set of rows in the first relation but not the second
Participating relations must be compatible
A B
a1 b1
a2 b2
R
A B
a2 b2
a3 b3
S
A B
a1 b1
R - S
Advanced Database System
27
Set vs Bag semantic
A bag (or multiset ) is like a set, but an element may appear more than once.
Example1: {1,2,1,3} is a bag. Example2: {1,2,3} is also a bag that happens to
be a set. The multi-set {1, 2, 1, 3} is different to multi-set
{1, 2, 3}.