slides

Theory, Practice & Methodology
of Relational Database
Design and Programming
Copyright © Ellis Cohen 2002-2007
Extended SQL
& The
Relational Calculus
These slides are licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 2.5 License.
For more information on how you may use them,
please see http://www.openlineconsult.com/db
1
Overview of Lecture
Extending SQL
with Existential Subqueries
Extending SQL
with Universal Subqueries
State Assertions
with Quantifier Subqueries
Extending SQL with Attribute
Filtering
The Tuple Relational Calculus
The Domain Relational Calculus
© Ellis Cohen 2001-2007
2
Extending SQL
with
Existential Subqueries
Useful, but non-existent
SQL constructs
based on the Relational Calculus
& First-Order Logic
© Ellis Cohen 2001-2007
3
SOME
List the names of employees
who are project managers
SELECT ename FROM Emps e
WHERE exists(
SELECT * FROM Projs p
WHERE p.pmgr = e.empno)
Wouldn't you rather write:
SELECT ename FROM Emps e WHERE
(SOME Projs p
SATISFIES p.pmgr = e.empno)
Select the name of every employee e where there is
some project p whose manger is e
© Ellis Cohen 2001-2007
4
SOME and ANY
List the names of employees
who are project managers
SELECT ename FROM Emps e WHERE
(SOME Projs p
SATISFIES p.pmgr = e.empno)
SELECT ename FROM Emps e WHERE
e.empno = ANY (
SELECT p.pmgr FROM Projs p)
© Ellis Cohen 2001-2007
5
SOME Exercise!
Use SOME to write the following
query:
List the names of employees who
manage some project whose
budget is identical to their salary
© Ellis Cohen 2001-2007
6
SOME Answer!
List the names of employees who
manage some project whose
budget is identical to their salary
SELECT ename FROM Emps e WHERE
(SOME Projs p
SATISFIES e.empno = p.pmgr
AND e.sal = p.budget)
© Ellis Cohen 2001-2007
7
NO
List the names of employees
who are not project managers
SELECT ename FROM Emps e
WHERE NOT exists(
SELECT * FROM Projs p
WHERE p.pmgr = e.empno)
Equivalent to
SELECT ename FROM Emps e WHERE
NOT (SOME Projs p
SATISFIES p.pmgr = e.empno)
But you can write it more succinctly as:
SELECT ename FROM Emps e WHERE
(NO Projs p
SATISFIES p.pmgr = e.empno)
© Ellis Cohen 2001-2007
8
Counting vs Existential Subqueries
SELECT ename FROM Emps e WHERE
(SOME Projs p
SATISFIES p.pmgr = e.empno)
SELECT ename FROM Emps e WHERE (
SELECT count(*) FROM Projs p
WHERE p.pmgr = e.empno) > 0
SELECT ename FROM Emps e WHERE
(NO Projs p
SATISFIES p.pmgr = e.empno)
SELECT ename FROM Emps e WHERE (
SELECT count(*) FROM Projs p
WHERE p.pmgr = e.empno) = 0
© Ellis Cohen 2001-2007
9
ONE
List the names of employees
who manage exactly one project
SELECT ename FROM Emps e
WHERE
(SELECT count(*) FROM Projs p
WHERE p.pmgr = e.empno)) = 1
Wouldn't you rather write:
SELECT ename FROM Emps e WHERE
(ONE Projs p
SATISFIES p.pmgr = e.empno)
© Ellis Cohen 2001-2007
10
Extending SQL with Existential Subqueries
SOME, ONE & NO
are not part of any standard
or commercial SQL
But, you will find it very
convenient to use them when
writing complex queries, which
you can then convert (by hand)
to standard SQL
© Ellis Cohen 2001-2007
11
Translating SOME, ONE & NO
SOME Tbl t SATISFIES expr
exists(
SELECT * FROM Tbl t
WHERE expr)
ONE Tbl t SATISFIES expr
(SELECT count(*) FROM Tbl t
WHERE expr) = 1
NO Tbl t SATISFIES expr
NOT exists(
SELECT * FROM Tbl t
WHERE expr)
© Ellis Cohen 2001-2007
12
SOME with WHERE
It can be convenient to use both
WHERE & SATISFIES with
SOME, NO and ONE.
Some employee is dept 20 is a clerk.
SOME Emps
WHERE deptno = 20
SATISFIES job = 'CLERK'
Equivalent
Some employee is in dept 20 & is a clerk
SOME Emps
SATISFIES deptno = 20 AND job = 'CLERK'
© Ellis Cohen 2001-2007
13
Extending SQL
with
Universal Subqueries
Another useful, but non-existent
SQL construct
based on the Relational Calculus
& First-Order Logic
© Ellis Cohen 2001-2007
14
EACH
List departments whose employees
all make > 1000

List the name of a department as long as
Each employee
Who is in that department
Has a salary of more than 1000
In Extended SQL:
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
EACH always has a SATISFIES clause
The WHERE clause is not always needed (but be careful …)
© Ellis Cohen 2001-2007
15
EACH: WHERE and SATISFIES
… EACH Emps e WHERE e.deptno = 20 SATISFIES e.sal > 1000
7782
7839
7934
7369
7876
7902
7788
7566
7499
7698
7654
7900
7844
7521
CLARK
KING
MILLER
SMITH
ADAMS
FORD
SCOTT
JONES
ALLEN
BLAKE
MARTIN
JAMES
TURNER
WARD
10
10
10
20
20
20
20
20
30
30
30
30
30
30
2450
5000
1300
800
1100
3000
3000
2975
1600
2850
1250
950
1500
1250
WHERE identifies
a subset of the tuples
(e.g. those in dept 20)
SATISFIES checks
whether that subset all
satisfy the property
(all have sal > 1000)
© Ellis Cohen 2001-2007
16
Understanding EACH
…
EACH Emps e
WHERE e.deptno = 20
SATISFIES e.sal > 1000
Checks that every employee
in department 20 has sal > 1000
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
Find the departments d
in which every employee
in that department has sal > 1000
© Ellis Cohen 2001-2007
17
EACH: WHERE vs SATISFIES
What's the difference between
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
NOT THE SAME
SELECT dname FROM Depts d WHERE
(EACH Emps e
SATISFIES e.deptno = d.deptno
AND e.sal > 1000)
© Ellis Cohen 2001-2007
18
Answer: WHERE vs SATISFIES
SELECT dname FROM Depts d WHERE
(EACH Emps e
SATISFIES e.deptno = d.deptno
AND e.sal > 1000)
only selects a department if
every employee is in that department
AND every employee makes more
than 1000!
© Ellis Cohen 2001-2007
19
Counting vs Universal Subqueries
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
SELECT dname FROM Depts d WHERE
(SELECT count(*) FROM Emps e
WHERE e.deptno = d.deptno
AND e.sal > 1000) =
(SELECT count(*) FROM Emps e
WHERE e.deptno = d.deptno)
© Ellis Cohen 2001-2007
20
EACH without WHERE
List the names of departments whose
employees all make > 1000
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
is equivalent to
Either the employee is not in the department
SELECT dname FROM Depts d WHERE
(EACH Emps e
SATISFIES e.deptno IS NULL
OR e.deptno != d.deptno
OR e.sal > 1000)
OR (they are in the department, so) they better make > 1000
© Ellis Cohen 2001-2007
21
EACH and ALL
SELECT dname FROM Depts d
WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
SELECT dname FROM Depts d
WHERE
1000 < ALL (
SELECT e.sal FROM Emps e
WHERE e.deptno = d.deptno)
Find the names of the depts where no employees are
commissioned (i.e. all the commissions are null)
both with and without using EACH
© Ellis Cohen 2001-2007
22
EACH and ALL (w NULL Tests)
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.comm IS NULL)
SELECT dname FROM Depts d WHERE
(SELECT sum(e.comm)
FROM Emps e
WHERE e.deptno = d.deptno) IS NULL
-- or compare the count to zero
SELECT dname FROM Depts d WHERE
-1 = ALL (SELECT nvl( sal, -1 )
FROM Emps e
WHERE e.deptno = d.deptno)
© Ellis Cohen 2001-2007
23
EACH  NO
List the names of departments whose
employees all make > 1000

List the name of a department as long as
There is no employee
Who is in that department
and does not make more than 1000
Corresponds to:
SELECT dname FROM Depts d WHERE
(NO Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal <= 1000)
Assumes every employee has a salary.
What if that's not true
© Ellis Cohen 2001-2007
24
EACH  NO with Nullable Attributes
List the names of departments whose
employees all make > 1000

List the name of a department as long as
There is no employee
Who is in that department
and either has a salary of 1000 or less
or has no salary
Corresponds to:
SELECT dname FROM Depts d WHERE
(NO Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal <= 1000 OR e.sal IS NULL)
© Ellis Cohen 2001-2007
25
EACH  NO  EXISTS
List the names of departments whose
employees all make > 1000
SELECT dname FROM Depts d WHERE
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal > 1000)
SELECT dname FROM Depts d WHERE
(NO Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal <= 1000)
Assuming every employee has a salary,
Rewrite this in standard SQL using EXISTS
© Ellis Cohen 2001-2007
26
NO  EXISTS
List the names of departments whose
employees all make > 1000
SELECT dname FROM Depts d WHERE
(NO Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal <= 1000)
SELECT dname FROM Depts d WHERE
NOT (SOME Emps e
WHERE e.deptno = d.deptno
SATISFIES e.sal <= 1000)
SELECT dname FROM Depts d WHERE
NOT exists (SELECT * Emps e
WHERE e.deptno = d.deptno
AND e.sal <= 1000)
© Ellis Cohen 2001-2007
27
Translating EACH via EXISTS
EACH Tbl t SATISFIES expr
NO Tbl t SATISFIES
NOT(expr) OR (expr IS NULL)
NOT exists( SELECT * FROM Tbl t
WHERE (NOT(expr) OR (expr IS NULL)) )
EACH Tbl t WHERE cond SATISFIES expr
NO Tbl t SATISFIES
cond AND (NOT(expr) OR (expr IS NULL))
NOT exists( SELECT * FROM Tbl t
WHERE cond AND
(NOT(expr) OR (expr IS NULL)) )
Suppose Emps is empty. Is this true or false:
EACH Emps SATISFIES sal > 10000000
© Ellis Cohen 2001-2007
28
State Assertions
with
Quantifier Subqueries
© Ellis Cohen 2001-2007
29
Quantifier Subqueries
Quantifier Subqueries are
Existential Subqueries
SOME, ONE & NONE
Universal Subqueries
EACH
Corresponds to quantifiers of
first-order predicate calculus
© Ellis Cohen 2001-2007
30
Using SOME in State Assertions
SOME Emps e SATISFIES
e.sal > 1500
– some employee has a salary
greater than 1500
SOME Emps e1 SATISFIES
(SOME Emps e2 SATISFIES
e1.empno != e2.empno AND
e1.sal = e2.sal)
– there are two different employees
who have the same salary
© Ellis Cohen 2001-2007
31
Mandatory Participation
EACH Depts d SATISFIES
(SOME Emps e SATISFIES
e.deptno = d.deptno)
-- For each dept, there is some
employee in that department
(SELECT deptno FROM Depts) =
(SELECT DISTINCT deptno
FROM Emps)
© Ellis Cohen 2001-2007
32
Multilevel Use of SOME & EACH
EACH Depts d WHERE loc = 'BOSTON' SATISFIES
(EACH Emps e
WHERE e.deptno = d.deptno
AND e.job = 'CLERK'
SATISFIES e.sal > 1000)
-- In every department located in Boston,
every clerk makes more than 1000
SOME Depts d SATISFIES
(EACH Emps e
WHERE e.deptno = d.deptno
SATISFIES sal > 1500)
-- there is some department in which
every employee makes a salary greater than 1500
EACH Depts d SATISFIES
(SOME Emps e
SATISFIES e.deptno = d.deptno
AND sal > 1500)
-- Every department has some employee
who makes a salary greater than 1500
© Ellis Cohen 2001-2007
33
Extended SQL Assertion Exercise
Write Extended SQL Equivalents
Every department
has a clerk
Every department
that has employees
has a clerk
© Ellis Cohen 2001-2007
34
Extended SQL Assertion Answers
Every department has a clerk
EACH Depts d SATISFIES
(SOME Emps e
WHERE e.deptno = d.deptno AND
SATISFIES e.job = 'CLERK')
Every department
that has employees
has a clerk
EACH Depts d
WHERE (SOME Emps e SATISFIES
e.deptno = d.deptno)
SATISFIES (SOME Emps e
WHERE e.deptno = d.deptno
SATISFIES e.job = 'CLERK')
© Ellis Cohen 2001-2007
35
Scope for Table Names/Aliases
Every department that has employees
has a clerk
EACH Depts d
WHERE (SOME Emps e SATISFIES
e.deptno = d.deptno)
SATISFIES (SOME Emps e
WHERE e.deptno = d.deptno
SATISFIES e.job = 'CLERK')
What's wrong with the following assertion?
EACH Depts d
WHERE (SOME Emps e SATISFIES
e.deptno = d.deptno)
SATISFIES (e.job = 'CLERK')
© Ellis Cohen 2001-2007
36
Factored Extended SQL Assertions
Every department
that has employees
has a clerk
CREATE VIEW EmpDepts AS
(SELECT DISTINCT deptno FROM Emps)
EACH EmpDepts ed SATISFIES
SOME Emps e SATISFIES
e.deptno = ed.deptno AND
e.job = 'CLERK'
© Ellis Cohen 2001-2007
37
Manifest Views
Manifest Views simplify enforcing & debugging
constraints and debugging data which violate them,
due to the following characteristics:
1) Manifest: Just by looking at the contents of the
manifest view, it is easy to tell whether the
constraint is satisfied.
2) Easy Checking: The constraint can be enforced
simply and clearly based on the manifest view.
3) Summarization: The view summarizes the
underlying data, keeping as much information as
possible. It should not be empty or result in just
one tuple if the constraint is satisfied.
4) Debugging: If the manifest view indicates that the
constraint is not satisfied, it also provides useful
information about which tuples in the underlying
data are responsible for violating the constraint.
Write the manifest view & extended SQL assertion for:
Every department that has employees has a clerk
© Ellis Cohen 2001-2007
38
Best Manifest View Answer
Every department that has employees
has a clerk
Manifest view: For every department, show the
# of employees, and the # of clerks
CREATE VIEW DeptEmpClerks AS
SELECT deptno,
(SELECT count(*) FROM Emps e
WHERE e.deptno = d.deptno) AS eknt,
(SELECT count(*) FROM Emps e
WHERE e.deptno = d.deptno
AND e.job = 'CLERK') AS cknt
FROM Depts d
EACH DeptEmpClerks
WHERE eknt > 0
SATISIES cknt > 0
© Ellis Cohen 2001-2007
39
Alt Manifest View Answer
Every department that has employees
has a clerk
Manifest View: For every dept that has employees
show the # of clerks
CREATE VIEW EDClerks AS
WITH EmpDepts AS
(SELECT DISTINCT deptno FROM Emps)
SELECT deptno,
(SELECT count(*) FROM Emps e
WHERE e.deptno = ed.deptno
AND e.job = 'CLERK') AS cknt
FROM EmpDepts ed
EACH EDClerks
SATISIES cknt > 0
© Ellis Cohen 2001-2007
40
Extending SQL
with Attribute Filtering
Another useful, but
non-existent SQL construct
© Ellis Cohen 2001-2007
41
Attribute Filtering
Attribute filtering selects a subset of
the tuples of a relation based on
restricting one of more of its
attributes to specific values
Emps( job: 'CLERK' ) means
(SELECT * FROM Emps
WHERE job = 'CLERK')
Emps( job: j, salary: 2000 ) means
(SELECT * FROM Emps
WHERE job = j
AND salary = 2000)
© Ellis Cohen 2001-2007
42
Selection Examples
List the names of employees who are clerks
SELECT ename
FROM Emps( job: 'CLERK' )
List the names of employees who are clerks
and who make > 1000
SELECT ename
FROM Emps( job: 'CLERK' )
WHERE sal > 1000
© Ellis Cohen 2001-2007
43
Quantified Examples
List the names of departments which have a clerk
SELECT dname FROM Depts d
WHERE
(SOME Emps( deptno: d.deptno,
job: 'CLERK' ))
List the names of departments which have an
employee who makes more than 2000
SELECT dname FROM Depts d
WHERE
(SOME Emps( deptno: d.deptno )
SATISFIES sal > 2000)
© Ellis Cohen 2001-2007
44
Universally Quantified Example
List the names of departments where
every employee makes more than 2000
SELECT dname FROM Depts d
WHERE
(EACH Emps( deptno: d.deptno )
SATISFIES sal > 2000)
© Ellis Cohen 2001-2007
45
Attribute Filtering Exercise
Every department has a clerk
EACH Depts d SATISFIES
(SOME Emps e SATISFIES
e.deptno = d.deptno AND
e.job = 'CLERK')
Every department that has employees
has a clerk
EACH Depts d
WHERE (SOME Emps e SATISFIES
e.deptno = d.deptno)
SATISFIES (SOME Emps e SATISFIES
e.deptno = d.deptno AND
e.job = 'CLERK')
Rewrite these using attribute filtering
© Ellis Cohen 2001-2007
46
SQL Assertions
with Attribute Filtering
Every department has a clerk
EACH Depts d SATISFIES
(SOME Emps( deptno: d.deptno, job: 'CLERK' ))
Every department that has employees
has a clerk
EACH Depts d
WHERE
(SOME Emps( deptno: d.deptno ))
SATISFIES
(SOME Emps( deptno: d.deptno, job: 'CLERK' ))
© Ellis Cohen 2001-2007
47
Multi-Join Example
For each project, list its name,
and the name of the
department of its project
manager
SELECT pname, dname
FROM Projs p,
(Emps( empno: p.pmgr )
NATURAL JOIN Depts)
© Ellis Cohen 2001-2007
48
Negation & Set Difference
Suppose BadEmps is a table of employee #s of
bad employees.
List the names of all employees who make more
than 1000 and who are not bad
SELECT ename
FROM Emps e
WHERE sal > 1000
AND (NO BadEmps(
empno: e.empno ))
© Ellis Cohen 2001-2007
49
The Tuple
Relational
Calculus
© Ellis Cohen 2001-2007
50
Extended SQL  Relational Calculus
The TRC (Tuple Relational Calculus) is a formalization
on which many features of SQL are based
(especially subqueries). It is similar to Extended
SQL, with some small changes:
Rewrite
SOME Emps e SATISFIES e.age > 20
 SOME e SATISFIES (e  Emps) AND (e.age > 20)
Rewrite
EACH Emps e SATISFIES e.age > 20
 EACH e WHERE e  Emps SATISFIES e.age > 20
Rewrite
SELECT e.empno FROM Emps e WHERE e.age > 20
 SELECT e.empno
WHERE (e  Emps) AND (e.age > 20)
© Ellis Cohen 2001-2007
51
Boolean Expressions
Boolean expressions in the Tuple
Relational Calculus are formed using
• Membership: e.g. e  Emps
• Comparison: e.g. e.age > 20
• Logical Operators: e.g.
NOT(e  Emps) AND (e.age > 20)
• Quantification
SOME e SATISFIES …
EACH e SATISFIES …
EACH e WHERE … SATISFIES …
© Ellis Cohen 2001-2007
52
Simple Example
List the names of employees who are clerks
SELECT e.ename WHERE
(e  Emps)
AND
(e.job = 'CLERK')
© Ellis Cohen 2001-2007
53
Comparison Example
List the names of employees who are clerks
and who make > 1000
SELECT e.ename WHERE
(e  Emps)
AND
(e.job = 'CLERK')
AND
(e.sal > 1000)
© Ellis Cohen 2001-2007
54
Quantified Example
List the names of departments which have an
employee who makes more than 2000
SELECT d.dname WHERE
(d  Depts)
AND
(SOME e SATISFIES
(e  Emps)
AND
(e.deptno = d.deptno)
AND
(e.sal > 2000))
© Ellis Cohen 2001-2007
55
Universally Quantified Example
List the names of departments where
every employee makes more than 2000
SELECT d.dname WHERE
(d  Depts)
AND
(EACH e WHERE
(e  Emps)
AND
(e.deptno = d.deptno)
SATISFIES
e.sal > 2000)
© Ellis Cohen 2001-2007
56
Multi-Join Example
For each project, list its name, and the
name of the department of its project
manager
SELECT p.pname, d.dname WHERE
(p  Projs) AND (d  Depts)
AND
(SOME e SATISFIES
(e  Emps)
AND
(p.pmgr = e.empno)
AND
(e.deptno = d.deptno))
© Ellis Cohen 2001-2007
57
Set Union
Suppose ExEmps is a table of information about ex-employees
List the names of all current and ex-employees
SELECT e.ename WHERE
(e  Emps)
OR
(e  ExEmps)
© Ellis Cohen 2001-2007
58
Negation & Set Difference
Suppose BadEmps is a table of employee #s of
bad employees.
List the names of all employees who make more
than 1000 and who are not bad
SELECT e.ename WHERE
(e  Emps)
Negated Membership
is necessary
AND
to represent
Set Difference
(e.sal > 1000)
AND
NOT (e  BadEmps)
© Ellis Cohen 2001-2007
59
Safe & Unsafe Queries
Negated Membership leads to
unsafe queries
SELECT e.ename WHERE
NOT (e  Emps)
asks for the names of any tuple that's not in
Emps.
There's an infinite number of such tuples!
A Relational Calculus expression is safe
if its results can be determined by only
examining tuples in the relations named
in the expression (e.g. Emps)
© Ellis Cohen 2001-2007
60
Primitive Tuple Relational Calculus
We have been describing the Primitive Tuple
Relational Calculus. It ONLY has
tuple variables with qualified attributes (e.g. e.sal)
comparison operators (<, =, etc.)
tuple membership tests (e.g. e  Emps)
logical operators (AND, OR, NOT)
quantification expressions
SOME e1, e2, … SATISFIES …
EACH e1, e2, … SATISFIES …
EACH e1, e2, … WHERE … SATISFIES …
simple SELECT (SELECT … WHERE …)
at the outermost level only
It does NOT have
numeric/string operations (+, –, ||, etc.)
ordinary or aggregate functions
duplicates (no base relations have duplicates, and
SELECT does not produce duplicates)
any mechanism for grouping
collection operations (UNION, INTERSECT, EXCEPT)
join operators
© Ellis Cohen 2001-2007
61
Primitive SQL
Suppose we define Primitive SQL.
It is exactly like standard SQL queries
(SELECTs only), except we remove
numeric/string operations (+, –, ||, etc.)
ordinary or aggregate functions
duplicates (no base relations have duplicates, and
all SELECTs automatically remove duplicates).
© Ellis Cohen 2001-2007
62
Relational Equivalence
Exactly the same set of queries
can be expressed using
• Primitive SQL
• The Safe Primitive Tuple
Relational Calculus
© Ellis Cohen 2001-2007
63
The Domain
Relational Calculus
© Ellis Cohen 2001-2007
64
Domain Relational Calculus
The TRC (Tuple Relational Calculus)
• Variables used in SOME, EACH &
SELECT represent tuples
• Membership tested using 
The DRC (Domain Relational Calculus)
• Variables used in SOME, EACH &
SELECT represent domain values
(e.g. strings, integers, etc)
• Membership tested based on
attribute matching
© Ellis Cohen 2001-2007
65
Attribute Matching
Examples of Attribute Matching
?Emps( job: 'CLERK' )
– means that there is some employee in
Emps whose job is CLERK
?Emps( job: j )
– means that there is some employee in
Emps whose job has the same value as j
?Emps( job: j, salary: 2000 )
– means that there is some employee in
Emps whose job has the same value as j
and whose salary is 2000
© Ellis Cohen 2001-2007
66
Simple Example
List the names of employees who are clerks
SELECT en WHERE
?Emps(
ename: en, job: 'CLERK' )
© Ellis Cohen 2001-2007
67
Comparison Example
List the names of employees who are clerks
and who make > 1000
SELECT enam WHERE
(SOME esal SATISFIES
?Emps( ename: enam, sal: esal,
job: 'CLERK' )
AND
(esal > 1000))
© Ellis Cohen 2001-2007
68
Quantified Example
List the names of departments which have an
employee who makes more than 2000
SELECT dnam WHERE
(SOME dno, esal SATISFIES
?Depts( deptno: dno, dname: dnam )
AND
?Emps( deptno: dno, sal: esal )
AND
(esal > 2000))
© Ellis Cohen 2001-2007
69
Universally Quantified Example
List the names of departments where
every employee makes more than 2000
SELECT dnam WHERE
(SOME dno SATISFIES
?Depts( deptno: dno, dname: dnam )
AND
(EACH eno, esal WHERE
?Emps( empno: eno, deptno: dno,
sal: esal )
SATISFIES
esal > 2000))
© Ellis Cohen 2001-2007
70
Multi-Join Example
For each project, list its name, and the
name of the department of its project
manager
SELECT pnam, dnam WHERE
(SOME eno, dno SATISFIES
?Projs( pname: pnam, pmgr: eno )
AND
?Emps( empno: eno, deptno: dno )
AND
?Depts( deptno: dno, dname: dnam ))
© Ellis Cohen 2001-2007
71
Set Union
Suppose ExEmps is a table of information about ex-employees
List the names of all current and ex-employees
SELECT enam WHERE
?Emps( ename: enam )
OR
?ExEmps( ename: enam )
© Ellis Cohen 2001-2007
72
Negation & Set Difference
Suppose BadEmps is a table of employee #s of
bad employees.
List the names of all employees who make more
than 1000 and who are not bad
SELECT enam WHERE
(SOME esal SATISFIES
?Emps( ename: enam, sal: esal )
AND
(esal > 1000)
AND
NOT ?BadEmps( ename: enam ))
© Ellis Cohen 2001-2007
73
Query-By-Example
The DRC (Domain Relational Calculus) is the
basis of QBE (Query-By-Example) used in
visual query languages in DB's like MS
Access and FileMaker Pro.
These generally allow comparisons in
attribute matching as well. In the DRC,
this would correspond to
?Emps( job: j, salary: < 2000 )
– means that there is some employee in
Emps whose job has the same value as j
and whose salary is < 2000
© Ellis Cohen 2001-2007
74
Examples with
Attribute Comparison Matching
List the names of employees who are clerks
and who make > 1000
SELECT enam WHERE
?Emps( ename: enam, job: 'CLERK', sal: > 1000)
List the names of departments where
every employee makes more than 2000
SELECT dnam WHERE
(SOME dno SATISFIES
?Depts( deptno: dno, dname: dnam )
AND
(EACH eno WHERE
?Emps( empno: eno, deptno: dno )
SATISFIES
?Emps( empno: eno, sal: > 2000 ))
© Ellis Cohen 2001-2007
75
Using Set Membership
A simplified but more verbose variant of the
DRC uses set membership instead of
attribute matching and builds tuples
explicitly
Emps( empno, ename, job, sal, deptno)
List the names of employees who are clerks
SELECT enam WHERE
(SOME eno, ejob, esal, dno SATISFIES
[eno, enam, ejob, esal, dno]  Emps
AND
ejob = 'CLERK')
© Ellis Cohen 2001-2007
76
Primitive Domain Relational Calculus
We have been describing the Primitive Domain
Relational Calculus. It ONLY has
domain variables
comparison operators (<, =, etc.)
attribute matching tests (e.g. Emps( job: 'CLERK' ))
logical operators (AND, OR, NOT)
quantification expressions
SOME e1, e2, … SATISFIES …
EACH e1, e2, … SATISFIES …
EACH e1, e2, … WHERE … SATISFIES …
simple SELECT (SELECT … WHERE …)
at the outermost level only
It does NOT have
numeric/string operations (+, –, ||, etc.)
ordinary or aggregate functions
duplicates (no base relations have duplicates, and
SELECT does not produce duplicates)
any mechanism for grouping
collection operations (UNION, INTERSECT, EXCEPT)
join operators
© Ellis Cohen 2001-2007
77
Relational Equivalence
Exactly the same set of queries
can be expressed
• using the Primitive Tuple Relational
Calculus, and
• using the Primitive Domain Relational
Calculus
The same is true, if we restrict the queries
to those which are safe
© Ellis Cohen 2001-2007
78