ocl4 oracle 10g: sql & pl/sql session #4

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

Upload: sian

Post on 11-Feb-2016

123 views

Category:

Documents


0 download

DESCRIPTION

OCL4 Oracle 10g: SQL & PL/SQL Session #4. Matthew P. Johnson CISDD, CUNY June, 2005. Agenda. Evals Nulls Kinds of joins Set operations in SQL Grouping & Aggregation Updates Creating tables. New topic: Nulls in SQL. If we don’t have a value, can put a NULL - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

1

OCL4 Oracle 10g:SQL & PL/SQLSession #4

Matthew P. JohnsonCISDD, CUNY

June, 2005

Page 2: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

2

Agenda Evals Nulls Kinds of joins Set operations in SQL Grouping & Aggregation Updates Creating tables

Page 3: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

3

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

Page 4: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

4

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

Page 5: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

5

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=20height=NULLweight=180

SELECT *FROM PersonWHERE (age < 25) AND (height > 6 OR weight > 190)

Page 6: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

6

Comparing null and non-nulls Unexpected behavior:

Some Persons are not included! The “trichotomy law” does not hold!

SELECT *FROM PersonWHERE age < 25 OR age >= 25

Page 7: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

7

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 PersonWHERE age < 25 OR age >= 25 OR age IS NULL

Page 8: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

8

Null/logic review TRUE AND UNKNOWN = ?

TRUE OR UNKNOWN = ?

UNKNOWN OR UNKNOWN = ?

X = NULL = ?

Page 9: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

9

Example with nulls look at emp table

Select names, salaries, commissions, total salaries

What if commission is null? nvl

Page 10: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

10

Joins operations Variations:

Cross join (Cartesian product) Join … On Natural join Outer join

Apply to relations appearing in selections

Page 11: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

11

Cross join - exampleName Address Gender BirthdateHanks 123 Palm Rd M 01/01/60Taylor 456 Maple Av F 02/02/40Lucas 789 Oak St M 03/03/55

Name Address NetworthSpielberg 246 Palm Rd 10MTaylor 456 Maple Av 20MLucas 789 Oak St 30M

MovieStar

MovieExec

Page 12: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

12

Cross join – example

MovieStar.name

MovieStar.address

MovieStar. Gender

MovieStar.Birthdate

MovieExec. Name

MovieExec.Address

MovieExec. Networth

Hanks 123 Palm Rd M 01/01/60 Spielberg 246 Palm Rd 10M

Hanks 123 Palm Rd M 01/01/60 Taylor 456 Maple Av 20M

Hanks 123 Palm Rd M 01/01/60 Lucas 789 Oak St 30M

Taylor 456 Maple Av F 02/02/40 Spielberg 246 Palm Rd 10M

Taylor 456 Maple Av F 02/02/40 Taylor 456 Maple Av 20M

Taylor 456 Maple Av F 02/02/40 Lucas 789 Oak St 30M

Lucas 789 Oak St M 03/03/55 Spielberg 246 Palm Rd 10M

Lucas 789 Oak St M 03/03/55 Taylor 456 Maple Av 20M

Lucas 789 Oak St M 03/03/55 Lucas 789 Oak St 30M

Row

1

2

3

4

5

6

7

8

9

SELECT *FROM MovieStar CROSS JOIN MovieExec

Page 13: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

13

Join … On: example

MovieStar.name

MovieStar.address

MovieStar. Gender

MovieStar.Birthdate

MovieExec. Name

MovieExec.Address

MovieExec. Networth

Hanks 123 Palm Rd M 01/01/60 Spielberg 246 Palm Rd 10M

Hanks 123 Palm Rd M 01/01/60 Taylor 456 Maple Av 20M

Hanks 123 Palm Rd M 01/01/60 Lucas 789 Oak St 30M

Taylor 456 Maple Av F 02/02/40 Spielberg 246 Palm Rd 10M

Taylor 456 Maple Av F 02/02/40 Taylor 456 Maple Av 20M

Taylor 456 Maple Av F 02/02/40 Lucas 789 Oak St 30M

Lucas 789 Oak St M 03/03/55 Spielberg 246 Palm Rd 10M

Lucas 789 Oak St M 03/03/55 Taylor 456 Maple Av 20M

Lucas 789 Oak St M 03/03/55 Lucas 789 Oak St 30M

Row

1

2

3

4

5

6

7

8

9

SELECT *FROM MovieStar JOIN MovieExecON MovieStar.Name <> MovieExec.Name

Page 14: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

14

Natural JoinsMovieStar(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)

Page 15: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

15

Example - Natural joinName Address Gender BirthdateHanks 123 Palm Rd M 01/01/60Taylor 456 Maple Av F 02/02/40Lucas 789 Oak St M 03/03/55

Name Address NetworthSpielberg 246 Palm Rd 10MTaylor 456 Maple Av 20MLucas 789 Oak St 30M

MovieStar

MovieExec

Name Address Gender Birthdate Networth

Taylor 456 Maple Av F 02/02/40 20M

Lucas 789 Oak St M 03/03/55 30M

SELECT * FROM MovieStar NATURAL JOIN MovieExec

Page 16: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

16

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”

Page 17: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

17

Outer Join - ExampleSELECT * FROM MovieStar LEFT OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name

SELECT * FROM MovieStar RIGHT OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name

MS.name MS.address MS. Gender MSr.Birthdate ME. Name ME.Address ME. Networth

Hanks 123 Palm Rd M 01/01/60 Null Null null

Taylor 456 Maple Av F 02/02/40 Taylor 456 Maple Av 20M

Lucas 789 Oak St M 02/02/40 Lucas 789 Oak St 30M

Null Null Null null Spielberg 246 Palm Rd 10M

MS.name MS.address MS. Gender MSr.Birthdate ME. Name ME.Address ME. Networth

Hanks 123 Palm Rd M 01/01/60 Null Null null

Taylor 456 Maple Av F 02/02/40 Taylor 456 Maple Av 20M

Lucas 789 Oak St M 02/02/40 Lucas 789 Oak St 30M

Null Null Null null Spielberg 246 Palm Rd 10M

Page 18: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

18

Outer Join - Example

Name Address Gender Birthdate

Hanks 123 Palm Rd M 01/01/60

Taylor 456 Maple Av F 02/02/40

Lucas 789 Oak St M 03/03/55

Name Address Networth

Spielberg 246 Palm Rd 10M

Taylor 456 Maple Av 20M

Lucas 789 Oak St 30M

MovieStar MovieExec

SELECT * FROM MovieStar FULL OUTER JOIN MovieExec ON MovieStart.name=MovieExec.name

MS.name MS.address MS. Gender MSr.Birthdate ME. Name ME.Address ME. Networth

Hanks 123 Palm Rd M 01/01/60 Null Null null

Taylor 456 Maple Av F 02/02/40 Taylor 456 Maple Av 20M

Lucas 789 Oak St M 02/02/40 Lucas 789 Oak St 30M

Null Null Null null Spielberg 246 Palm Rd 10M

Page 19: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

19

New-style join syntax Old-style syntax simply lists tables separated

by commas:

New-style makes the join explicit:

SELECT *FROM A,BWHERE …;

SELECT *FROM A JOIN B ON …WHERE …;

Page 20: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

20

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

Page 21: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

21

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

Page 22: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

22

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

Page 23: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

23

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”

Page 24: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

24

Live examples Examples from sqlzoo.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

Page 25: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

25

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

Page 26: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

26

Review Examples from sqlzoo.net

SELECT LFROM R1, …, Rn

WHERE C

L(C(R1 x … Rn)

Page 27: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

27

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')

Page 28: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

28

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

Page 29: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

29

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88Ford 345 Palm M 7/7/77

R S:

Page 30: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

30

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

R S: Name Address Gender BirthdateFisher 123 Maple F 9/9/99

Page 31: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

31

Set operations - exampleName Address Gender BirthdateFisher 123 Maple F 9/9/99Hamill 456 Oak M 8/8/88

Name Address Gender BirthdateFisher 123 Maple F 9/9/99Ford 345 Palm M 7/7/77

R:

S:

R - S: Name Address Gender BirthdateHamill 456 Oak M 8/8/88

Page 32: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

32

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 differences

These ops applied to queries:

(SELECT name FROM Person WHERE City = 'New York')

INTERSECT(SELECT custname FROM Purchase WHERE store='Kim''s')

Page 33: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

33

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats or green boats

SELECT DISTINCT ssnFROM reserveWHERE color = 'red' OR color = 'green'

Page 34: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

34

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats and green boats

SELECT DISTINCT ssnFROM reserveWHERE color = 'red' AND color = 'green'

Page 35: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

35

Boat examples Reserve(ssn,bmodel,color)

Q: Find ssns of sailors who reserved red boats and green boats

SELECT DISTINCT r1.ssnFROM reserve r1, reserve r2WHERE r1.ssn = r2.ssn AND r1.color = 'red' AND r2.color = 'green'

Page 36: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

36

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')

Page 37: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

37

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')

Page 38: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

38

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')

Page 39: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

39

(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

Page 40: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

40

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)

Result(name, address)

Page 41: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

41

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 differences

Page 42: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

42

Confession

Relations aren’t really sets!

They’re bags!

Page 43: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

43

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?

Page 44: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

44

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}

Page 45: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

45

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

Page 46: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

46

First (?) Unintuitive SQLism Looking for R (S T)

But what happens if T is empty?

See transcript of this in Oracle on sales

SELECT R.AFROM R, S, TWHERE R.A=S.A OR R.A=T.A

Page 47: OCL4 Oracle 10g: SQL & PL/SQL Session #4

Matthew P. Johnson, OCL4, CISDD CUNY, Sept 2005

47

Labs…

Tomorrow: Grouping/Aggregation PL/SQL