Академический Документы
Профессиональный Документы
Культура Документы
___________________________________________________________________________________________________
Copyrights Reserved Page 1 of 11
TRANSACT-SQL PROGRAM STRUCTURES &
TRANSACTION MANAGEMENT
Chapter Includes:
Triggers
Magic Tables
Nested Triggers
Stored Procedures
Transaction Mechanism
Transaction Control
Transaction Programming
Chained Transactions
___________________________________________________________________________________________________
Copyrights Reserved Page 2 of 11
Triggers:
Triggers are database objects that are bound directly to tables to enforce the
referential integrity. Triggers are bound to the table for various purposes such as
insertion, updating and deletion.
Types of Trigger:
Insert Trigger
Update Trigger and
Delete Trigger
Insert Trigger:
Update Trigger:
Delete Trigger:
sql statement.
___________________________________________________________________________________________________
Copyrights Reserved Page 3 of 11
Example : create trigger titles_trigger on titles for insert as print “title inserted”
Magical Tables:
Whenever a trigger is updated, internally two tables will be formed. First table is
for the previous (statement before update) statement and second table filled with
the modified statement. So by going through the first table we can identify the
number of tables that are updated. As these two tables are formed automatically
for every update trigger, they are called Magic tables.
Nested Triggers:
If a trigger performs an insert, update or delete on another table that has trigger
defined for that action, by default that trigger will also fire. This is called nested
trigger. Triggers are limited to 16levels of nesting.
Check @@nestlevel to avoid exceeding limits.
Stored Procedures:
A stored procedure is a database object that exists independently of a table. It can
be called from client, parameters may be passed and returned, and error codes
may be checked.
Faster execution
Modular Programming
Enforced consistency
Example:
The SQL server optimizer generates a query plan for a stored procedure
based on the parameters passed in the first time it is executed. This query
plan is then run from cache for subsequent executions. To force a new query
plan we use recompile option.
Transaction Mechanism:
SQL server locks table pages during transactions so other users cannot
___________________________________________________________________________________________________
Copyrights Reserved Page 5 of 11
Transaction Control:
All SQL statements are said to be transactions. They can be controlled by
transaction control syntax to group sets of SQL statements to single logical work
units.
Transaction Programming:
The programming issues associated with writing transactional SQL are fairly
straightforward. Once you issue a Begin tran statement. The server performs all
the subsequent work without formally writing a final record of the work.
In order to ensure data integrity, SQL Server places exclusive locks on data
pages involved in a transaction. As SQL Server progress through the query, locks
are acquired on each table modified by a statement and are then held until SQL
___________________________________________________________________________________________________
Copyrights Reserved Page 6 of 11
Transaction and Triggers:
begin tran
update titles
commit tran
current data modification statement, immediately return from the trigger and
Rollback Trigger:
The effect of the rollback trigger is to rollback entire transaction and abort
___________________________________________________________________________________________________
Copyrights Reserved Page 7 of 11
Transactions in Stored Procedures:
The stored procedures and the batches that call those procedures need to be
consistent with each other to provide with proper data integrity control. One must
establish coding standards for that enable transactional control to work whether
the transaction commits successfully or the transaction is rolled back.
Chained Transactions:
___________________________________________________________________________________________________
Copyrights Reserved Page 8 of 11
Trigger
Stored procedures
-5,-4,-2,-9
6) Explain about the Cursors in a Stored procedure? How do you replace a cursor
in a stored procedure?
1) SQL Server notes the transaction nesting level before calling a stored
procedure
2) A Stored procedure may not create view, default, rule and trigger
3) SQL server optimizer generates a query plan for a stored procedure based
on the parameters passed in the first time it is executed.
DATABASE SCHEMA
Under consideration is the database of naval ships that took part in World War II.
The database has the following relations:
Classes(class,type,country,numGuns,bore,displacement)
Ships(name,class,launched)
Battles(name,date)
Outcomes(ship,battle,result)
Ships in classes are arranged to a single project. A class is normally assigned the
name of the first ship in the class under consideration (head ship); otherwise,
class name does not coincide with any ship name in the database).
The relation Classes includes the class name, type (bb for a battle ship, or bc for
a battle cruiser), country the ship was built, number of main guns, gun caliber
(diameter of the gun barrel, in inches), and displacement (weight in tons). The
relation Ships includes the ship's name, its class name, and launch year. The
relation Battles covers the name and date of the battle the ships participated;
while the result of their participation in the battle (sunk, damaged, or unharmed -
OK) is in the relation Outcomes. Note: the relation Outcomes may include the
ships not included in the relation Ships.
___________________________________________________________________________________________________
Copyrights Reserved Page 9 of 11
Based On the above Database schema solve the Following queries
2)Delete from Ships table all the ships that belong to USA.
3)Find the names of ship in the ships sunk in battles and the names of
corresponding battles.
4)Determine names of all the ships in Ships table that meet a combination of
at least four criteria from the following list:
numGuns = 8
bore = 15
displacement = 32000
type = bb
launched = 1915
class = Kongo
country = USA
List the names of head ships in the database (taking into account Outcomes
table).
Topics Covered:
by
Ray Rankins
Jeffrey R. Garbus
David Solomon
___________________________________________________________________________________________________
Copyrights Reserved Page 11 of 11