Top Banner
PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T - SQL) Database Programming Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 7
32

Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

Sep 16, 2018

Download

Documents

hoangngoc
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: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

PROCEDURAL DATABASE PROGRAMMING

( PL/SQL AND T-SQL)

Database Programming

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Class 7

Page 2: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

AGENDA

9. Database Triggers

9.1 Overview of Triggers

9.2 DML Trigger Concepts

9.3 Basic DML Triggers

9.4 Advanced DML Triggers

9.5 Instead of Triggers

Page 3: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

Procedural Database Programming (PL/SQL and T-SQL)

9. DATABASE TRIGGERS

Page 4: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.1 OVERVIEW OF TRIGGERS

Triggers are program units where procedural code is executed.

Triggers are executed when certain events take place.

Different kinds of triggers:

DML Triggers: Executed through CRUD operations. Perform validation,

additional data updates, default values, auditing of data.

DDL Triggers: Executed when DDL statements are executed. Auditing of

DDL object creation, prevent certain DDL statements.

Database Triggers: Executed when database starts up or is shut down.

SQL Server only has Logon trigger.

Instead Of Triggers: Alternatives to DML triggers. Execute when DML

operations are about to occur, you specify what code to execute instead of

the regular DML statement.

Keep in mind that DML triggers are part of a transaction.

Transaction is not complete until the trigger completes.

167

Page 5: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.2 DML TRIGGER CONCEPTS

DML triggers fire when a DML statement is executed.

DML triggers are the most commonly used ones.

Triggers are categorized by three attributes: what causes them to execute, when do they execute, and how often they execute.

Event (What): Statement that causes the firing of a trigger is called the triggering event. Trigger will be defined on a table and on Update, Delete, and/or Insert.

Action Time (When): Trigger can execute BEFORE or AFTER the triggering statement Trigger activation time or action time.

Granularity (How often): Statement or row triggers. A statement trigger executes once per statement; a row trigger executes once for each modified row.

There are total of 12 possible trigger combinations:

Row/Statement/Before/After Insert

Row/Statement/Before/After Update

Row/Statement/Before/After Delete

168

Page 6: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.2 DML TRIGGER CONCEPTS

Triggers cannot:

accept arguments.

In general, perform a commit or rollback (since triggers are in the middle of a DML process)

query or even update the table that the trigger belongs to (mutating table error).

In a trigger, you can reference the “before” and “after” value.

Insert and Delete triggers affect the entire row, but Update triggers can be programmed to execute on a column basis.

Write one trigger to combine Update, Delete, and Insert trigger.

Use special keywords to check for a particular DML action.

169

Page 7: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.2 DML TRIGGER CONCEPTS

To determine

the DML action

Simple keywords

in Oracle.

In SQL Server,

use system shadow tables Inserted and Deleted.

Example Update: Inserted table contains new, updated values,

Deleted table contains old values before the update.

You can create multiple triggers on one table. not

recommended.

Only when you really need to break down large chunk of code

into multiple units.

You can specify the order of those triggers (new in Oracle 11g).

170

Page 8: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

Syntax to create

triggers

ON keyword is used

to specify the table

or view this trigger

is attached to.

For/After or Instead Of specifies when the trigger fires.

INSERT/UPDATE/DELETE specifies the DML action when the

trigger fires, one or more actions are allowed.

AS starts the trigger body. Triggers do not return any data nor do

they have input parameters.

171

Page 9: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

System tables deleted and inserted are used to store values

before and after the trigger operation.

During a delete operation, only the deleted table is populated.

During an insert operation, only the inserted table is populated.

During an update operation, both inserted and deleted tables

are populated. Deleted hold the previous values, whereas

inserted holds the updated values.

If Update(column) is used to execute statements only when

specific columns are updated.

IF Columns Updated() uses bitwise comparison to determine

which columns were updated.

172

Page 10: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

Before/After/Instead Of

the timing of the trigger.

Oracle has a true before

trigger!

INSERT/DELETE/UPDATE

UPDATE OF to specify the

DML action when trigger

fires.

ON table/view determines the table or view the trigger belongs

to.

For Each Row sets the trigger as a row level trigger as opposed

to as a statement trigger.

IF Updating/Inserting/Deleting can be used to execute

statements based on DML conditions.

173

Page 11: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

174

Page 12: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

175

Page 13: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

176

Page 14: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

177

Page 15: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

178

Page 16: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 BASIC DML TRIGGERS

179

Page 17: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

Triggers seem like a simple tool to implement business rules.

Exhaust first any other possibilities before using trigger since

maintenance (scattered code), testing and debugging proves to

be more difficult.

New in Oracle 11g: Compound triggers and variables

Mutating Table Error

Whenever a trigger is fired, a DML operation is performed on a

table. Oracle does not allow a row level trigger to read or even

update the table that is undergoing the change.

SQL Server does not raise this error. However, to implement

code that reads the data in a table before the insert is

theoretically not possible in SQL Server as it does not have a

Before trigger!

180

Page 18: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

181

Page 19: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

182

Page 20: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

183

Page 21: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

184

Page 22: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

Status Transition Rules

Another common application for triggers is the enforcement of

transition rules.

For example, employee status starts with a P for new employees.

Certain rules concerning the transitioning from one status to

another.

185

Page 23: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

186

Page 24: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

187

Page 25: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

188

Page 26: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

Simple Audit Example

Both SQL Server and Oracle contain built-in auditing

mechanisms that you can turn on and configure.

Create customized audit system through the use of triggers.

The following sample triggers determines the DML action and

monitors the salary column of the employees table.

The audit table stores information about the table name, column

name, the old and the new value, date and time, and the user

who performed the change.

189

Page 27: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

190

Page 28: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

191

Page 29: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 ADVANCED DML TRIGGERS

192

Page 30: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 INSTEAD OF TRIGGERS

In SQL Server, Instead Of triggers are used to simulate Before

triggers since they are also allowed on tables.

In Oracle, Instead Of triggers control insert, update, merge, and

delete on views only, not tables.

Sometimes you want to present a view to the users (especially

when it is a one-to-one) so that they can see all their data at

once.

In general, views are nonupdateable, due to the nature of

relationships between the tables involved in the view.

The idea behind the Instead Of trigger is that the developer

designs custom programming logic to update data in the

corresponding tables in such a way that no relational rules are

violated and that it makes sense from a business perspective.

193

Page 31: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 INSTEAD OF TRIGGERS

194

Page 32: Class 7 Database Programming PROCEDURAL …€¦ · Procedural Database Programming (PL/SQL and T-SQL) 9. DATABASE TRIGGERS . 9.1 OVERVIEW OF TRIGGERS ...

9.3 INSTEAD OF TRIGGERS

195