Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2009 Collection Operators These slides are.

Similar presentations


Presentation on theme: "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2009 Collection Operators These slides are."— Presentation transcript:

1 1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2009 Collection Operators 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-2009 Overview of Lecture Union Intersection Difference Collection Operators & Joins Transitive Closure & Recursive Views

3 3 © Ellis Cohen 2001-2009 Union

4 4 © Ellis Cohen 2001-2009 Need for Unions empno ename deptno … hiredate 7499ALLEN30…20-FEB-81 7654MARTIN30…28-SEP-81 7698BLAKE30…01-MAY-81 7839KING10…17-NOV-81 7844TURNER30…08-SEP-81 7986STERN50…23-NOV-99 pno pname pstart 60492Running Amuck…12-FEB-82 60493Cooling Off…01-JAN-05 60498Lifting Off…01-JAN-05 Suppose you wanted to generate a combined list of important events (employee hirings + project starts) Event Name Event Date

5 5 © Ellis Cohen 2001-2009 Result of Unions evname evdate ALLEN20-FEB-81 MARTIN28-SEP-81 BLAKE01-MAY-81 KING17-NOV-81 TURNER08-SEP-81 STERN23-NOV-99 Running Amuck12-FEB-82 Cooling Off01-JAN-05 Lifting Off01-JAN-05 SELECT ename AS evname, hiredate AS evdate FROM Emps UNION SELECT pname AS evname, pstart AS evdate FROM Projs

6 6 © Ellis Cohen 2001-2009 ORDER BY evname evdate ALLEN20-FEB-81 BLAKE01-MAY-81 Cooling Off01-JAN-05 KING17-NOV-81 Lifting Off01-JAN-05 MARTIN28-SEP-81 Running Amuck12-FEB-82 STERN23-NOV-99 TURNER08-SEP-81 SELECT ename AS evname, hiredate AS evdate FROM Emps UNION SELECT pname AS evname, pstart AS evdate FROM Projs ORDER BY ename ORDER BY at the end applies to entire result set

7 7 © Ellis Cohen 2001-2009 SELECT ('Hired ' || ename) AS evname, hiredate AS evdate FROM Emps UNION SELECT ('Started ' || pname) AS evname, pstart AS evdate FROM Projs WHERE pstart is NOT NULL ORDER BY evdate UNION Syntax Single ORDER BY at the end Types (not names) must conform First SELECT determines column names not needed, but put in in!

8 8 © Ellis Cohen 2001-2009 Simple Case Expressions SELECT ename, (CASE WHEN sal 3000 THEN 'OVERPAID' ELSE to_char(sal) END) AS salary, sal FROM Emps Rewrite this using UNION instead of CASE

9 9 © Ellis Cohen 2001-2009 Union of Disjoint Cases SELECT ename, 'UNDERPAID' AS salary, sal FROM Emps WHERE sal < 1500 UNION SELECT ename, to_char(sal) AS salary, sal FROM Emps WHERE sal BETWEEN 1500 AND 3000 UNION SELECT ename, 'OVERPAID' AS salary, sal FROM Emps WHERE sal > 3000

10 10 © Ellis Cohen 2001-2009 Unions with Duplicates SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE sal >= 3000 UNION SELECT job FROM Emps WHERE hiredate > '1-jan-82'

11 11 © Ellis Cohen 2001-2009 Counted Union vs. Set Union SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE sal >= 3000 UNION ALL SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST PRESIDENT ANALYST CLERK SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK SELECT job FROM Emps WHERE sal >= 3000 UNION SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK PRESIDENT Set Union eliminates duplicates Counted Union includes all tuples 12 1 2 also called MultiSet Union

12 12 © Ellis Cohen 2001-2009 Counted & Set Union SELECT job FROM Emps1 UNION SELECT job FROM Emps2 WITH CountedJobs AS ( SELECT job FROM Emps1 UNION ALL SELECT job FROM Emps2 ) SELECT DISTINCT job FROM CountedJobs Equivalent Use UNION ALL whenever possible, since it does not require removing duplicates, which can be expensive

13 13 © Ellis Cohen 2001-2009 Disjunctive Joins vs Unions List the names of all employees who are project or department managers Compare SELECT DISTINCT ename FROM Emps, Projs WHERE job = 'DEPTMGR' OR empno = pmgr vs SELECT ename FROM Emps WHERE job = 'DEPTMGR' UNION SELECT DISTINCT ename FROM (Emps JOIN Projs ON empno = pmgr) Which is better? Suppose there are no projects?

14 14 © Ellis Cohen 2001-2009 Disjunctive Joins w/o Projects If there are no projects in Projs SELECT ename FROM Emps WHERE job = 'DEPTMGR' UNION SELECT DISTINCT ename FROM Emps, Projs WHERE empno = pmgr SELECT DISTINCT ename FROM Emps, Projs WHERE job = 'DEPTMGR' OR empno = pmgr Empty Just the dept managers Empty Projs is empty, so ANY inner join with Projs will be empty too! Beware of disjunctive joins!

15 15 © Ellis Cohen 2001-2009 Intersection

16 16 © Ellis Cohen 2001-2009 Intersection SELECT ename FROM Emps WHERE job = 'CLERK' INTERSECT SELECT ename FROM Emps WHERE deptno = 20 In this case, we could also write SELECT ename FROM Emps WHERE job = 'CLERK' AND deptno = 20 Of course, this kind of rewriting won’t work when the intersected results are from different tables 1) Generates the ename's of clerks 2) Generates the ename's of employees in dept 20 3) Generates the ename's common to both – i.e. the clerks in department 20 1 2 3

17 17 © Ellis Cohen 2001-2009 Intersection SELECT ename FROM Emps WHERE job = 'CLERK' SMITH ADAMS JAMES MILLER SELECT ename FROM Emps WHERE deptno = 20 JONES SCOTT FORD SMITH ADAMS What's the result of SELECT ename FROM Emps WHERE job = 'CLERK' INTERSECT SELECT ename FROM Emps WHERE deptno = 20

18 18 © Ellis Cohen 2001-2009 Intersection SELECT ename FROM Emps WHERE job = 'CLERK' SMITH ADAMS JAMES MILLER SELECT ename FROM Emps WHERE deptno = 20 JONES SCOTT FORD SMITH ADAMS What's the result of SELECT ename FROM Emps WHERE job = 'CLERK' INTERSECT SELECT ename FROM Emps WHERE deptno = 20 SMITH ADAMS

19 19 © Ellis Cohen 2001-2009 Intersection with Duplicates SELECT job FROM Emps WHERE deptno = 20 CLERK MANAGER ANALYST CLERK ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE deptno = 20 INTERSECT SELECT job FROM Emps WHERE hiredate > '1-jan-82'

20 20 © Ellis Cohen 2001-2009 Intersection with Duplicates SELECT job FROM Emps WHERE deptno = 20 CLERK MANAGER ANALYST CLERK ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE deptno = 20 INTERSECT SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK Set Intersect eliminates duplicates

21 21 © Ellis Cohen 2001-2009 INTERSECT ALL SELECT job FROM Emps WHERE deptno = 20 CLERK MANAGER ANALYST CLERK ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE deptno = 20 INTERSECT ALL SELECT job FROM Emps WHERE hiredate > '1-jan-82' Not in Oracle

22 22 © Ellis Cohen 2001-2009 INTERSECT ALL vs INTERSECT SELECT job FROM Emps WHERE deptno = 20 CLERK MANAGER ANALYST CLERK ANALYST SELECT job FROM Emps WHERE deptno = 20 INTERSECT ALL SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK SELECT job FROM Emps WHERE deptno = 20 INTERSECT SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK Set Intersect eliminates duplicates Counted Intersect counts # of duplicate tuples & keep minimum # of each Not in Oracle Also called MultiSet Intersect

23 23 © Ellis Cohen 2001-2009 Counted & Set Intersect SELECT job FROM Emps1 INTERSECT SELECT job FROM Emps2 Equivalent SELECT DISTINCT job FROM Emps1 INTERSECT ALL SELECT DISTINCT job FROM Emps2 Not in Oracle

24 24 © Ellis Cohen 2001-2009 Difference

25 25 © Ellis Cohen 2001-2009 Difference SELECT ename FROM Emps WHERE job = 'CLERK' EXCEPT SELECT ename FROM Emps WHERE deptno = 20 In this case, we could also write SELECT empno FROM Emps WHERE job = 'CLERK' AND (deptno != 20 OR deptno IS NULL) Of course, this kind of rewriting won’t work when the differenced results are from different tables 1) Generates the ename's of clerks 2) Generates the ename's of employees in dept 20 3) Generates the ename's in (1) which are not in (2) 1 2 3 Oracle uses MINUS in place of EXCEPT

26 26 © Ellis Cohen 2001-2009 Difference SELECT ename FROM Emps WHERE job = 'CLERK' SMITH ADAMS JAMES MILLER SELECT ename FROM Emps WHERE deptno = 20 JONES SCOTT FORD SMITH ADAMS What's the result of SELECT ename FROM Emps WHERE job = 'CLERK' EXCEPT SELECT ename FROM Emps WHERE deptno = 20 Oracle uses MINUS in place of EXCEPT

27 27 © Ellis Cohen 2001-2009 Difference SELECT ename FROM Emps WHERE job = 'CLERK' SMITH ADAMS JAMES MILLER SELECT ename FROM Emps WHERE deptno = 20 JONES SCOTT FORD SMITH ADAMS What's the result of SELECT ename FROM Emps WHERE job = 'CLERK' EXCEPT SELECT ename FROM Emps WHERE deptno = 20 JAMES MILLER

28 28 © Ellis Cohen 2001-2009 Difference with Duplicates SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE sal >= 3000 EXCEPT SELECT job FROM Emps WHERE hiredate > '1-jan-82' Oracle uses MINUS in place of EXCEPT

29 29 © Ellis Cohen 2001-2009 Difference with Duplicates SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE sal >= 3000 EXCEPT SELECT job FROM Emps WHERE hiredate > '1-jan-82' PRESIDENT Set Difference eliminates duplicates before taking the difference Oracle uses MINUS in place of EXCEPT

30 30 © Ellis Cohen 2001-2009 EXCEPT ALL SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK What's the result of SELECT job FROM Emps WHERE sal >= 3000 EXCEPT ALL SELECT job FROM Emps WHERE hiredate > '1-jan-82' Not in Oracle

31 31 © Ellis Cohen 2001-2009 EXCEPT ALL vs EXCEPT SELECT job FROM Emps WHERE sal >= 3000 ANALYST PRESIDENT ANALYST SELECT job FROM Emps WHERE sal >= 3000 EXCEPT ALL SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST PRESIDENT SELECT job FROM Emps WHERE hiredate > '1-jan-82' ANALYST CLERK SELECT job FROM Emps WHERE sal >= 3000 EXCEPT SELECT job FROM Emps WHERE hiredate > '1-jan-82' PRESIDENT Set Difference eliminates duplicates before taking the difference Counted Difference counts # of tuples, computes difference & keeps that many Not in Oracle Oracle uses MINUS in place of EXCEPT Also called MultiSet Difference

32 32 © Ellis Cohen 2001-2009 Counted & Set Difference SELECT job FROM Emps1 EXCEPT SELECT job FROM Emps2 Equivalent SELECT DISTINCT job FROM Emps1 EXCEPT ALL SELECT DISTINCT job FROM Emps2 Oracle uses MINUS in place of EXCEPT Not in Oracle

33 33 © Ellis Cohen 2001-2009 Collection Operators & Joins

34 34 © Ellis Cohen 2001-2009 Write a SQL expression (without using Outer Joins) to get the same result as SELECT dname, ename FROM Depts NATURAL LEFT JOIN Emps Representing Outer Joins using Collection Operators Suppose SQL did not have an Outer Join operators. Could you use collection operators instead? DNAME ENAME ------------ ------ ACCOUNTING CLARK RESEARCH SALES ALLEN SALES BLAKE OPERATIONS 10ACCOUNTING… 20RESEARCH… 30SALES… 40OPERATIONS… deptno dname … Depts Emps * * empno ename … deptno 7782CLARK…10 7499ALLEN…30 8214JOJO… 7698BLAKE…30

35 35 © Ellis Cohen 2001-2009 Outer Join = Inner Join + Remainder DNAME ENAME ------------ ------ ACCOUNTING CLARK RESEARCH SALES ALLEN SALES BLAKE OPERATIONS 10ACCOUNTING… 20RESEARCH… 30SALES… 40OPERATIONS… deptno dname … Depts Emps * * empno ename … deptno 7782CLARK…10 7499ALLEN…30 8214JOAN… 7698BLAKE…30 DNAME ENAME ------------ ------ ACCOUNTING CLARK SALES ALLEN SALES BLAKE DNAME ENAME ------------ ------ RESEARCH OPERATIONS Inner Join Depts not in Inner Join

36 36 © Ellis Cohen 2001-2009 Representing Outer Joins using Collection Operators WITH EmptyDepts AS ( SELECT deptno FROM Depts EXCEPT SELECT deptno FROM Emps ) SELECT dname, ename FROM Emps NATURAL JOIN Depts UNION SELECT dname, NULL FROM EmptyDepts NATURAL JOIN Depts SELECT dname, ename FROM Depts NATURAL LEFT JOIN Emps Remaining Depts Inner Join Remaining Depts with NULL ename

37 37 © Ellis Cohen 2001-2009 Intersection & Inner Joins Given two tables, e.g. T1( a, b, c ) T2( a, b, c ) SELECT * FROM T1 INTERSECT SELECT * FROM T2 Assuming T1 and T2 have the same attributes Neither T1 nor T2 have duplicate tuples No matching tuples in T1 or T2 contain NULL values SELECT * (T1 NATURAL JOIN T2) Equivalent

38 38 © Ellis Cohen 2001-2009 Unions and Full Outer Joins Given two tables, e.g. T1( a, b, c ) T2( a, b, c ) SELECT * FROM T1 UNION SELECT * FROM T2 SELECT * (T1 NATURAL FULL JOIN T2) Equivalent Assuming T1 and T2 have the same attributes Neither T1 nor T2 have duplicate tuples No matching tuples in T1 or T2 contain NULL values

39 39 © Ellis Cohen 2001-2009 Transitive Closure & Recursive Views

40 40 © Ellis Cohen 2001-2009 Hierarchy Induced by Reflexive Relationships 7566 7788 7369 7876 7902 [mgr: 7902] 7839 7782 7698 KING JONES BLAKE CLARK FORD SMITH 7934 7499 7900 Hierarchy induced by manages relationship, implemented by the mgr attribute &x &x is the root of a subtree

41 41 © Ellis Cohen 2001-2009 1 st Level Query Exercise Write a query that lists the empno and ename of &x's as well as all the employees directly managed by &x 7566 7788 7902 JONES FORD &x Emps( empno, ename, mgr, … )

42 42 © Ellis Cohen 2001-2009 1 st Level Query Answer LEVEL 0 & 1: Find &x and 1 st level (i.e. direct) reports SELECT empno, ename FROM Emps WHERE empno = &x UNION SELECT empno, ename FROM Emps e WHERE mgr = &x

43 43 © Ellis Cohen 2001-2009 2 nd Level Query Exercise Write a query that lists the empno of &x's as well as all the employees directly managed by &x as well as all the employees they directly manage 7566 7788 7369 7876 7902 &x

44 44 © Ellis Cohen 2001-2009 2 nd Level Query Answer LEVEL 0 & 1: Find &x and 1 st level (i.e. direct) reports SELECT empno, ename FROM Emps WHERE empno = &x UNION SELECT empno, ename FROM Emps WHERE mgr = &x LEVEL 0 & 1 & 2: Find &x and 1 st and 2 nd level reports SELECT empno, ename FROM Emps WHERE empno = &x UNION SELECT empno, ename FROM Emps WHERE mgr = &x UNION SELECT e2.empno, e2.ename FROM Emps e1, Emps e2 WHERE e2.mgr = e1.empno AND e1.mgr = &x What if you want to go one level deeper?

45 45 © Ellis Cohen 2001-2009 3 rd Level Query Answer LEVEL 0 & 1 & 2 & 3: Find &x and 1 st, 2 nd and 3 rd level reports SELECT empno, ename FROM Emps WHERE e.empno = &x UNION SELECT empno, ename FROM Emps WHERE e.mgr = &x UNION SELECT e2.empno, e2.ename FROM Emps e1, Emps e2 WHERE e2.mgr = e1.empno AND e1.mgr = &x UNION SELECT e3.empno, e3.ename FROM Emps e1, Emps e2, Emps e3 WHERE e3.mgr = e2.empno AND e2.mgr = e1.empno AND e1.mgr = &x And another level deeper?

46 46 © Ellis Cohen 2001-2009 4th Level Query Answer LEVEL 0 & 1 & 2 & 3 & 4: Find &x and 1 st, 2 nd, 3 rd & 4 th level reports SELECT empno, ename FROM Emps WHERE e.empno = &x UNION SELECT empno, ename FROM Emps WHERE e.mgr = &x UNION SELECT e2.empno, e2.ename FROM Emps e1, Emps e2 WHERE e2.mgr = e1.empno AND e1.mgr = &x UNION SELECT e3.empno, e3.ename FROM Emps e1, Emps e2, Emps e3 WHERE e3.mgr = e2.empno AND e2.mgr = e1.empno AND e1.mgr = &x UNION SELECT e4.empno, e4.ename FROM Emps e1, Emps e2, Emps e3, Emps e4 WHERE e4.mgr = e3.empno AND e3.mgr = e2.empno AND e2.mgr = e1.empno AND e1.mgr = &x

47 47 © Ellis Cohen 2001-2009 Transitive Closure Find &x plus all employees who work under &x at any level in the hierarchy. Cannot be implemented directly using SQL learned so far! Can be implemented by recursive views, defined in SQL-99, but not available in commercial RDBs Oracle instead supports hierarchical queries

48 48 © Ellis Cohen 2001-2009 Recursive Views CREATE RECURSIVE VIEW Remps AS ( SELECT empno, ename, 0 AS level FROM Emps WHERE empno = &x UNION SELECT e.empno, e.ename, (1 + r.level) AS level FROM Remps r, Emps e WHERE r.empno = e.mgr) Recursive Views are in the SQL-99 standard, but not implemented in Oracle

49 49 © Ellis Cohen 2001-2009 Hierarchical Queries in Oracle Find x plus all employees who work under x along with their level in the hierarchy. 7566 7788 7369 7876 7902 SELECT empno, ename, level, prior empno, prior ename, FROM Emps START WITH empno = &x CONNECT BY mgr = prior empno Start with empno = &x [mgr: 7902] prior empno i.e. empno at the prior level of the hierarchy Current level of hierarchy being evaluated &x Oracle only


Download ppt "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2009 Collection Operators These slides are."

Similar presentations


Ads by Google