24
Stored procedures and transactions & triggers 1 Intro stored procedures Declaring parameters Using @@ERROR in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable properties of transactions Handling transactions Manually raising errors Primary benefits of stored procedures Intro to triggers FOR/AFTER vs INSTEAD OF clause Using triggers for data integrity rules The UPDATE() function Steen Jensen, autumn 2013

1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable

Embed Size (px)

DESCRIPTION

 Declaring a parameter requires 2 to 4pieces of information: Name Data type Default value Direction  See examples page

Citation preview

Data modeling using ER1
Intro to transactions
Concurrency control & recovery
States of transactions
Intro to triggers
Using triggers for data integrity rules
The UPDATE() function
Intro stored procedures
A stored procedure (or a sproc) is a script or more correctly a batch, that is stored in the database rather than in a separate file
See example page 437
Name
3
4
Creating output parameters, return values, dealing with errors: page 442top – 452mid
Using @@ERROR in a sproc
@@ERROR can be used in a stored procedure to test the result of the last SQL statement
See examples page 453 – 454
5
Exercise in stored procedures
Think of a stored procedure, which might be useful for the Amazon example
What should the sproc contain?
Which parameter(s) should be used?
Finally try to execute the stored procedure by writing the name of the sproc followed by the necessary parameter(s)
6
A transaction is a logical unit of database processing
A transaction includes one or more database oparations, such as INSERT, DELETE, UPDATE or SELECT
The boundaries of a trnsaction can be specified by a BEGIN TRANSACTION and a END TRANSACTION statement
7
Why concurrency control is needed
If concurrent transactions are executed in an uncontrolled manner, errors can occur
An example of this could be an airline seat reservation system
4 kinds of errors could occur (see Elmasri chapter 20, page 726 – 728top):
The lost update problem (figure 20.3 a)
The temporary update problem (figure 20.3 b)
The incorrect summary problem (figure 20.3 c)
The unrepeatable problem
Why recovery is needed
When a transaction is submitted to a DBMS for execution, the system is responsible for ensuring, that all the operations in the transaction are completed successfully, or that the transaction doesn’t have any effect
In the first case the transaction is said to be committed, whereas in the second case the transaction is aborted
6 types of failure:
Computer failure (system crash)
Logical errors (insufficient account balance ...)
Concurrency control problems (deadlocks ...)
Transactions states
The recovery manager of the DBMS keeps track of the following oparations:
BEGIN TRANSACTION
10
Transaction should possess several properties, often nicknamed ACID:
Atomicity: a transaction should either be performed entirely or not at all
Consistency preservation: a transaction should take the database from one consistent state to another
Isolation: should appear as being executed in isolation from other transactions
Durability or permanency: changes applied to a database by a transaction must persist in the database
11
12
The system log, characterizing schedules: page 731 – 732bot + 733bot – 748top
And now back to the book SQL Server
Handling transactions
Transactions can be accepted (commit) or rejected (rollback) depending on, how the transaction went (ok or bad)
See examples page 455 – 458top
13
By using the statement RAISERROR errors can be raised manually
See examples page 462
RAISERROR severity & state
The state parameter can be used as a place marker telling exactly where the error occurred
15
16
Error arguments, re-throwing errors, adding your own error messages: page 463bot – 468bot
Primary benefits of stored procedures
The primary benefits include:
Because a sproc is stored in a database, it can be called without manually loading it. Sprocs can call other sprocs (nesting)
A tool of security: users can get the needed info without having access to the underlying table(s)
Can give a higher degree of performance
17
Intro to triggers
A trigger is a special kind of stored procedure, that fires in response to a specified event
Two kinds of triggers:
DDL triggers (normally only used for extreme auditing of changes/history of database structure)
DML triggers
Triggers are pieces of code attached to a table or view
Trigger are automatically run – they can’t be explicitly invoked (as a sproc)
DML triggers for INSERT, DELETE, UPDATE + mix/match
18
FOR/AFTER vs INSTEAD OF clause
SQL Server puts together two working tables: Inserted (containing a copy of inserted records) and Deleted (containing a copy of deleted records)
The INSTEAD OF keyword means, that working tables are created before any constraints are checked (in contrast to FOR/AFTER)
See also figure 15-1 page 542
19
Triggers can be used to check for integrity rules
See examples page 544 – 546mid
20
21
Using triggers for custom error messages, other trigger issues : page 547bot – 553bot
The UPDATE() function
The UPDATE function can be used to check, if a certain column has been changed
See example page 553bot – 554mid (the example is a revised version of the example from page 546)
22
23
Exercise in triggers
Make ex.1 + 2 on page 558 (chapter 15) in the book without looking at the answer
Then check your answer up against the indicative answer on page 794
If possible try to implement a trigger in either the Amazon or the Adventureworks system
24