Upload
fouad-abatouy
View
254
Download
0
Embed Size (px)
Citation preview
7/27/2019 Important Alg Rela
1/39
1
Relational Algebra & SQL
Lecture 5
7/27/2019 Important Alg Rela
2/39
2
Relational Algebra
The Relational Algebra is used to define the
ways in which relations (tables) can be
operated to manipulate their data.
It is used as the basis of SQL for relational
databases, and illustrates the basic operations
required of any DML.
This Algebra is composed of Unary operations
(involving a single table) and Binary
operations (involving multiple tables).
7/27/2019 Important Alg Rela
3/39
3
SQL
Structured Query Language (SQL)Standardised by ANSI
Supported by modern RDBMSs
Commands fall into three groups
Data Definition Language (DLL)
Create tables, etc
Data Manipulation Language (DML)
Retrieve and modify data Data Control Language
Control what users can dogrant and revoke privileges
7/27/2019 Important Alg Rela
4/39
4
Unary Operations
Selection
Projection
7/27/2019 Important Alg Rela
5/39
5
Selection
The selection or operation selects rows from a tablethat satisfy a condition:
< condition > < tablename >
Example:course = CMStudents
Students
stud# name course
100 Fred PH stud# name course
200 Dave CM 200 Dave CM
300 Bob CM 300 Bob CM
7/27/2019 Important Alg Rela
6/39
6
Projection
The projection or operation selects a list ofcolumns from a table.
< column list> < tablename >
Example: stud#, nameStudents
Students
stud# name course stud# name
100 Fred PH 100 Fred
200 Dave CM 200 Dave
300 Bob CM 300 Bob
7/27/2019 Important Alg Rela
7/39
7/27/2019 Important Alg Rela
8/39
8
Binary Operations
Cartesian ProductTheta Join
Inner Join
Natural Join
Outer Joins
Semi Joins
7/27/2019 Important Alg Rela
9/39
9
Cartesian Product
Concatenation of every row in the first
relation (R) with every row in the second
relation (S):
R X S
7/27/2019 Important Alg Rela
10/39
10
Cartesian Product - Example
Students Coursesstud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
300 Bob CM
Students XCourses =
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
100 Fred PH CM Computing
200 Dave CM PH Pharmacy
200 Dave CM CM Computing
300 Bob CM PH Pharmacy
300 Bob CM CM Computing
7/27/2019 Important Alg Rela
11/39
11
Theta Join
A Cartesian product with a condition applied:
R S
7/27/2019 Important Alg Rela
12/39
7/27/2019 Important Alg Rela
13/39
13
Inner Join (Equijoin)
A Theta join where the is the
match (=) of the primary and foreign keys.
R S
7/27/2019 Important Alg Rela
14/39
14
Inner Join - Example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
300 Bob CM
Students course = course# Courses
stud# Students.name course course# Courses.name100 Fred PH PH Pharmacy
200 Dave CM CM Computing
300 Bob CM CM Computing
7/27/2019 Important Alg Rela
15/39
7/27/2019 Important Alg Rela
16/39
16
Natural Join - Example
Students Coursesstud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
300 Bob CM
R1=Students CoursesR2= R1
stud# Students.name course Courses.name100 Fred PH Pharmacy
200 Dave CM Computing
300 Bob CM Computing
7/27/2019 Important Alg Rela
17/39
17
Outer Joins
Inner join + rows of one table which do not satisfythe .
Left Outer Join: R SAll rows from R are retained and unmatched rows of S are
padded with NULL
Right Outer Join: R SAll rows from S are retained and unmatched rows of R are
padded with NULL
7/27/2019 Important Alg Rela
18/39
18
Left Outer Join - Example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students Courses
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy200 Dave CM CM Computing
400 Peter EN NULL NULL
7/27/2019 Important Alg Rela
19/39
19
Right Outer Join - Example
Students Courses
stud# name course course# name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
400 Peter EN CH Chemistry
Students Courses
stud# Students.name course course# Courses.name
100 Fred PH PH Pharmacy
200 Dave CM CM Computing
NULL NULL NULL CH Chemistry
7/27/2019 Important Alg Rela
20/39
20
Combination of Unary and Join Operations
Students Courses
stud# name address course course# name
100 Fred Aberdeen PH PH Pharmacy
200 Dave Dundee CM CM Computing
300 Bob Aberdeen CM
Show the names of students (from Aberdeen) and the names of their courses
R1= Students CoursesR2= R1
R3=
R2 Students.name Courses.nameFred Pharmacy
Bob Computing
7/27/2019 Important Alg Rela
21/39
21
Set Operations
Union
IntersectionDifference
7/27/2019 Important Alg Rela
22/39
7/27/2019 Important Alg Rela
23/39
23
Intersection
Takes the set of rows that are common to eachrelation
Participating relations must be compatible
R S R S
A B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a2 b2
7/27/2019 Important Alg Rela
24/39
24
Difference
Takes the set of rows in the first relation but notthe second
Participating relations must be compatible
R S R - S
A B
a1 b1
a2 b2
A B
a2 b2
a3 b3
A B
a1 b1
7/27/2019 Important Alg Rela
25/39
7/27/2019 Important Alg Rela
26/39
7/27/2019 Important Alg Rela
27/39
27
Unary Operations
Selection
course = ComputingStudents
In SQL:
Select *
From StudentsWhere course = Computing;
Projectionstud#, nameStudents
In SQL:
Select stud#, name
From Students;
Selection & Projection
stud#, name(course = ComputingStudents)In SQL:
Select stud#, name
From students
Where course = Computing;
7/27/2019 Important Alg Rela
28/39
7/27/2019 Important Alg Rela
29/39
7/27/2019 Important Alg Rela
30/39
30
Outer Joins
Left Outer Join
Students Courses
In SQL:
Select *
From Students, Courses
Where course = course#(+)
Right Outer Join
Students Courses
In SQL:Select *
From Students, Courses
Where course(+) = course#
7/27/2019 Important Alg Rela
31/39
31
Combination of Unary and Join Operations
R1= Students
CoursesR2= R1
R3= R2
In SQL:
Select Students.name, Courses.name
From Students, Courses
Where course=course#
AND address=Aberdeen;
7/27/2019 Important Alg Rela
32/39
32
Set Operations
Union: RSIn SQL:
Select * From R
UnionSelect * From S;
Intersection: R SIn SQL:
Select * From R
IntersectSelect * From S;
Difference: R- S
In SQL:
Select * From R
Minus
Select * From S;
7/27/2019 Important Alg Rela
33/39
7/27/2019 Important Alg Rela
34/39
7/27/2019 Important Alg Rela
35/39
35
SQL Operators
SELECT Catno
FROM Loan
WHERE Date-Returned IS NULL;
SELECT Catno
FROM Loan
WHERE Date-Returned IS NOT NULL;
7/27/2019 Important Alg Rela
36/39
36
SQL Operators
SELECT Name
FROM Member
WHERE memno IN (100, 200, 300, 400);
SELECT Name
FROM MemberWHERE memnoNOTIN (100, 200, 300, 400);
7/27/2019 Important Alg Rela
37/39
7/27/2019 Important Alg Rela
38/39
7/27/2019 Important Alg Rela
39/39