Download presentation
Presentation is loading. Please wait.
Published byPreston Lamb Modified over 8 years ago
1
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
2
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 2
3
Declaring a parameter requires 2 to 4pieces of information: Name Data type Default value Direction See examples page 440 - 441 3
4
4 Creating output parameters, return values, dealing with errors: page 442top – 452mid
5
@@ERROR can be used in a stored procedure to test the result of the last SQL statement See examples page 453 – 454 5
6
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
7
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
8
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 8
9
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) Transaction or system error (integer overflow, division by zero...) Logical errors (insufficient account balance...) Concurrency control problems (deadlocks...) Disk failure Physical problems & catastrophes 9
10
The recovery manager of the DBMS keeps track of the following oparations: BEGIN TRANSACTION READ or WRITE END TRANSACTION COMMIT TRANSACTION a successful transaction ROLLBACK TRANSACTION an unsuccessful transaction 10
11
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
12 The system log, characterizing schedules: page 731 – 732bot + 733bot – 748top And now back to the book SQL Server
13
Transactions can be accepted (commit) or rejected (rollback) depending on, how the transaction went (ok or bad) See examples page 455 – 458top 13
14
By using the statement RAISERROR errors can be raised manually See examples page 462 14
15
The state parameter can be used as a place marker telling exactly where the error occurred 15
16
16 Error arguments, re-throwing errors, adding your own error messages: page 463bot – 468bot
17
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
18
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
19
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
20
Triggers can be used to check for integrity rules See examples page 544 – 546mid 20
21
21 Using triggers for custom error messages, other trigger issues : page 547bot – 553bot
22
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
23 The COLUMNS_UPDATED() function, additional about triggers: page 554mid – 558mid
24
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.