62
7 1 Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel

Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

7

1

Chapter 7

Advanced SQL

Database Systems:Design, Implementation, and Management,

Sixth Edition, Rob and Coronel

Page 2: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

2

In this chapter, you will learn:

•About the relational set operators UNION,UNION ALL, INTERSECT, and MINUS

•How to use the advanced SQL JOIN operatorsyntax

•About the different types of subqueries andcorrelated queries

•How to use SQL functions to manipulate dates,strings, and other data

Page 3: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

3

In this chapter, you will learn: (continued)

•How to create and use updatable views

•How to create and use triggers and storedprocedures

•How to create embedded SQL

Page 4: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

4

UNION Query Result

Page 5: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

5

UNION ALL Query Result

Page 6: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

6

INTERSECT Query Result

Page 7: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

7

MINUS Query Results

Page 8: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

8

INTERSECT Alternative

Page 9: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

9

MINUS Alternative

Page 10: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

10

SQL Join Expression Styles

Page 11: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

11

NATURAL JOIN Result

Page 12: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

12

JOIN USING Result

Page 13: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

13

JOIN ON Result

Page 14: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

14

Outer Joins

•Returns not only rows matching join conditionbut also rows with unmatched values

•Three types:

–Left

–Right

–Full

Page 15: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

15

LEFT JOIN Result

Page 16: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

16

RIGHT JOIN Result

Page 17: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

17

FULL JOIN Result

Page 18: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

18

SELECT Subquery Examples

Page 19: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

19

WHERE Subquery Examples

Page 20: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

20

IN Subquery Example

Page 21: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

21

HAVING Subquery Example

Page 22: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

22

Multirow Subquery Operator Example

Page 23: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

23

FROM Subquery Example

Page 24: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

24

Inline Subquery Example

Page 25: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

25

Correlated Subquery Examples

Page 26: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

26

EXISTS Correlated Subquery Examples

Page 27: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

27

Selected MS Access/SQL ServerDate/Time Functions

Page 28: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

28

Selected Oracle Date/Time Functions

Page 29: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

29

Selected Oracle Date/Time Functions(continued)

Page 30: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

30

Selected Oracle Numeric Functions

Page 31: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

31

Selected Oracle String Functions

Page 32: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

32

Selected Oracle Conversion Functions

Page 33: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

33

Selected Oracle Conversion Functions(continued)

Page 34: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

34

Oracle Sequence

Page 35: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

35

Oracle Sequence Examples

Page 36: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

36

The PRODMASTERand PRODSALES Tables

Page 37: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

37

The Oracle UPDATE Error Message

Page 38: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

38

Creating an Updatable View in Oracle

Page 39: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

39

PRODMASTER Table Update,Using an Updatable View

Page 40: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

40

Anonymous PL/SQL Block Examples

Page 41: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

41

SHOW ERRORS

•Can help diagnose errors found in PL/SQLblocks

•Yields additional debugging informationwhenever an error is generated after anPL/SQL block is created or executed

Page 42: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

42

Anonymous PL/SQL Blockwith Variables and Loops

Page 43: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

43

PL/SQL Basic Data Types

Page 44: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

44

The PRODUCT Table

Page 45: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

45

Creating theTRG_PRODUCT_REORDER Trigger

Page 46: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

46

Verifying the TRG_PRODUCT_REORDERTrigger Execution

Page 47: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

47

The P_REORDER Value Mismatch AfterUpdate of the P_MIN Attribute

Page 48: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

48

Stored Procedures: Advantages

•Substantially reduce network traffic andincrease performance

•No transmission of individual SQL statementsover network

•Help reduce code duplication by means ofcode isolation and code sharing

•Minimize chance of errors and cost ofapplication development and maintenance

Page 49: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

49

Creating the PRC_PROD_DISCOUNTStored Procedure

Page 50: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

50

Results of the PRC_PROD_DISCOUNTStored Procedure

Page 51: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

51

The PRC_CUS_ADD Stored Procedure

Page 52: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

52

The PRC_INV_ADD andPRC_LINE_ADD Stored Procedures

Page 53: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

53

Testing the PRC_INV_ADD andPRC_LINE_ADD Procedures

Page 54: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

54

Cursor Processing Commands

Page 55: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

55

Cursor Attributes

Page 56: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

56

SQL and Procedural Languages:Key Differences

•Run-time mismatch:–SQL executed one instruction at a time–Host language typically runs at the client side

in its own memory space

•Processing mismatch:–Host language processes one data element at

a time

•Data type mismatch:–Data types may not match

Page 57: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

57

Embedded SQL Framework

•A standard syntax to identify embedded SQLcode within host language

•A standard syntax to identify host variables

•A communication area used to exchangestatus and error information between SQLand the host language

Page 58: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

58

SQL Status and Error Reporting Variables

Page 59: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

59

Static SQL

•Embedded SQL in which the programmerused predefined SQL statements andparameters

–End users of programs are limited to actionsthat were specified in application programs

•SQL statements will not change whileapplication is running

Page 60: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

60

Dynamic SQL

•SQL statement is not known in advance, butinstead is generated at run time

•Program can generate SQL statements at runtime that are required to respond to ad hocqueries

•Attribute list and the condition are not knownuntil the end user specifies them

•Tends to be much slower than static SQL•Requires more computer resources

Page 61: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

61

Summary

•SQL provides relational set operators tocombine output of two queries to generate newrelation

•Operations that join tables can be classified asinner joins and outer joins

•Subqueries and correlated queries are usedwhen it is necessary to process data based onother processed data

•SQL functions are used to extract or transformdata

Page 62: Chapter 7 · 2018. 9. 1. · Microsoft PowerPoint - chapter07.ppt Author: Gwo-Jen Hwang Created Date: 2004082511406

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

7

62

Summary (continued)

•Oracle sequences may be used to generatevalues to be assigned to a record

•PL/SQL may be used to create triggers,stored procedures, and PL/SQL functions

•If SQL statements are designed to returnmore than one value inside the PL/SQL code,a cursor is needed

•Embedded SQL refers to the use of SQLstatements within an applicationprogramming language