程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> SQL SERVER 觸發器引見

SQL SERVER 觸發器引見

編輯:MSSQL

SQL SERVER 觸發器引見。本站提示廣大學習愛好者:(SQL SERVER 觸發器引見)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL SERVER 觸發器引見正文


觸發器是一種特別類型的存儲進程,它分歧於之前的我們引見的存儲進程。觸發器重要是經由過程事宜停止觸發被主動挪用履行的。而存儲進程可以經由過程存儲進程的稱號被挪用。

Ø 甚麼是觸發器

觸發器對表停止拔出、更新、刪除的時刻會主動履行的特別存儲進程。觸發器普通用在check束縛加倍龐雜的束縛下面。觸發器和通俗的存儲進程的差別是:觸發器是當對某一個表停止操作。諸如:update、insert、delete這些操作的時刻,體系會主動挪用履行該表上對應的觸發器。SQL Server 2005中觸發器可以分為兩類:DML觸發器和DDL觸發器,個中DDL觸發器它們會影響多種數據界說說話語句而激起,這些語句有create、alter、drop語句。

DML觸發器分為:

1、 after觸發器(以後觸發)

a、 insert觸發器
b、 update觸發器
c、 delete觸發器

2、 instead of 觸發器 (之前觸發)

個中after觸發器請求只要履行某一操作insert、update、delete以後觸發器才被觸發,且只能界說在表上。而instead of觸發器表現其實不履行其界說的操作(insert、update、delete)而僅是履行觸發器自己。既可以在表上界說instead of觸發器,也能夠在視圖上界說。
觸發器有兩個特別的表:拔出表(instered表)和刪除表(deleted表)。這兩張是邏輯表也是虛表。有體系在內存中創立者兩張表,不會存儲在數據庫中。並且兩張表的都是只讀的,只能讀取數據而不克不及修正數據。這兩張表的成果老是與被改觸發器運用的表的構造雷同。當觸發器完成任務後,這兩張表就會被刪除。Inserted表的數據是拔出或是修正後的數據,而deleted表的數據是更新前的或是刪除的數據。

對表的操作

Inserted邏輯表

Deleted邏輯表

增長記載(insert)

寄存增長的記載

刪除記載(delete)

寄存被刪除的記載

修正記載(update)

寄存更新後的記載

寄存更新前的記載

Update數據的時刻就是先刪除表記載,然後增長一筆記錄。如許在inserted和deleted表就都有update後的數據記載了。留意的是:觸發器自己就是一個事務,所以在觸發器外面可以對修正數據停止一些特別的檢討。假如不知足可以應用事務回滾,撤消操作。

Ø 創立觸發器

語法

create trigger tgr_name
on table_name
with encrypion –加密觸發器
for update...
as
Transact-SQL 

# 創立insert類型觸發器

--創立insert拔出類型觸發器
if (object_id('tgr_classes_insert', 'tr') is not null)
drop trigger tgr_classes_insert
go
create trigger tgr_classes_insert
on classes
for insert --拔出觸發
as
--界說變量
declare @id int, @name varchar(20), @temp int;
--在inserted表中查詢曾經拔出記載信息
select @id = id, @name = name from inserted;
set @name = @name + convert(varchar, @id);
set @temp = @id / 2; 
insert into student values(@name, 18 + @id, @temp, @id);
print '添加先生勝利!';
go
--拔出數據
insert into classes values('5班', getDate());
--查詢數據
select * from classes;
select * from student order by id; 
insert觸發器,會在inserted表中添加一條剛拔出的記載。


# 創立delete類型觸發器

--delete刪除類型觸發器
if (object_id('tgr_classes_delete', 'TR') is not null)
drop trigger tgr_classes_delete
go
create trigger tgr_classes_delete
on classes
for delete --刪除觸發
as
print '備份數據中……'; 
if (object_id('classesBackup', 'U') is not null)
--存在classesBackup,直接拔出數據
insert into classesBackup select name, createDate from deleted;
else
--不存在classesBackup創立再拔出
select * into classesBackup from deleted;
print '備份數據勝利!';
go
--
--不顯示影響行數
--set nocount on;
delete classes where name = '5班';
--查詢數據
select * from classes;
select * from classesBackup; 
delete觸發器會在刪除數據的時刻,將適才刪除的數據保留在deleted表中。


# 創立update類型觸發器

--update更新類型觸發器
if (object_id('tgr_classes_update', 'TR') is not null)
drop trigger tgr_classes_update
go
create trigger tgr_classes_update
on classes
for update
as
declare @oldName varchar(20), @newName varchar(20);
--更新前的數據
select @oldName = name from deleted;
if (exists (select * from student where name like '%'+ @oldName + '%'))
begin
--更新後的數據
select @newName = name from inserted;
update student set name = replace(name, @oldName, @newName) where name like '%'+ @oldName + '%';
print '級聯修正數據勝利!';
end
else
print '無需修正student表!';
go
--查詢數據
select * from student order by id;
select * from classes;
update classes set name = '五班' where name = '5班'; 
update觸發器會在更新數據後,將更新前的數據保留在deleted表中,更新後的數據保留在inserted表中。


# update更新列級觸發器

if (object_id('tgr_classes_update_column', 'TR') is not null)
drop trigger tgr_classes_update_column
go
create trigger tgr_classes_update_column
on classes
for update
as
--列級觸發器:能否更新了班級創立時光
if (update(createDate))
begin
raisError('體系提醒:班級創立時光不克不及修正!', 16, 11);
rollback tran;
end
go
--測試
select * from student order by id;
select * from classes;
update classes set createDate = getDate() where id = 3;
update classes set name = '四班' where id = 7;

更新列級觸發器可以用update能否斷定更新列記載;

# instead of類型觸發器

instead of觸發器表現其實不履行其界說的操作(insert、update、delete)而僅是履行觸發器自己的內容。

創立語法

create trigger tgr_name
on table_name
with encryption
instead of update...
as
T-SQL 

# 創立instead of觸發器

if (object_id('tgr_classes_inteadOf', 'TR') is not null)
drop trigger tgr_classes_inteadOf
go
create trigger tgr_classes_inteadOf
on classes
instead of delete/*, update, insert*/
as
declare @id int, @name varchar(20);
--查詢被刪除的信息,病賦值
select @id = id, @name = name from deleted;
print 'id: ' + convert(varchar, @id) + ', name: ' + @name;
--先刪除student的信息
delete student where cid = @id;
--再刪除classes的信息
delete classes where id = @id;
print '刪除[ id: ' + convert(varchar, @id) + ', name: ' + @name + ' ] 的信息勝利!';
go
--test
select * from student order by id;
select * from classes;
delete classes where id = 7; 

# 顯示自界說新聞raiserror

if (object_id('tgr_message', 'TR') is not null)
drop trigger tgr_message
go
create trigger tgr_message
on student
after insert, update
as raisError('tgr_message觸發器被觸發', 16, 10);
go
--test
insert into student values('lily', 22, 1, 7);
update student set sex = 0 where name = 'lucy';
select * from student order by id; 

# 修正觸發器

alter trigger tgr_message
on student
after delete
as raisError('tgr_message觸發器被觸發', 16, 10);
go
--test
delete from student where name = 'lucy'; 

# 啟用、禁用觸發器

--禁用觸發器
disable trigger tgr_message on student;
--啟用觸發器
enable trigger tgr_message on student; 

# 查詢創立的觸發器信息

--查詢已存在的觸發器
select * from sys.triggers;
select * from sys.objects where type = 'TR';

--檢查觸發器觸發事宜
select te.* from sys.trigger_events te join sys.triggers t
on t.object_id = te.object_id
where t.parent_class = 0 and t.name = 'tgr_valid_data';

--檢查創立觸發器語句
exec sp_helptext 'tgr_message'; 


# 示例,驗證拔出數據

if ((object_id('tgr_valid_data', 'TR') is not null))
drop trigger tgr_valid_data
go
create trigger tgr_valid_data
on student
after insert
as
declare @age int,
@name varchar(20);
select @name = s.name, @age = s.age from inserted s;
if (@age < 18)
begin
raisError('拔出新數據的age有成績', 16, 1);
rollback tran;
end
go
--test
insert into student values('forest', 2, 0, 7);
insert into student values('forest', 22, 0, 7);
select * from student order by id; 

# 示例,操作日記

if (object_id('log', 'U') is not null)
drop table log
go
create table log(
id int identity(1, 1) primary key,
action varchar(20),
createDate datetime default getDate()
)
go
if (exists (select * from sys.objects where name = 'tgr_student_log'))
drop trigger tgr_student_log
go
create trigger tgr_student_log
on student
after insert, update, delete
as
if ((exists (select 1 from inserted)) and (exists (select 1 from deleted)))
begin
insert into log(action) values('updated');
end
else if (exists (select 1 from inserted) and not exists (select 1 from deleted))
begin
insert into log(action) values('inserted');
end
else if (not exists (select 1 from inserted) and exists (select 1 from deleted))
begin
insert into log(action) values('deleted');
end
go
--test
insert into student values('king', 22, 1, 7);
update student set sex = 0 where name = 'king';
delete student where name = 'king';
select * from log;
select * from student order by id;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved