在sql server 2005中,觸發器可以分為兩大類:dml觸發器和ddl觸發器
dml觸發器:dml觸發器是當資料庫伺服器中發生資料操作語言(data manipulation language)事件時執行的儲存過程。dml觸發器又分為兩類:after觸發器和instead of觸發器
ddl觸發器:ddl觸發器是在響應資料定義語言(data definition language)事件時執行的儲存過程。ddl觸發器一般用於執行資料庫中管理任務。如審核和規範資料庫操作、防止資料庫表結構被修改等。
dml觸發器的分類
sql server 2005的dml觸發器分為兩類:
after觸發器:這類觸發器是在記錄已經改變完之後(after),才會被啟用執行,它主要是用於記錄變更後的處理或檢查,一旦發現錯誤,也可以用rollback transaction語句來回滾本次的操作。
instead of觸發器:這類觸發器一般是用來取代原本的操作,在記錄變更之前發生的,它並不去執行原來sql語句裡的操作(insert、update、delete),而去執行觸發器本身所定義的操作
create
table employees(
employee_id int
primary
keynot
null,
last_name varchar
(50)
null,
first_name varchar
(50)
null,
hire_date datetime
null,
job_id int
null,
email varchar
(500)
null,
department_id int
null,
salary int
null,
commission_pct int
null)
create
trigger employees_update
on employees
after update
asbegin
'有乙個員工更改了'
endcreate
trigger employees_insert
on employees
after insert
asbegin
'有乙個員工增加了'
endcreate
trigger employees_delete
on employees
after delete
asbegin
'有乙個員工刪除了'
end
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values
(12345,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
delete
employees
update
employees set last_name =
'dabing'
where employee_id=12345;
select
*from employees
select
commission_pct from employees where employee_id=12345;
drop
trigger employees_insert
create
trigger employees_insert
on employees
after insertas
begin
declare @employee_id int
set @employee_id =
(select employee_id from inserted)
if(@employee_id > 5 )
begin
'員工號大於的不能新增'
rollback
transaction
endend;
alter
trigger employees_insert
on employees
instead
ofinsertas
begin
declare @employee_id int
set @employee_id =
(select employee_id from inserted)
if(@employee_id > 5 )
begin
'員工號大於的不能新增'
endend;
drop
trigger employees_delete
create
trigger employees_delete
on employees
instead
ofdeleteas
begin
declare @employee_id int
set @employee_id =
(select employee_id from deleted)
if(@employee_id > 2 )
begin
'員工號大於的不能刪除'
endend;
delete
employees where employee_id =
'3'
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(1,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(2,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(3,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(4,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(5,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
insert
into employees(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
values(6,
'chen'
,'donny'
,'2010-05-08'
, 12,
,60,10000,25);
select
*from employees
九 資料庫mysql觸發器
9.1觸發器作用 當操作了某張表時,希望同時觸發一些動作 行為,可以使觸發器完成!例如 當向員工表插入一條記錄時,希望同時往日誌表插入資料 select from emp create table test log id int primary key auto increment,content ...
資料庫觸發器知識總結
create trigger trigger name trigger time trigger event on tbl name for each row trigger stmttrigger 觸發器關鍵字 trigger name 觸發器名稱 trigger time 觸發時機 before...
資料庫觸發器
觸發器是一種特殊型別的儲存過程,它不同於我們前面介紹過的儲存過程。觸發器主要是通過事件進行觸發而被執行的,而儲存過程可以通過儲存過程名字而 被直接呼叫。當對某一表進行諸如update insert delete 這些操作時,sql server 就會自動執行觸發器所定義的sql 語句,從而確保對資料...