1 2
It is thus possible to apply an operation to the result of the mathematics which underpin SQL operations
another operation The DBMS must take whatever SQL statements the
Two main collections of relational operators: user types in and translate them into relational algebra
operations before applying them to the database.
Set theory operations:
Union, Intersection, Difference and Cartesian product.
3 4
1 7 Defined as:
5 7
p(r) = {t | t r and p(t)}
12 3
Where p is a formula consisting of terms connected by :
23 10 (and), (or), (not)
Each term is one of:
<attribute> op <attribute> or <constant>
A=B ^ D > 5 (r)
A B C D where op is one of: =, , >, . <.
Example of selection:
branch-name=Perryridge(account)
1 7
23 10
5 6
1
Project Operation Example Project Operation
Relation r: A B C
Notation:
10 1
20 1 A1, A2, , Ak (r)
30 1 where A1, A2 are attribute names and r is a relation name.
40 2 The result is defined as the relation of k columns obtained
A,C (r) A C A C
by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
1 1
E.g. To eliminate the branch-name attribute of account
account-number, balance (account)
1 = 1
1 2
2
7 8
1 2
2 3
1 s
r
r s: A B
1
2
1
3
9 10
Notation: r s 1 2
Defined as: 2 3
r s = {t | t r or t s} 1 s
r
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column r s: A B
of r deals with the same type of values as does the 2nd
column of s) 1
E.g. to find all customers with either an account or a loan 1
customer-name (depositor) customer-name (borrower)
11 12
2
DIFFERENCE Example
Set Difference Operation
Notation r s
Defined as:
r s = {t | t r and t s}
Set differences must be taken between compatible
relations.
r and s must have the same arity
attribute domains of r and s must be compatible
13 14
Cartesian-Product Operation-Example
Cartesian-Product Operation
Relations r, s: A B C D E
1 10 a Notation r x s
2
10
20
a
b
Defined as:
r 10 b r x s = {t q | t r and q s}
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
15 16
17 18
3
For each student, identified by name and
student number, return the name of the tutor
and their office number
we have to combine tuples from Student and
Staff that satisfy Student.tutor=Staff.lecturer
and keep the attributes studno, name, tutor and
lecturer.
In relational algebra:
studno,name,lecturer,roomno(tutor=lecturer (Student Staff ) )
19 20
Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
A B C D E
rxs 1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
A B C D E
A=C(r x s)
1 10
20
a
2 a
2 20 b
21 22
23 24
4
INTERSECTION Example
Set-Intersection Operation - Example
A B A B
1 2
Relation r, s: 2 3
1
r s
A B
rs
25 26
B D E
Robinson 34
A B C D Smith 34
1 a 1 a
2 a 3 a
4 b 1 a Department Table
DepartmentID DepartmentName
1 a 2 b 31 Sales
2 b 3 b 33 Engineering
r s
34 Clerical
35 Marketing
r s
A B C D E DepartmentID Employee.LastName Department.DepartmentName
1 a
34 Smith Clerical
33 Jones Engineering
1 a
34 Robinson Clerical
1 a 33 Steinberg Engineering
1 a 31 Rafferty Sales
2 b
29 30
5
31 32
Division Operation
rs
Suited to queries that include the phrase for all.
Let r and s be relations on schemas R and S
respectively where
R = (A1, , Am, B1, , Bn)
S = (B1, , Bn)
The result of r s is a relation on schema
R S = (A1, , Am)
rs={t | t R-S(r) u s ( tu r ) }
33 34
r s: r s:
B is branch-name Students who have taken both "a
A r A B C and b courses, with instructor 1
1and 2 here show two specific branch-
names a
(Find students who have taken all
(Find customers who have an account in all a
branches of the bank) courses given by instructor 1)
35 B - tutor 36
6
Example Queries
Division Find all customers who have an account from at least the
Downtown and the Uptown branches.
Query 1
CN(BN=Downtown(depositor account))
CN(BN=Uptown(depositor account))
Query 2
customer-name, branch-name (depositor account)
temp(branch-name) ({(Downtown), (Uptown)})
37 38
39 40
Can delete only whole tuples; cannot delete values on loan loan amount 0and amount 50 (loan)
only particular attributes
A deletion is expressed in relational algebra by:
rrE
where r is a relation and E is a relational algebra query.
41 42
7
Insertion Insertion Examples
Insert information in the database specifying that Smith
has $1200 in account A-973 at the Perryridge branch.
To insert data into a relation, we either:
specify a tuple to be inserted account account {(Perryridge, A-973, 1200)}
write a query whose result is a set of tuples to be inserted depositor depositor {(Smith, A-973)}
43 44
Update Examples
Updating Make interest payments by increasing all balances by 5 percent.
A mechanism to change a value in a tuple without account AN, BN, BAL * 1.05 (account)
charging all values in the tuple
where AN, BN and BAL stand for account-number, branch-name
Use the generalized projection operator to do this task and balance, respectively.
r F1, F2, , FI, (r) Pay all accounts with balances over $10,000 6 percent interest
Each Fi is either and pay all others 5 percent
the ith attribute of r, if the ith attribute is not updated, or,
account AN, BN, BAL * 1.06 ( BAL 10000 (account))
if the attribute is to be updated Fi is an expression,
AN, BN, BAL * 1.05 (BAL 10000 (account))
involving only constants and the attributes of r, which gives
the new value for the attribute
45 46
47 48
8
Example Queries Example Queries
Find the names of all customers who have a loan at the
Find the names of all customers who have a loan, an Perryridge branch.
account, or both, from the bank
customer-name (branch-name=Perryridge
customer-name (borrower) customer-name (depositor)
(borrower.loan-number = loan.loan-number(borrower x loan)))
Find the names of all customers who have a loan and an Find the names of all customers who have a loan at the
account at bank. Perryridge branch but do not have an account at any branch of
the bank.
49 50
Example Queries
Find the names of all customers who have a loan at the
Perryridge branch. Consider the University db with the tables:
Query 1 student(studno,name, hons, tutor, year)
customer-name(branch-name = Perryridge ( staff(lecturer, roomno)
borrower.loan-number = loan.loan-number(borrower x loan))) enrolled(studno, courseno, labmark, exammark)
Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = Perryridge(loan)) x borrower))
51 52
Views
Write queries in relational algebra that return the following: In some cases, it is not desirable for all users to see the
The numbers of courses where a student had a better exam entire logical model (i.e., all the actual relations stored
mark than lab mark. in the database.)
The names of the lecturers who are tutoring a student who Consider a person who needs to know a customers loan
had an exam mark worse than the lab mark. number but has no need to see the loan amount. This
The names of the lecturers who are tutoring a 3rd year person should see a relation described, in the relational
student. algebra, by
The room numbers of the lecturers who are tutoring a 3rd customer-name, loan-number (borrower loan)
year student.
Any relation that is not of the conceptual model but is
The names of the lecturers who are tutoring more than one
made visible to a user as a virtual relation is called a
student
view.
The names of the lecturers who are tutoring no more than
one student
53 54
9
View Examples
View Definition Consider the view (named all-customer) consisting of
branches and their customers.
A view is defined using the create view statement which
has the form create view all-customer as
create view v as <query expression> branch-name, customer-name (depositor account)
branch-name, customer-name (borrower
where <query expression> is any legal relational algebra loan)
query expression. The view name is represented by v.
Once a view is defined, the view name can be used to We can find all customers of the Perryridge branch by writing:
57 58
59 60
10
Example Queries Example Queries
Find the names of all customers having a loan, an Find the names of all customers having a loan at the
account, or both at the bank Perryridge branch
{t | s borrower( t[customer-name] = s[customer-name]) {t | s borrower(t[customer-name] = s[customer-name]
u depositor( t[customer-name] = u[customer-name]) u loan(u[branch-name] = Perryridge
u[loan-number] = s[loan-number]))}
Find the names of all customers who have a loan and an account Find the names of all customers who have a loan at the
at the bank Perryridge branch, but no account at any branch of the bank
61 62
63 64
65 66
11
Example Queries Example Queries
Find the loan-number, branch-name, and amount for Find the names of all customers having a loan, an
loans of over $1200 account, or both at the Perryridge branch:
{ l, b, a | l, b, a loan a > 1200}
{ c | l ({ c, l borrower
b,a( l, b, a loan b = Perryridge))
Find the names of all customers who have a loan of over $1200 a( c, a depositor
{ c | l, b, a ( c, l borrower l, b, a loan a > 1200)} b,n( a, b, n account b = Perryridge))}
Find the names of all customers who have a loan from the Find the names of all customers who have an account at all
Perryridge branch and the loan amount: branches located in Brooklyn:
67 68
Safety of Expressions
{ x1, x2, , xn | P(x1, x2, , xn)}
12