際際滷

際際滷Share a Scribd company logo
息 2012 2S Corporation. All rights reserved.
 Some Sql Features 
息 2012 2S Corporation. All rights reserved. 
Trigger 
Transaction 
Batches 
Stored Procedure
DML DDL 
息 2012 2S Corporation. All rights reserved. 
Trigger 
What is Trigger? 
A Trigger is a named database object which defines 
some action that the database should take when 
some databases related event occurs. 
The trigger event can be any of the DML or DDL Statement: 
INSERT 
UPDATE 
DELETE 
CREATE TABLE 
ALTER TABLE
The general syntax of CREATE TRIGGER is : 
CREATE TRIGGER trigger_name trigger_time trigger_event 
ON tbl_name FOR EACH ROW trigger_statement 
[Trigger_statement have the statement that executes when the 
trigger fires but if you want to execute multiple statement the you 
have to use the BEGIN or END compound statement.] 
息 2012 2S Corporation. All rights reserved. 
Note: 
The general syntax of DROP TRIGGER is : 
DROP TRIGGER trigger_name
What is DML Trigger? 
A DML Trigger is fired when data in the underlying table is 
affected by DML statements, such as 
INSERT,UPDATE,DELETE. 
息 2012 2S Corporation. All rights reserved. 
What is DDL Trigger? 
A DDL trigger is fired to response to DDL statement, 
such as CREATE TABLE or ALTER TABLE. 
Depending on the way in which trigger are fired , they are 
categorized as: 
1. After Trigger 2. Nested Trigger 
3. Instead of Trigger 4. Recursive Trigger
ALTER TRIGGGER trigger_name 
{FOR|AFTER}{event_type[,...n] | 
DDL_DATABASE_LEVEL_EVENTS} 
{AS 
{sql_statement[...n]} 
} 
息 2012 2S Corporation. All rights reserved. 
Managing Triggers 
 How can you manage triggers? 
While managing triggers, you can perform the following 
operations on a triggers: 
1. Altering a trigger 
2. Deleting a trigger 
DROP TRIGGER trigger_name
息 2012 2S Corporation. All rights reserved. 
Transactions 
What is Transaction? 
A transaction can be define as a sequence performed 
together as a single logical unit of work. 
A Single unit of work must process the four properties called 
ACID. 
Atomicity 
Consistency 
Isolation 
Durability 
A 
C 
I 
D
The SQL Server allows implementing transactions in the following 
two ways: 
息 2012 2S Corporation. All rights reserved. 
1. Autocommit Transaction 
2. Explicit Transaction 
Explicit transaction are specified by using two statement: 
1. BEGIN TRANSACTION 
2. COMMIT TRANSACTION 
For example:- 
BEGIN TRAN myTran 
Select * From Humanresource.Department 
COMMIT TRAN myTran
Reverting Transaction 
At a time there is a possibility that due to a problem all the 
statements of a transaction will not be successfully executed. 
Client gets an 
error with Try 
Again msg 
Client still 
waiting 
息 2012 2S Corporation. All rights reserved. 
Transaction 
timed out 
For example:- 
Client hit the 
button to place 
the orders 
12:00 AM 12:02 AM 12:05 AM 12:08 AM 12:11 AM 12:14 AM 
Then we use for resolving this problem:- 
 ROLLBACK TRANSACTION: 
ROLLBACK TRAN[SACTION] [transaction_name | 
@tran_name_variable | savepoint_name | 
@savepoint_variable]]
Transactional Integrity 
To maintain the Security & Concurrency issues in giving Multi-user 
Environment we use 'LOCKS'. 
息 2012 2S Corporation. All rights reserved. 
 Need For Locking: 
In the absence of Locking, problems may occur if more then 
one transaction use the same data from a database at the same 
time. 
These problem Include:- 
1. Lost Updates 
2. Uncommitted dependency(Dirty Read) 
3. Inconsistent Analysis 
4. Phantom Reads
息 2012 2S Corporation. All rights reserved. 
 Controlling Locks 
If you want to change the lock mode from a Normal shared lock to 
an Exclusive lock you need use Isolation levels 
Types of isolation levels are: 
1. READ UNCOMMITED 
2. READ COMMITED 
3. REPEATABLE READ 
4. SNAPSHOT 
5. SERIALIZABLE 
 Deadlocks Condition: 
A Deadlock is a situation where two or more transaction have 
locks on separate objects, and each transaction wait for a lock on 
the other object to be released.
息 2012 2S Corporation. All rights reserved. 
 Resolving Deadlocks: 
To resolved the deadlock Situation we uses :- 
1. Setting Deadlock Priority 
SET DEADLOCK_PRIORITY{LOW|NORMAL|@deadlock_var} 
2. Customizing Lock Timeout 
SET LOCK_TIMEOUT [timeout_period] 
3. Using sys.dm_exec_requests 
SELECT * FROM sys.dm_exec_requests
息 2012 2S Corporation. All rights reserved. 
Batches 
What is Batches ? 
A Batches is group of Sql statements submitted together to 
the Sql Server for execution. 
To create a batch, you can write multiple SQL statements 
followed by the keyword GO at the end:- 
<T-SQL Statement1> 
<T-SQL Statement2> 
<T-SQL Statement3> 
...... 
GO 
Note:- Go is the command that specifies the end of the batch 
statement.
 Using Variables with Batches 
While creating batches, you might need to save value 
temporarily during the execution time then you define a variable 
by DECLARE statement. 
息 2012 2S Corporation. All rights reserved. 
For example:- 
DECLARE @variable_name data_type 
 Guideline to Create Batches 
You cannot combine statements, such as CREATE RULE, 
CREATE TRIGGER etc, with other statements while 
creating a batch. 
You can use the EXECUTE statement in a batch when it is not 
the 1st statement of the batch.
息 2012 2S Corporation. All rights reserved. 
 Stored Procedures 
A stored Procedure is a Pre-Compiled object stored in the 
database. 
The syntax of the create Procedure Statement is:- 
CREATE PROCEDURE proc_name 
AS 
BEGIN 
sql_statment1, sql_statement2 
END 
 Executing a Stored Procedure 
A procedure can be executed by using the EXEC 
PROCEDURE statement. 
EXEC | EXECUTE PROCEDURE proc_name AS [{login|user} ='name']
息 2012 2S Corporation. All rights reserved. 
 Altering a Stored Procedure 
A Stored Procedure can be modified by using the ALTER 
PROCEDURE statement. 
Syntax:- 
ALTER PROCEDURE proc_name 
 Dropping a Stored Procedure 
You can Drop the Stored Procedure from the Database by 
using DROP PROCEDURE statement. 
Syntax:- 
DROP PROCEDURE proc_name
Creating Parameterized Stored procedures 
When you need to Execute Procedure for different values of a 
variable that provided at runtime. for this, you can create 
parameterized store procedure. 
息 2012 2S Corporation. All rights reserved. 
Syntax:- 
CREATE PROC prcListEmployee @title char(50) 
AS 
BEGIN 
PRINT 'List of Employees' 
SELECT EmployeeID, Login, Title from 
HumanResource.Employee WHERE Title=@title 
END 
EXECUTE prcListEmployee @title=tool designer
息 2012 2S Corporation. All rights reserved.

More Related Content

ISAS On SQL Features like Trigger, Transaction,Batches, Stored Procedure

  • 1. 息 2012 2S Corporation. All rights reserved.
  • 2. Some Sql Features 息 2012 2S Corporation. All rights reserved. Trigger Transaction Batches Stored Procedure
  • 3. DML DDL 息 2012 2S Corporation. All rights reserved. Trigger What is Trigger? A Trigger is a named database object which defines some action that the database should take when some databases related event occurs. The trigger event can be any of the DML or DDL Statement: INSERT UPDATE DELETE CREATE TABLE ALTER TABLE
  • 4. The general syntax of CREATE TRIGGER is : CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement [Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGIN or END compound statement.] 息 2012 2S Corporation. All rights reserved. Note: The general syntax of DROP TRIGGER is : DROP TRIGGER trigger_name
  • 5. What is DML Trigger? A DML Trigger is fired when data in the underlying table is affected by DML statements, such as INSERT,UPDATE,DELETE. 息 2012 2S Corporation. All rights reserved. What is DDL Trigger? A DDL trigger is fired to response to DDL statement, such as CREATE TABLE or ALTER TABLE. Depending on the way in which trigger are fired , they are categorized as: 1. After Trigger 2. Nested Trigger 3. Instead of Trigger 4. Recursive Trigger
  • 6. ALTER TRIGGGER trigger_name {FOR|AFTER}{event_type[,...n] | DDL_DATABASE_LEVEL_EVENTS} {AS {sql_statement[...n]} } 息 2012 2S Corporation. All rights reserved. Managing Triggers How can you manage triggers? While managing triggers, you can perform the following operations on a triggers: 1. Altering a trigger 2. Deleting a trigger DROP TRIGGER trigger_name
  • 7. 息 2012 2S Corporation. All rights reserved. Transactions What is Transaction? A transaction can be define as a sequence performed together as a single logical unit of work. A Single unit of work must process the four properties called ACID. Atomicity Consistency Isolation Durability A C I D
  • 8. The SQL Server allows implementing transactions in the following two ways: 息 2012 2S Corporation. All rights reserved. 1. Autocommit Transaction 2. Explicit Transaction Explicit transaction are specified by using two statement: 1. BEGIN TRANSACTION 2. COMMIT TRANSACTION For example:- BEGIN TRAN myTran Select * From Humanresource.Department COMMIT TRAN myTran
  • 9. Reverting Transaction At a time there is a possibility that due to a problem all the statements of a transaction will not be successfully executed. Client gets an error with Try Again msg Client still waiting 息 2012 2S Corporation. All rights reserved. Transaction timed out For example:- Client hit the button to place the orders 12:00 AM 12:02 AM 12:05 AM 12:08 AM 12:11 AM 12:14 AM Then we use for resolving this problem:- ROLLBACK TRANSACTION: ROLLBACK TRAN[SACTION] [transaction_name | @tran_name_variable | savepoint_name | @savepoint_variable]]
  • 10. Transactional Integrity To maintain the Security & Concurrency issues in giving Multi-user Environment we use 'LOCKS'. 息 2012 2S Corporation. All rights reserved. Need For Locking: In the absence of Locking, problems may occur if more then one transaction use the same data from a database at the same time. These problem Include:- 1. Lost Updates 2. Uncommitted dependency(Dirty Read) 3. Inconsistent Analysis 4. Phantom Reads
  • 11. 息 2012 2S Corporation. All rights reserved. Controlling Locks If you want to change the lock mode from a Normal shared lock to an Exclusive lock you need use Isolation levels Types of isolation levels are: 1. READ UNCOMMITED 2. READ COMMITED 3. REPEATABLE READ 4. SNAPSHOT 5. SERIALIZABLE Deadlocks Condition: A Deadlock is a situation where two or more transaction have locks on separate objects, and each transaction wait for a lock on the other object to be released.
  • 12. 息 2012 2S Corporation. All rights reserved. Resolving Deadlocks: To resolved the deadlock Situation we uses :- 1. Setting Deadlock Priority SET DEADLOCK_PRIORITY{LOW|NORMAL|@deadlock_var} 2. Customizing Lock Timeout SET LOCK_TIMEOUT [timeout_period] 3. Using sys.dm_exec_requests SELECT * FROM sys.dm_exec_requests
  • 13. 息 2012 2S Corporation. All rights reserved. Batches What is Batches ? A Batches is group of Sql statements submitted together to the Sql Server for execution. To create a batch, you can write multiple SQL statements followed by the keyword GO at the end:- <T-SQL Statement1> <T-SQL Statement2> <T-SQL Statement3> ...... GO Note:- Go is the command that specifies the end of the batch statement.
  • 14. Using Variables with Batches While creating batches, you might need to save value temporarily during the execution time then you define a variable by DECLARE statement. 息 2012 2S Corporation. All rights reserved. For example:- DECLARE @variable_name data_type Guideline to Create Batches You cannot combine statements, such as CREATE RULE, CREATE TRIGGER etc, with other statements while creating a batch. You can use the EXECUTE statement in a batch when it is not the 1st statement of the batch.
  • 15. 息 2012 2S Corporation. All rights reserved. Stored Procedures A stored Procedure is a Pre-Compiled object stored in the database. The syntax of the create Procedure Statement is:- CREATE PROCEDURE proc_name AS BEGIN sql_statment1, sql_statement2 END Executing a Stored Procedure A procedure can be executed by using the EXEC PROCEDURE statement. EXEC | EXECUTE PROCEDURE proc_name AS [{login|user} ='name']
  • 16. 息 2012 2S Corporation. All rights reserved. Altering a Stored Procedure A Stored Procedure can be modified by using the ALTER PROCEDURE statement. Syntax:- ALTER PROCEDURE proc_name Dropping a Stored Procedure You can Drop the Stored Procedure from the Database by using DROP PROCEDURE statement. Syntax:- DROP PROCEDURE proc_name
  • 17. Creating Parameterized Stored procedures When you need to Execute Procedure for different values of a variable that provided at runtime. for this, you can create parameterized store procedure. 息 2012 2S Corporation. All rights reserved. Syntax:- CREATE PROC prcListEmployee @title char(50) AS BEGIN PRINT 'List of Employees' SELECT EmployeeID, Login, Title from HumanResource.Employee WHERE Title=@title END EXECUTE prcListEmployee @title=tool designer
  • 18. 息 2012 2S Corporation. All rights reserved.