david m. kroenke and david j. auer database...

60
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application Processing 7-1 KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Upload: others

Post on 23-Mar-2020

10 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

David M. Kroenke and David J. Auer

Database Processing Fundamentals, Design, and Implementation

Chapter Seven: SQL for Database

Construction and Application Processing

7-1

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 2: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Chapter Objectives • To create and manage table structures using SQL statements • To understand how referential integrity actions are

implemented in SQL statements • To create and use SQL constraints • To understand several uses for SQL views • To use SQL statements to create and use views • To gain an understanding of how SQL is used in an

application program • To understand SQL/Persistent Stored Modules (SQL/PSM) • To understand how to create and use triggers • To understand how to create and use stored procedures

7-2

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 3: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

View Ridge Gallery

• View Ridge Gallery is a small art gallery that has been in business for 30 years.

• It sells contemporary European and North American fine art.

• View Ridge has one owner, three salespeople, and two workers.

• View Ridge owns all of the art that it sells; it holds no items on a consignment basis.

7-3

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 4: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Application Requirements

• View Ridge application requirements: – Track customers and their artist interests – Record gallery’s purchases – Record customers’ art purchases – List the artists and works that have appeared

in the gallery – Report how fast an artist’s works have sold

and at what margin – Show current inventory in a Webpage

7-4

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 5: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

View Ridge Gallery Database Design

7-5

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 6: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL DDL and DML

7-6

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 7: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Creating the VRG Database

7-7

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 8: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

CREATE TABLE

• CREATE TABLE statement is used for creating relations.

• Each column is described with three parts: column name, data type, and optional constraints.

• Example

7-8

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 9: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Data Types: SQL Server 2008 R2 Data Types

7-9

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 10: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Data Types: Oracle Database 11g Data Types

7-10

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 11: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Data Types: MySQL 5.5 Data Types I

7-11

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 12: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Data Types: MySQL 5.5 Data Types II

7-12

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 13: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Constraints • Constraints can be defined within the CREATE

TABLE statement, or they can be added to the table after it is created using the ALTER table statement.

• Five types of constraints: – PRIMARY KEY may not have null values – UNIQUE may have null values – NULL/NOT NULL – FOREIGN KEY – CHECK

7-13

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 14: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Creating Relationships I

7-14

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 15: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Creating Relationships II

7-15

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 16: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Creating Relationships III

7-16

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 17: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Implementing Cardinalities

7-17

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 18: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Default Values and Data Constraints

7-18

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 19: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL for Constraints

7-19

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 20: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

ALTER TABLE Statement

• ALTER TABLE statement changes table structure, properties, or constraints after it has been created.

• Example ALTER TABLE ASSIGNMENT ADD CONSTRAINT EmployeeFK FOREIGN KEY (EmployeeNumber) REFERENCES EMPLOYEE (EmployeeNumber) ON UPDATE CASCADE ON DELETE NO ACTION;

7-20

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 21: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Adding and Dropping Columns

• The following statement will add a column named MyColumn to the CUSTOMER table: ALTER TABLE CUSTOMER ADD MyColumn Char(5) NULL;

• You can drop an existing column with the statement: ALTER TABLE CUSTOMER DROP COLUMN MyColumn;

7-21

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 22: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Adding and Dropping Constraints

• ALTER TABLE can be used to add a constraint as follows: ALTER TABLE CUSTOMER ADD CONSTRAINT MyConstraint CHECK (LastName NOT IN ('Robert-No-Pay'));

• ALTER TABLE can be used to drop a constraint: ALTER TABLE CUSTOMER DROP CONSTRAINT MyConstraint;

7-22

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 23: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Removing Tables

• SQL DROP TABLE: DROP TABLE TRANS;

• If there are constraints:

ALTER TABLE CUSTOMER_ARTIST_INT DROP CONSTRAINT Customer_Artist_Int_CustomerFK;

ALTER TABLE TRANS DROP CONSTRAINT TransactionCustomerFK; DROP TABLE CUSTOMER;

7-23

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 24: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Removing Data Only

• SQL TRUNCATE TABLE: TRUNCATE TABLE TRANS;

• Cannot be used with a table that is referenced by a foreign key constraint.

7-24

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 25: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL DML—INSERT

• SQL INSERT statement: INSERT INTO ARTIST (LastName, FirstName, Nationality, DateOfBirth, DateDeceased)

VALUES ('Tamayo', ‘Rufino', 'Mexican', 1899, 1991);

• Bulk INSERT: INSERT INTO ARTIST (LastName, FirstName, Nationality, DateOfBirth) SELECT LastName, FirstName, Nationality, DateOfBirth FROM IMPORTED_ARTIST;

7-25

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 26: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL DML—UPDATE

• SQL UPDATE statement: UPDATE CUSTOMER SET City = 'New York City' WHERE CustomerID = 1000;

• Bulk UPDATE: UPDATE CUSTOMER SET AreaCode = '333' WHERE City = 'Denver';

7-26

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 27: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL DML—MERGE

• SQL MERGE command: MERGE INTO ARTIST AS A USING ARTIST_DATA AS ADR ON (A.LastName = ADR.LastName AND A.FirstName = ADR.FirstName WHEN MATCHED THEN UPDATE SET A.Nationality = ADR.Nationality WHEN NOT MATCHED THEN INSERT (LastName, FirstName, Nationality);

7-27

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 28: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL DML—DELETE

• SQL DELETE statement: DELETE FROM CUSTOMER WHERE CustomerID = 1000;

• If you omit the WHERE clause, you will delete every row in the table.

7-28

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 29: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

JOIN ON Syntax

• JOIN ON syntax: SELECT CUSTOMER.Name, ARTIST.Name FROM CUSTOMER JOIN CUSTOMER_ARTIST_INT ON CUSTOMER.CustomerID = CUSTOMER_ARTIST_INT.CustomerID

JOIN ARTIST ON CUSTOMER_ARTIST_INT.ArtistID = ARTIST.ArtistID;

7-29

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 30: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Using Aliases

• Use of aliases: SELECT C.Name, A.Name FROM CUSTOMER AS C JOIN CUSTOMER_ARTIST_INT AS CI ON C.CustomerID = CI.CustomerID JOIN ARTIST AS A ON CI.ArtistID = A.ArtistID;

• DBMS products differ CUSTOMER AS C versus CUSTOMER C

7-30

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 31: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—CUSTOMER I

7-31

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 32: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—CUSTOMER II

7-32

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 33: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—ARTIST

7-33

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 34: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data CUSTOMER_ARTIST_INT

7-34

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 35: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—WORK I

7-35

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 36: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—WORK II

7-36

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 37: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—TRANS I

7-37

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 38: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

VRG Data—TRANS II

7-38

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 39: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Outer Joins

• Left Outer Join: SELECT C.LastName, C.FirstName, A.LastName AS ArtistName FROM CUSTOMER C LEFT JOIN CUSTOMER_ARTIST_INT CI ON C.CustomerID = CI.CustomerID LEFT JOIN ARTIST A ON CI.ArtistID = A.ArtistID;

7-39

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 40: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Result of Left Outer Join

7-40

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 41: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL Views • An SQL view is a virtual table that is constructed from

other tables or views. • It has no data of its own, but obtains data from tables or

other views. • SELECT statements are used to define views:

– A view definition may not include an ORDER BY clause.

• SQL views are a subset of the external views: – They can be used only for external views that involve one

multivalued path through the schema.

7-41

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 42: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL Views

7-42

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 43: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

CREATE VIEW Command

• CREATE VIEW command: CREATE VIEW CustomerNameView AS SELECT LastName AS CustomerLastName, FirstName AS CustomerFirstName, FROM CUSTOMER;

• Results: SELECT * FROM CustomerNameView ORDER BY CustomerLastName, CustomerFirstName;

7-43

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 44: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Updateable Views

7-44

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 45: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Embedding SQL in Program Code

• SQL cursors are used to select one row at a time from pseudo-files.

• Problem: assigning SQL table columns with program variables

• Solution: object-oriented programming, PL/SQL • Problem: paradigm mismatch between SQL and

application programming language: – SQL statements return sets of rows; an application works on one

row at a time

• Solution: process the SQL results as pseudo-files

7-45

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 46: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL Cursors in Program Code

• SQL can be embedded in triggers, stored procedures, and program code.

• A typical cursor code pattern is: DECLARE SQLCursor CURSOR FOR (SELECT * FROM CUSTOMER); OPEN SQLCursor: MOVE SQLCursorer to first row WHILE (SQLCursor not past last row) LOOP {SQL action statements go here} REPERT LOOP UNTIL DONE; CLOSE SQLCursor;

7-46

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 47: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

SQL/Persistent Stored Modules (SSL/PSM)

• SQL/Persistent Stored Modules (SQL/PSM) is an ANSI/ISO standard for embedding procedural programming functionality into SQL

• Each DBMS product implements SQL/PSM in a different way, with some closer to the standard than others. – Microsoft SQL Server 2008 R2 calls its version Transact-SQL

(T-SQL) – Oracle Database 11g calls its variant Procedural

Language/SQL (PL/SQL – MySQL implements SQL/PSM, but has no special name for its

variant of SQL

7-47

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 48: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Triggers I • A trigger is a stored program that is executed by the

DBMS whenever a specified event occurs on a specified table or view.

• Three trigger types: BEFORE, INSTEAD OF, and AFTER – Each type can be declared for Insert, Update, and Delete. – Resulting in a total of nine trigger types.

• Oracle supports all nine trigger types. • SQL Server supports six trigger types (INSTEAD OF and

AFTER). • MySQL supports six trigger types (BEFORE and

AFTER). 7-48

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 49: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Triggers II

7-49

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 50: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Firing Triggers • When a trigger is fired, the DBMS supplies:

– Old and new values for the update – New values for inserts – Old values for deletions

• The way the values are supplied depends on the DBMS product.

• Trigger applications include: – Providing default values – Enforce data constraints – Updating views – Performing referential integrity actions

7-50

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 51: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-51

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 52: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-52

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 53: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-53

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 54: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Stored Procedures • A stored procedure is a program that is stored within the

database and is compiled when used. – In Oracle, it can be written in PL/SQL or Java. – In SQL Server, it can be written in TRANSACT-SQL.

• Stored procedures can receive input parameters and they can return results.

• Stored procedures can be called from: – Programs written in standard languages, e.g., Java, C#. – Scripting languages, e.g., JavaScript, VBScript. – SQL command prompt, e.g., SQL Plus, Query Analyzer.

7-54

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 55: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Stored Procedure Advantages

• Greater security as store procedures are always stored on the database server

• Decreased network traffic • SQL can be optimized by the DBMS compiler • Code sharing resulting in:

– Less work – Standardized processing – Specialization among developers

7-55

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 56: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-56

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 57: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-57

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 58: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

Triggers vs. Stored Procedures

7-58

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 59: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

David Kroenke and David Auer

Database Processing Fundamentals, Design, and Implementation

(12th Edition)

End of Presentation: Chapter Seven

7-59

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall

Page 60: David M. Kroenke and David J. Auer Database Processingdb.dongguk.ac.kr/dbp2013/kroenke_dbp12_pp_ch07.pdf · 2013-03-04 · View Ridge Gallery • View Ridge Gallery is a small art

7-60

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic,

mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America.

Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition © 2012 Pearson Prentice Hall