Matthew P. Johnson, OCL4, CISDD CUNY, Sept 20051 OCL4 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Chapter (7): Advanced SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
Algebraic and Logical Query Languages Spring 2011 Instructor: Hassan Khosravi.
1 Lecture 12: Further relational algebra, further SQL
Relational Operations on Bags Extended Operators of Relational Algebra.
Subqueries Example Find the name of the producer of ‘Star Wars’.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #9 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2008.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #9 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
CMSC424: Database Design Instructor: Amol Deshpande
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY Fall, 2004.
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #7 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
CMSC424: Database Design Instructor: Amol Deshpande
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #11 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
1 Views. 2 What are views good for?(1) Simplifying complex queries: we saw one example. Here is another example that allows the user to "pretend" that.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 M.P. Johnson Stern School of Business, NYU Spring, 2008.
Relational Operations on Bags Extended Operators of Relational Algebra.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #13 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
1 Views. 2 What are views good for? (1) Simplifying complex queries: We saw one example. Here is another that allows the user to "pretend" that there.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #11 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Matthew P. Johnson, OCL3, CISDD CUNY, June OCL3 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY June, 2005.
Relational Algebra.
1 Database Systems Lecture #6 Yan Pan School of Software, SYSU 2011.
1 Database Systems Lecture #7 Yan Pan School of Software, SYSU 2011.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Advanced Relational Algebra & SQL (Part1 )
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
More Relation Operations 2015, Fall Pusan National University Ki-Joune Li.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CMPT 258 Database Systems SQL Queries (Chapter 5).
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 9 A First Course in Database Systems.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
More SQL: Complex Queries,
Lecture 05: SQL Wednesday, January 12, 2005.
Chap 5. The DB Language (SQL)
SQL The Query Language R & G - Chapter 5
Introduction to Database Systems CSE 444 Lecture 04: SQL
The Relational Algebra and Relational Calculus
CS4222 Principles of Database System
Lectures 7: Introduction to SQL 6
Presentation transcript:

Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #4 Matthew P. Johnson CISDD, CUNY June, 2005

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Agenda Evals Nulls Kinds of joins Set operations in SQL Grouping & Aggregation Updates Creating tables

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Nulls in SQL If we don’t have a value, can put a NULL Null can mean several things:  Value does not exists  Value exists but is unknown  Value not applicable The schema specifies whether null is allowed for each attribute  NOT NULL if not allowed  By default, null is allowed

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Null Values x = NULL  4*(3-x)/7 = NULL x = NULL  x + 3 – x = NULL x = NULL  3 + (x-x) = NULL x = NULL  x = 'Joe' is UNKNOWN In general: no row using null fields appear in the selection test will pass the test  With one exception Pace Boole, SQL has three boolean values:  FALSE=0  TRUE=1  UNKNOWN=0.5

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Null values in boolean expressions C1 AND C2= min(C1, C2) C1 OR C2= max(C1, C2) NOT C1= 1 – C1 height > 6 = UNKNOWN  UNKNOWN OR weight > 190 = UNKOWN  (age < 25) AND UNKNOWN = UNKNOWN E.g.: age=20 height=NULL weight=180 SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190) SELECT * FROM Person WHERE (age < 25) AND (height > 6 OR weight > 190)

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Comparing null and non-nulls Unexpected behavior: Some Persons are not included! The “trichotomy law” does not hold! SELECT * FROM Person WHERE age = 25 SELECT * FROM Person WHERE age = 25

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Testing for null values Can test for NULL explicitly:  x IS NULL  x IS NOT NULL But:  x = NULL is always unknown Now it includes all Persons SELECT * FROM Person WHERE age = 25 OR age IS NULL SELECT * FROM Person WHERE age = 25 OR age IS NULL

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Null/logic review TRUE AND UNKNOWN = ? TRUE OR UNKNOWN = ? UNKNOWN OR UNKNOWN = ? X = NULL = ?

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Example with nulls look at emp table  Select names, salaries, commissions, total salaries  What if commission is null?  nvl

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Joins operations Variations:  Cross join (Cartesian product)  Join … On  Natural join  Outer join Apply to relations appearing in selections

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Cross join - example NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av20M Lucas789 Oak St30M MovieStar MovieExec

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Cross join – example MovieS tar.nam e MovieStar.add ress MovieSta r. Gender MovieStar.Birthdate MovieEx ec. Name MovieExec.Ad dress MovieEx ec. Networth Hanks123 Palm RdM01/01/60Spielberg246 Palm Rd10M Hanks123 Palm RdM01/01/60Taylor456 Maple Av20M Hanks123 Palm RdM01/01/60Lucas789 Oak St30M Taylor456 Maple AvF02/02/40Spielberg246 Palm Rd10M Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Taylor456 Maple AvF02/02/40Lucas789 Oak St30M Lucas789 Oak StM03/03/55Spielberg246 Palm Rd10M Lucas789 Oak StM03/03/55Taylor456 Maple Av20M Lucas789 Oak StM03/03/55Lucas789 Oak St30M Row SELECT * FROM MovieStar CROSS JOIN MovieExec SELECT * FROM MovieStar CROSS JOIN MovieExec

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Join … On: example MovieSt ar.name MovieStar.addr ess MovieS tar. Gender MovieStar. Birthdate MovieExec. Name MovieExec.Add ress MovieEx ec. Networth Hanks123 Palm RdM01/01/60Spielberg246 Palm Rd10M Hanks123 Palm RdM01/01/60Taylor456 Maple Av20M Hanks123 Palm RdM01/01/60Lucas789 Oak St30M Taylor456 Maple AvF02/02/40Spielberg246 Palm Rd10M Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Taylor456 Maple AvF02/02/40Lucas789 Oak St30M Lucas789 Oak StM03/03/55Spielberg246 Palm Rd10M Lucas789 Oak StM03/03/55Taylor456 Maple Av20M Lucas789 Oak StM03/03/55Lucas789 Oak St30M Row SELECT * FROM MovieStar JOIN MovieExec ON MovieStar.Name <> MovieExec.Name SELECT * FROM MovieStar JOIN MovieExec ON MovieStar.Name <> MovieExec.Name

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Natural Joins MovieStar(name, address, gender, birthdate) MovieExec(name, address, networth) Natural Join: …MovieStar Natural Join MovieExec Results in: list of individuals who are movie-stars as well as executives: (Name, address, gender, birthdate, networth)

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Example - Natural join NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av20M Lucas789 Oak St30M MovieStar MovieExec NameAddressGenderBirthdateNetworth Taylor456 Maple AvF02/02/4020M Lucas789 Oak StM03/03/5530M SELECT * FROM MovieStar NATURAL JOIN MovieExec

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Outerjoin Like L R except that dangling tuples are included, padded with nulls Left outerjoin: dangling tuples from left are included  Nulls appear “on the right” Right outerjoin: dangling tuples from right are included  Nulls appear “on the left”

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Outer Join - Example SELECT * FROM MovieStar LEFT OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name SELECT * FROM MovieStar RIGHT OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name MS.nameMS.addressMS. GenderMSr.BirthdateME. NameME.AddressME. Networth Hanks123 Palm RdM01/01/60Null null Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Lucas789 Oak StM02/02/40Lucas789 Oak St30M Null nullSpielberg246 Palm Rd10M MS.nameMS.addressMS. GenderMSr.BirthdateME. NameME.AddressME. Networth Hanks123 Palm RdM01/01/60Null null Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Lucas789 Oak StM02/02/40Lucas789 Oak St30M Null nullSpielberg246 Palm Rd10M

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Outer Join - Example NameAddressGenderBirthdate Hanks123 Palm RdM01/01/60 Taylor456 Maple AvF02/02/40 Lucas789 Oak StM03/03/55 NameAddressNetworth Spielberg246 Palm Rd10M Taylor456 Maple Av20M Lucas789 Oak St30M MovieStarMovieExec SELECT * FROM MovieStar FULL OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name MS.nameMS.addressMS. GenderMSr.BirthdateME. NameME.AddressME. Networth Hanks123 Palm RdM01/01/60Null null Taylor456 Maple AvF02/02/40Taylor456 Maple Av20M Lucas789 Oak StM02/02/40Lucas789 Oak St30M Null nullSpielberg246 Palm Rd10M

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New-style join syntax Old-style syntax simply lists tables separated by commas: New-style makes the join explicit: SELECT * FROM A,B WHERE …; SELECT * FROM A,B WHERE …; SELECT * FROM A JOIN B ON … WHERE …; SELECT * FROM A JOIN B ON … WHERE …;

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New-style join syntax Functionally equivalent to old-style, but perhaps more elegant Introduced in Oracle 8i, MySQL 3.x/4.x Older versions / other DBMSs may only support old-style syntax

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New-style join types cross joins (simplest):  …FROM A CROSS JOIN B Inner joins (regular joins):  …FROM A [INNER] JOIN B ON … Natural join:  …FROM A NATURAL JOIN B;  Joins on common fields and merges Outer joins

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New-style outer joins Outer joins may be left, right, or middle  FROM A LEFT [OUTER] JOIN B;  FROM A RIGHT [OUTER] JOIN B;  FROM A FULL [OUTER] JOIN B; “OUTER” is optional If “OUTER” is included, then “FULL” is the default Q: How to remember left v. right? A: It indicates the side whose rows are always included

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Old-style outer joins in Oracle Outer joins can also be done with the old-style syntax, but with the (+) …WHERE A.att=B.att(+) corresponds to: …FROM A LEFT JOIN B; The (+) is applied to all B attributes referred to in the WHERE clause Q: How to remember which side gets the (+)? A: The side that gets null rows “added”

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Live examples Examples from sqlzoo.netsqlzoo.net Q: produce a list of employees and their bosses  What if no boss? Or no subordinate? Joins on emp, emp man:  Comma-based  Inner  Natural  Cross  Outer – left, right, full

Matthew P. Johnson, OCL4, CISDD CUNY, Sept More live examples Inner joins require an ON clause  Like a where clause  Arbitrary boolean expression  If always true (1=1), reduces to cross join New compar op: BETWEEN  a between 5 and 10  a >= 5 and a <= 10 Q: produce a list of employees with their salary grades  emp, salgrade

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C  L (  C (R 1 x … R n )

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: Set/bag ops in SQL Orthodox SQL has set operators:  UNION, INTERSECT, EXCEPT And bag operators:  UNION ALL, INTERSECT ALL, EXCEPT ALL These operators are applied to queries: (SELECT name FROM Person WHERE City='New York') UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store='The Wiz') (SELECT name FROM Person WHERE City='New York') UNION (SELECT name FROM Person, Purchase WHERE buyer=name AND store='The Wiz')

Matthew P. Johnson, OCL4, CISDD CUNY, Sept New topic: R.A./SQL Set Operators Relations are sets  have set-theoretic ops  Venn diagrams Union: R1  R2 Example:  ActiveEmployees  RetiredEmployees Difference: R1 – R2 Example:  AllEmployees – RetiredEmployees = ActiveEmployees Intersection: R1  R2 Example:  RetiredEmployees  UnionizedEmployees

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 Ford345 PalmM7/7/77 R  S:

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R  S: NameAddressGenderBirthdate Fisher123 MapleF9/9/99

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Set operations - example NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Hamill456 OakM8/8/88 NameAddressGenderBirthdate Fisher123 MapleF9/9/99 Ford345 PalmM7/7/77 R: S: R - S: NameAddressGenderBirthdate Hamill456 OakM8/8/88

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Set ops in SQL Orthodox SQL has set operators:  UNION, INTERSECT, EXCEPT  Oracle SQL uses MINUS rather than EXCEPT  See the Ullman page on more differencesUllman These ops applied to queries: (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s') (SELECT name FROM Person WHERE City = 'New York') INTERSECT (SELECT custname FROM Purchase WHERE store='Kim''s')

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' OR color = 'green'

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green' SELECT DISTINCT ssn FROM reserve WHERE color = 'red' AND color = 'green'

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green' SELECT DISTINCT r1.ssn FROM reserve r1, reserve r2 WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green'

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats and green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') INTERSECT(SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats or green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') UNION (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Boat examples Reserve(ssn,bmodel,color) Q: Find ssns of sailors who reserved red boats but not green boats (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green') (SELECT DISTINCT ssn FROM reserve WHERE color = 'red') EXCEPT (SELECT DISTINCT ssn FROM reserve WHERE color = 'green')

Matthew P. Johnson, OCL4, CISDD CUNY, Sept (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name FROM Cust2) Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: list of all customer names and addresses (if known) Can’t do: Both tables must have same sequence of types  Applies to all set ops

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Union-Compatibility Situation: Cust1(name,address,…), Cust2(name,…) Want: list of all customer names and addresses (if known) But can do: Resulting field names taken from first table (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) (SELECT name, address FROM Cust1) UNION (SELECT name, '(N/A)' FROM Cust2) Result(name, address)

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Set/bag ops in Oracle SQL Oracle SQL support uses MINUS rather than EXCEPT Oracle SQL supports bag op UNION ALL but not INTERSECT ALL or MINUS ALL See the Ullman page on more differencesUllman

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Confession Relations aren’t really sets! They’re bags!

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Bag theory SELECT/WHERE: no duplicate elimination Cross, join: no duplicate elimination  |R1xR2| = |R1|*|R2| Can convert to sets when necessary  DISTINCT Allowing duplicates by default is cheaper  Union  Projection How hard is removing duplicates?

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Bag theory Bags: like sets but elements may repeat  “multisets” Set ops change somewhat when applied to bags  intuition: pretend identical elements are distinct {a,b,b,c}  {a,b,b,b,e,f,f} = {a,a,b,b,b,b,b,c,e,f,f} {a,b,b,b,c,c} – {b,c,c,c,d} = {a,b,b} {a,b,b,b,c,c}  {b,c,c,c,d} = {b,c,c}

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Some surprises in bag theory Be careful about your set theory laws – not all hold in bag theory (R  S) – T = (R – T)  (S – T)  always true in set theory  But true in bag theory?  suppose x is in R, S and T

Matthew P. Johnson, OCL4, CISDD CUNY, Sept First (?) Unintuitive SQLism Looking for R  (S  T) But what happens if T is empty? See transcript of this in Oracle on salestranscript SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A SELECTR.A FROM R, S, T WHERER.A=S.A OR R.A=T.A

Matthew P. Johnson, OCL4, CISDD CUNY, Sept Labs… Tomorrow:  Grouping/Aggregation  PL/SQL