Top Banner
1 Advanced SQL Topics Edward Wu
42

1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

Mar 27, 2015

Download

Documents

Brian Corbett
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

1

Advanced SQL Topics

Edward Wu

Page 2: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

2

Lesson ObjectivesLearn how to create and use indexes

Create, Alter, and Drop Views

Outer / Self Join

Nested Queries

Useful Function (Decode, Case, Rownum)

Page 3: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

3

Database IndexesSimilar to an index in a book

Table with list of sorted data values and corresponding physical location

Used to speed searches

Primary key indexed automatically

Unlimited number allowed, but more indexes means more processing time for action queries (insert, update, delete)

Page 4: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

4

Creating an IndexCreate index after table data is loadedCREATE INDEX index_name ON tablename (index_fieldname);Convention for naming index: tablename_fieldname.

Page 5: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

5

Composite IndexContains multiple (up to 16) sorted columnsUsed for queries with multiple search conditionsCREATE INDEX index_name ON tablename(index_fieldname1, index_fieldname2, …);

Page 6: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

6

Viewing Index InformationUse data dictionary view USER_INDEXES

Page 7: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

7

Dropping an IndexIf an index is no longer needed or does not improve performance, delete it

DROP INDEX index_name;

Page 8: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

8

Use an Index WhenTable contains a large number of records (a rule of thumb is that a large table contains over 100,000 records)The field contains a wide range of valuesThe field contains a large number of NULL valuesApplication queries frequently use the field in a search condition or join conditionMost queries retrieve less than 2% to 4% of the table rows

Page 9: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

9

Do Not Use an Index WhenThe table does not contain a large number of recordsApplications do not use the proposed index field in a query search conditionMost queries retrieve more than 2% to 4% of the table recordsApplications frequently insert or modify table data

Page 10: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

10

Logical table based on a queryDoes not physically exist in the databasePresents data in a different format from underlying tablesUses:SecuritySimplifying complex queries

Database Views

Page 11: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

11

Creating a view:

CREATE VIEW view_name AS

SQL_command;

Views can be queried just like tables:

SELECT *

FROM view_name;

Database Views

Page 12: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

12

Simple Views Based on SQL query that retrieves data from only one table (limit data for different user)View can support all table DML operations: INSERTUPDATEDELETE

Page 13: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

13

Complex Views Based on query that retrieves data from multiple tables

Can only be used to support SELECT operationsNo DML operations supported

For reporting purposes. (Sales Report)

Can create index on view.

Page 14: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

14

Join QueriesRetrieve data from multiple tables by joining tables using foreign key references

Join query types:OuterSelf Inequality

Page 15: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

15

Inner Join with Group by Find the total inventory amount on hand for each item

Different price

Page 16: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

16

Outer JoinsLimitation of inner joins: some records may be omitted if corresponding records don’t exist in one of the tables

Example: retrieve records for all students, along with their corresponding ENROLLMENT information

Page 17: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

17

Outer Joins

Student 105 (Michael Connoly) does not have any ENROLLMENT records

Page 18: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

18

Outer JoinsNo records retrieved for Michael:

Page 19: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

19

Outer Joins To include records in first (inner)

table, even when they do not have matching records in second (outer) table, place outer join marker (+) beside outer table name in join clause

Page 20: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

20

Outer JoinsOuter join marker

Page 21: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

21

A B

A B

BA

A.id = B.id

(+)A.id = B.id

A.id = B.id(+)

Note: Orange is the area that is returned

Page 22: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

22

Self JoinsUsed to join a table to itself when the table has a hierarchical relationship

Page 23: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

23

Self JoinsTo create a self-join, you need to create a table alias, which gives an alternate name to the table so you can create a join conditionSyntax to create table alias in FROM clause:

FROM table1 alias1, table2 alias2

Page 24: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

24

Self JoinsP_ID PROJECT_NAME CLIENT_ID MGR_ID PARENT_P_ID

1 Hardware Support Intranet 2 105

2 Hardware Support Interface 2 103 1

3 Hardware Support Database 2 102 1

4 Teller Support System 4 105

5 Internet Advertising 6 105

6 Network Design 6 104 5

7 Exploration Database 5 102

PARENT_PROJECT

P_ID PROJECT_NAME CLIENT_ID MGR_ID PARENT_P_ID

1 Hardware Support Intranet 2 105

2 Hardware Support Interface 2 103 1

3 Hardware Support Database 2 102 1

4 Teller Support System 4 105

5 Internet Advertising 6 105

6 Network Design 6 104 5

7 Exploration Database 5 102

PROJECT

P_ID PROJECT_NAME CLIENT_ID MGR_ID PARENT_P_ID

1 Hardware Support Intranet 2 105

2 Hardware Support Interface 2 103 1

3 Hardware Support Database 2 102 1

4 Teller Support System 4 105

5 Internet Advertising 6 105

6 Network Design 6 104 5

7 Exploration Database 5 102

SUB_PROJECT

Page 25: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

25

Self Join Example

Page 26: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

26

Inequality JoinsJoin created by placing making join condition satisfy an inequality condition

Only makes sense when primary/foreign key values are not surrogate keys

Page 27: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

27

Inequality Joins

Page 28: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

28

Nested QueriesCreated when a subquery is nested within a main queryMain query: first query listed in SELECT

commandSubquery: retrieves one or more values

that specify the main query’s search condition

Page 29: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

29

Nested Query WhereSubquery Returns a Single Value

Syntax:SELECT column1, column2, …

FROM table1, table2, …

WHERE join conditions

AND search_column1 =(SELECT column1

FROM table1, table2, …

WHERE search and

join conditions)

Subquerythat returnsone value

Note: subquery is just another sql command contained within a parenthesis

Page 30: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

30

Example Nested Query WhereSubquery Returns a Single Value

Find the class that Amanda goes to.

Find all the student that goes to Amanda class

Page 31: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

31

Nested Query WhereSubquery Returns Multiple Values

Syntax:SELECT column1, column2, …

FROM table1, table2, …

WHERE join conditions

AND search_column1 IN (SELECT column1

FROM table1, table2, …

WHERE search and

join conditions)

Subquerythat returnsmultiple values

Page 32: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

32

Example Nested Query WhereSubquery Returns Multiple Values

Find all the course that Amanda enrolled in. which returned

c_sec_id: 1010, 1011

Find all the students that enrolled in c_sec_id in (1010, 1011)

Page 33: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

33

Example of nested queries (cont…)

Find the c_sec_id that use bldg_code=‘CR’ which is 1000, 1002, 1007, 1010

select c_sec_id from student s, enrollment e, course_section c where s.s_id = e.s_id and s_last = 'Mobley‘ and s_first = 'Amanda‘ and c_sec_id in (1000, 1002, 1007, 1010);

Page 34: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

34

Remember previous example of Inner Join with Group by

Find the total inventory amount on hand for each item

Different price

Page 35: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

35

Creating Table Alias from select statement

Add an extra column for the percentage of inventory on hand

Page 36: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

36

Performs set operations on outputs of two unrelated queries

Both queries must have:same number of display fieldscorresponding display fields must have

same data type

Using Set Operators in Queries

Page 37: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

37

UNION: combines results, suppresses duplicate rows

UNION ALL: combines results, displays duplicates

INTERSECT: finds matching rows

MINUS: returns the difference between returned record sets

Query Set Operators

Page 38: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

38

Decode FunctionFacilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement

SyntaxDecode(column/expression, search1, result1

[, search2, result2, …, ]

[, default ] );

Page 39: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

39

The Decode Function

The DECODE function decodes an expression in a way similar to the IF-THEN-ELSE logic used in various languages. The DECODE function decodes expression after comparing it to each search value. If the expression is the same as search, result is returned.

Note: If the default value is omitted, a null value is returned where a search value does not match any of the result value.

Page 40: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

40

Using the DECODE Function

Page 41: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

41

Row Num

Page 42: 1 Advanced SQL Topics Edward Wu. 2 Lesson Objectives Learn how to create and use indexes Create, Alter, and Drop Views Outer / Self Join Nested Queries.

42

Search Result for web page