Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2007 Extended SQL & The Relational Calculus.

Similar presentations


Presentation on theme: "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2007 Extended SQL & The Relational Calculus."— Presentation transcript:

1 1 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

2 2 © Ellis Cohen 2001-2007 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

3 3 © Ellis Cohen 2001-2007 Extending SQL with Existential Subqueries Useful, but non-existent SQL constructs based on the Relational Calculus & First-Order Logic

4 4 © Ellis Cohen 2001-2007 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

5 5 © Ellis Cohen 2001-2007 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)

6 6 © Ellis Cohen 2001-2007 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

7 7 © Ellis Cohen 2001-2007 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)

8 8 © Ellis Cohen 2001-2007 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)

9 9 © Ellis Cohen 2001-2007 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

10 10 © Ellis Cohen 2001-2007 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)

11 11 © Ellis Cohen 2001-2007 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

12 12 © Ellis Cohen 2001-2007 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)

13 13 © Ellis Cohen 2001-2007 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' Some employee is in dept 20 & is a clerk SOME Emps SATISFIES deptno = 20 AND job = 'CLERK' Equivalent

14 14 © Ellis Cohen 2001-2007 Extending SQL with Universal Subqueries Another useful, but non-existent SQL construct based on the Relational Calculus & First-Order Logic

15 15 © Ellis Cohen 2001-2007 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 …)

16 16 © Ellis Cohen 2001-2007 EACH: WHERE and SATISFIES … EACH Emps e WHERE e.deptno = 20 SATISFIES e.sal > 1000 7782CLARK102450 7839KING105000 7934MILLER101300 7369SMITH20800 7876ADAMS201100 7902FORD203000 7788SCOTT203000 7566JONES202975 7499ALLEN301600 7698BLAKE302850 7654MARTIN301250 7900JAMES30950 7844TURNER301500 7521WARD301250 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)

17 17 © Ellis Cohen 2001-2007 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

18 18 © Ellis Cohen 2001-2007 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) SELECT dname FROM Depts d WHERE (EACH Emps e SATISFIES e.deptno = d.deptno AND e.sal > 1000) NOT THE SAME

19 19 © Ellis Cohen 2001-2007 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!

20 20 © Ellis Cohen 2001-2007 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)

21 21 © Ellis Cohen 2001-2007 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 SELECT dname FROM Depts d WHERE (EACH Emps e SATISFIES e.deptno IS NULL OR e.deptno != d.deptno OR e.sal > 1000) Either the employee is not in the department OR (they are in the department, so) they better make > 1000

22 22 © Ellis Cohen 2001-2007 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

23 23 © Ellis Cohen 2001-2007 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)

24 24 © Ellis Cohen 2001-2007 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

25 25 © Ellis Cohen 2001-2007 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)

26 26 © Ellis Cohen 2001-2007 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

27 27 © Ellis Cohen 2001-2007 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)

28 28 © Ellis Cohen 2001-2007 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

29 29 © Ellis Cohen 2001-2007 State Assertions with Quantifier Subqueries

30 30 © Ellis Cohen 2001-2007 Quantifier Subqueries Quantifier Subqueries are Existential Subqueries SOME, ONE & NONE Universal Subqueries EACH Corresponds to quantifiers of first-order predicate calculus

31 31 © Ellis Cohen 2001-2007 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

32 32 © Ellis Cohen 2001-2007 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)

33 33 © Ellis Cohen 2001-2007 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

34 34 © Ellis Cohen 2001-2007 Extended SQL Assertion Exercise Every department has a clerk Every department that has employees has a clerk Write Extended SQL Equivalents

35 35 © Ellis Cohen 2001-2007 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')

36 36 © Ellis Cohen 2001-2007 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')

37 37 © Ellis Cohen 2001-2007 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'

38 38 © Ellis Cohen 2001-2007 Manifest Views Manifest Views simplify enforcing & debugging constraints and debugging data which violate them, due to the following characteristics: 1)Manifest: 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: 2)Easy Checking: The constraint can be enforced simply and clearly based on the manifest view. 3)Summarization: 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: 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

39 39 © Ellis Cohen 2001-2007 Best Manifest View Answer Every department that has employees has a clerk 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 Manifest view: For every department, show the # of employees, and the # of clerks

40 40 © Ellis Cohen 2001-2007 Alt Manifest View Answer Every department that has employees has a clerk 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 Manifest View: For every dept that has employees show the # of clerks

41 41 © Ellis Cohen 2001-2007 Extending SQL with Attribute Filtering Another useful, but non-existent SQL construct

42 42 © Ellis Cohen 2001-2007 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)

43 43 © Ellis Cohen 2001-2007 Selection Examples SELECT ename FROM Emps( job: 'CLERK' ) List the names of employees who are clerks SELECT ename FROM Emps( job: 'CLERK' ) WHERE sal > 1000 List the names of employees who are clerks and who make > 1000

44 44 © Ellis Cohen 2001-2007 Quantified Examples SELECT dname FROM Depts d WHERE (SOME Emps( deptno: d.deptno ) SATISFIES sal > 2000) 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, job: 'CLERK' )) List the names of departments which have a clerk

45 45 © Ellis Cohen 2001-2007 Universally Quantified Example SELECT dname FROM Depts d WHERE (EACH Emps( deptno: d.deptno ) SATISFIES sal > 2000) List the names of departments where every employee makes more than 2000

46 46 © Ellis Cohen 2001-2007 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

47 47 © Ellis Cohen 2001-2007 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' ))

48 48 © Ellis Cohen 2001-2007 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)

49 49 © Ellis Cohen 2001-2007 Negation & Set Difference SELECT ename FROM Emps e WHERE sal > 1000 AND (NO BadEmps( empno: e.empno )) 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

50 50 © Ellis Cohen 2001-2007 The Tuple Relational Calculus

51 51 © Ellis Cohen 2001-2007 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)

52 52 © Ellis Cohen 2001-2007 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 …

53 53 © Ellis Cohen 2001-2007 Simple Example  SELECT e.ename WHERE (e  Emps) AND (e.job = 'CLERK') List the names of employees who are clerks

54 54 © Ellis Cohen 2001-2007 Comparison Example  SELECT e.ename WHERE (e  Emps) AND (e.job = 'CLERK') AND (e.sal > 1000) List the names of employees who are clerks and who make > 1000

55 55 © Ellis Cohen 2001-2007 Quantified Example   SELECT d.dname WHERE (d  Depts) AND (SOME e SATISFIES (e  Emps) AND (e.deptno = d.deptno) AND (e.sal > 2000)) List the names of departments which have an employee who makes more than 2000

56 56 © Ellis Cohen 2001-2007 Universally Quantified Example   SELECT d.dname WHERE (d  Depts) AND (EACH e WHERE (e  Emps) AND (e.deptno = d.deptno) SATISFIES e.sal > 2000) List the names of departments where every employee makes more than 2000

57 57 © Ellis Cohen 2001-2007 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))

58 58 © Ellis Cohen 2001-2007 Set Union   SELECT e.ename WHERE (e  Emps ) OR (e  ExEmps) Suppose ExEmps is a table of information about ex-employees List the names of all current and ex-employees

59 59 © Ellis Cohen 2001-2007 Negation & Set Difference   SELECT e.ename WHERE (e  Emps ) AND (e.sal > 1000) AND NOT (e  BadEmps ) 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 Negated Membership is necessary to represent Set Difference

60 60 © Ellis Cohen 2001-2007 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)

61 61 © Ellis Cohen 2001-2007 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

62 62 © Ellis Cohen 2001-2007 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).

63 63 © Ellis Cohen 2001-2007 Relational Equivalence Exactly the same set of queries can be expressed using Primitive SQL The Safe Primitive Tuple Relational Calculus

64 64 © Ellis Cohen 2001-2007 The Domain Relational Calculus

65 65 © Ellis Cohen 2001-2007 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

66 66 © Ellis Cohen 2001-2007 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

67 67 © Ellis Cohen 2001-2007 Simple Example SELECT en WHERE ?Emps( ename: en, job: 'CLERK' ) List the names of employees who are clerks

68 68 © Ellis Cohen 2001-2007 Comparison Example SELECT enam WHERE (SOME esal SATISFIES ?Emps( ename: enam, sal: esal, job: 'CLERK' ) AND (esal > 1000)) List the names of employees who are clerks and who make > 1000

69 69 © Ellis Cohen 2001-2007 Quantified Example SELECT dnam WHERE (SOME dno, esal SATISFIES ?Depts( deptno: dno, dname: dnam ) AND ?Emps( deptno: dno, sal: esal ) AND (esal > 2000)) List the names of departments which have an employee who makes more than 2000

70 70 © Ellis Cohen 2001-2007 Universally Quantified Example 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)) List the names of departments where every employee makes more than 2000

71 71 © Ellis Cohen 2001-2007 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 ))

72 72 © Ellis Cohen 2001-2007 Set Union SELECT enam WHERE ?Emps( ename: enam ) OR ?ExEmps( ename: enam ) Suppose ExEmps is a table of information about ex-employees List the names of all current and ex-employees

73 73 © Ellis Cohen 2001-2007 Negation & Set Difference SELECT enam WHERE (SOME esal SATISFIES ?Emps( ename: enam, sal: esal ) AND (esal > 1000) AND NOT ?BadEmps( ename: enam )) 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

74 74 © Ellis Cohen 2001-2007 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

75 75 © Ellis Cohen 2001-2007 Examples with Attribute Comparison Matching SELECT enam WHERE ?Emps( ename: enam, job: 'CLERK', sal: > 1000) List the names of employees who are clerks and who make > 1000 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 )) List the names of departments where every employee makes more than 2000

76 76 © Ellis Cohen 2001-2007 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')

77 77 © Ellis Cohen 2001-2007 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

78 78 © Ellis Cohen 2001-2007 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


Download ppt "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2007 Extended SQL & The Relational Calculus."

Similar presentations


Ads by Google