ILP_DotNET_Stream_SQLServer2005_11_V0.1

download ILP_DotNET_Stream_SQLServer2005_11_V0.1

of 12

Transcript of ILP_DotNET_Stream_SQLServer2005_11_V0.1

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    1/12

    1TCS Internal

    Triggers and Transaction Management

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    2/12

    2TCS Internal

    Objective

    To be able to learn the implementation of triggers and SQLServer transactions

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    3/12

    3TCS Internal

    Scope

    Introduction to Triggers

    Creating a trigger

    Transaction Management

    Locks

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    4/12

    4TCS Internal

    Introduction to Trigger

    SQL Server provides a mechanism called a trigger for

    enforcing procedural integrity constraints.

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    5/12

    5TCS Internal

    How trigger work

    A trigger is a mechanism that is invoked when a particularaction occurs on a particular table. Each trigger has threegeneral parts:

    Name

    ActionExecution

    The action of the trigger either be an INSERT, an UPDATE,or a DELETE statement.

    The execution part of the trigger usually contains a storedprocedure or a batch.

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    6/12

    6TCS Internal

    Creating a Trigger

    SQL Server 2005 allows us to create trigger using eitherTransact SQL or CLR programming languages such as C#and Visual Basic 2005.

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    7/12

    7TCS Internal

    Creating a Trigger (Transact-SQL)

    A trigger is created using the CREATE TRIGGER statement, which hasthe follow form:

    Syntax:

    CREATE TRIGGER [schema_name.]trigger_nameON table_name | view_name[WITH dml_trigger_option]{FOR | AFTER|INSTEAD OF} { [INSERT][,][UPDATE][,][DELETE]}[WITH APPEND]AS sql_statement | EXTERNAL NAME method_name

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    8/12

    8TCS Internal

    Transaction Management

    A transaction is a logical unit of work.

    Any single SQL statement is considered to be a single unitof work whether it affects only a single row or multiple rowswithin the table.

    In SQL Server a transaction is implicit.

    Explicit transactions can be defined by the user by includingthe SQL statements enclosed within BEGIN TRANSACTIONand COMMIT TRANSACTION.

    In creating explicitly defined transactions, the following

    statement are used.

    BEGIN TRANSACTION COMMIT TRANSACTION ROLLBACK TRANSACTION SAVE TRANSACTION

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    9/12

    9TCS Internal

    Database consistency / Concurrency

    When two or more users access the database concurrently,SQL Server uses locking to ensure that their actions dontinterfere each other.

    Locking prevents one user from reading the data which isbeing changed by the other users.

    It also prevents users from making more than one changeto a record at any one time.

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    10/12

    10TCS Internal

    Isolation Levels

    SQL Server supports file isolation levels:

    READ UNCOMMITEDREAD COMMITEDREPEATABLE READSNAPSHOTSERIALIZABLE

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    11/12

    11TCS Internal

    Deadlock

    A deadlock is a special situation in which two transactionsblock the progress of each other.

    The first transaction has a lock on some database objectthat the other transaction wants to access and vice versa.

  • 8/9/2019 ILP_DotNET_Stream_SQLServer2005_11_V0.1

    12/12

    12TCS Internal

    Summary

    Introduction to Triggers

    Creating a trigger

    Transaction Management

    Locks