定義:
由insert、update、delete等事件來觸發某種特定的操作
建立單條執行語句的觸發器
語法:mysql :
create trigger 觸發器 before | after 觸發事件
on 表名 for each row 執行語句
mssql :
create trigger 觸發器名
on tablename 觸發事件
as 執行語句
oracle :
create trigger 觸發器名
觸發事件
on 作用物件
as 執行語句
eg:
mysql :
create
trigger dept_trig1 before insert
on department for each row
insert
into trigger_time values
(now()
);mssql :
create
trigger dept_trig1
on department
forinsert
asinsert
into trigger_time values
(getdate())
;
建立多條執行語句的觸發器
語法:create trigger 觸發器名 before|after 觸發事件
on 表名 for each row
begin
執行語句
end注:在執行多條語句時,會以分號結束,為解決問題,delimiter#
eg:
create
trigger dept_trig2 after
delete
on department for each row
begin
insert
into trigger_time values
('22:36:36');
insert
into trigger_time values
('23:56:56');
end#
show triggers
eg: show triggers \g;
通過triggers 表檢視
eg:
select
*from information_schema.triggers \g;
查詢指定的
eg:
select
*from information_schema.triggers where trigger_name =
'dept_trig1' \g;
通過sysobjects表條件xtype='tr』檢視
eg:
select
*from sysobjects where xtype=
'tr'
通過sp_helptext檢視
eg:
exec sp_helptext 'dept_trig1'
通過user_objects 表條件object_type=』tgrigger』檢視
eg:
select
*from user_objects where lower(object_type)
='tgrigger'
通過user_triggers 檢視 ;查詢指定的
eg:
select
*from user_triggers where trigger_name = upper(
'trg_stu_log'
);
觸發器執行順序:
before觸發器 -> 表操作(insert|update|delete) -> after觸發器
注:乙個表在相同觸發事件的相同觸發事件,只能建立乙個觸發器
eg:
mysql
create
trigger before_insert before insert
on department for each row
insert
into trigger_test values
(null
,'after_insert');
mssql
create
trigger before_insert
on department
forinsert
asinsert
into trigger_time values
('23:10:10');
go;create
trigger after_insert
on department
after
insert
asinsert
into trigger_time values
('23:14:15'
);
注:建立觸發器時,對於執行語句中的values值不能少,哪怕資料庫有預設值或者自增,都要加上值,如:null;
eg:
drop
trigger dept_trig1;
觸發器語法
建立觸發器的語法如下所示 create trigger trigger name on table orview name with encryption execute as not for replication as sql statements external name assembly ...
sql 觸發器 直接遞迴觸發器
create trigger dbo loving20000 on dbo s for delete asdeclare age int select age sage from deleted delete s where sage age delete from s where sname xq...
觸發器六 系統觸發器 學習筆記
系統觸發器用於監視資料庫服務的開啟 關閉 錯誤等資訊的取得,或者是監控使用者的行為操作等。如果要建立系統觸發器,可以使用如下的語法 create or replace trigger 觸發器名稱 before after 資料庫事件 on database schema when 觸發條件 decl...