Class 6 - ucb
Transcription
Class 6 - ucb
Instructor: Michael Kremer, Ph.D. Technology & Information Management Class 6 Database Programming PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL) AGENDA 8. Using Declarative SQL in Procedural SQL 8.1 DML in Procedural SQL 8.2 Transaction Management 8.3 Autonomous Transactions Procedural Database Programming (PL/SQL and T-SQL) 8. USING DECLARATIVE SQL IN PROCEDURAL SQL 141 8.1 DML IN PROCEDURAL SQL Group multiple DML statements together into a logical transaction. Apply the principle of all (commit) or nothing (rollback). ACID Principle: Atomicity: A transaction’s changes to a state are atomic, either all or none take place. Consistency: Data before and after the transaction is in consistent state (but not necessarily during the transaction) Isolation: Changes resulting from a transaction are isolated from other sessions until a commit or rollback occurs. Durability: Once changes are made permanent, they are persistent forever in the database. Brief Review of DML statements. Generally no differences in syntax between Oracle and SQL Server. 142 8.1 DML IN PROCEDURAL SQL Insert Statement Two main variations. Optional column list allows you to specify only those columns that you want to provide values for. Update Statement Where clause is optional, if left out, entire table is updated. Delete Statement Where clause is optional, if left out, all rows are deleted 143 8.1 DML IN PROCEDURAL SQL Other DML statements: Specialized statements for particular data situations, such as Merge. Attributes for DML statements Information about the most recent DML statement is available in both platforms. For example, you may want to know how many rows were affected. Oracle: SQL Server: SET NOCOUNT ON|OFF @@ROWCOUNT 144 8.1 DML IN PROCEDURAL SQL 145 8.1 DML IN PROCEDURAL SQL 146 8.1 DML IN PROCEDURAL SQL Returning information from DML statements Using non-procedural SQL statements in procedural environment generated the need to add extensions. For example, a SELECT statement allows you to store returned column values in variables. For insert statements, store values of automatically generated data, such as sequences/identity columns in a variable. This is necessary to insert a corresponding child record! SQL Server: @@Identity global variable, not the best approach! SQL Server two new additional methods: IDENT_CURRENT(‘table_name’), SCOPE_IDENTITY In Oracle, use another select statement to find out about the most recent sequence number. Safest way to retrieve the primary key value is to issue a SELECT statement and use the logical key of the row that was inserted in the WHERE clause to uniquely identify the row. 147 8.1 DML IN PROCEDURAL SQL Best way is to be able to retrieve the primary key value when using the insert statement. Returning clause in Oracle: Output Inserted into in SQL Server: Need to declare a table variable. Allows to store multiple columns in one variable. 148 8.1 DML IN PROCEDURAL SQL 149 8.1 DML IN PROCEDURAL SQL 150 8.1 DML IN PROCEDURAL SQL 151 8.1 DML IN PROCEDURAL SQL 152 8.2 TRANSACTION MANAGEMENT Transactions Transactions allow you as the developer to define the boundaries of an activity that will be considered atomic. Start a transaction: Default Transaction Mode: SQL Server: Issue Begin Tran statement Oracle: Simply issue DML statement SQL Server: Autocommit. When statement completes successfully, it is committed otherwise is it rolled back. Oracle: You have to issue either Commit or Rollback Change SQL Server’s default behavior: SET IMPLICIT_TRANSACTIONS ON; ALTER DATABASE db_name SET READ_COMMITTED_SNAPSHOT ON; 153 8.2 TRANSACTION MANAGEMENT 154 8.2 TRANSACTION MANAGEMENT 155 8.2 TRANSACTION MANAGEMENT Commit Statement The following rules apply after issuing a commit: A locks are released You cannot issue a rollback Rollback Statement Undo some or all of your changes 156 8.2 TRANSACTION MANAGEMENT Savepoint/Save Statement SAVEPOINT gives a name to, and marks a point in, the processing of your transaction allowing you To ROLLBACK To that point, undoing any changes and releasing any locks issued after that savepoint Preserve any changes and locks that occurred before you marked the savepoint. 157 8.2 TRANSACTION MANAGEMENT 158 8.2 TRANSACTION MANAGEMENT 159 8.2 TRANSACTION MANAGEMENT 160 8.2 TRANSACTION MANAGEMENT 161 8.3 AUTONOMOUS TRANSACTIONS Autonomous transactions are started by a parent, or main, transaction but operate independently of the parent for transaction control. Commit, rollback or failure in either transaction does not impact the other transaction. Only in Oracle, you can simulate this in SQL Server. Oracle: PRAGMA AUTONOMOUS_TRANSACTION 162 8.3 AUTONOMOUS TRANSACTIONS Applications for autonomous transactions: Logging mechanism: You want to rollback your main transaction when an error occurs, yet you want to record the error info. Perform commits and rollbacks in your database triggers: Commit or rollback in triggers irrespective of main transaction. Reusable application components: Build completely independent units of functionality, especially in web apps. Call user-defined functions in SQL that modify tables: In Oracle only, you can declare a function as an autonomous transaction, perform DML statements while running it in a SQL Select statement. 163 8.3 AUTONOMOUS TRANSACTIONS 164 8.3 AUTONOMOUS TRANSACTIONS 165 8.3 AUTONOMOUS TRANSACTIONS