數據庫觸發器

觸發器只能由用戶對DB中表的操作(即插入,刪除和修改3中操作觸發),因此可以用觸發器維護表的一致性。

Note

觸發器不僅可以用來維護表數據的一致性,還可以實現更加複雜的操作

觸發器分爲AFTER觸發器和INSTEAD OF觸發器兩種
AFTER觸發器,表示是在增,刪,改這些操作之後進行執行。AFTER只能建立在表上,每個觸發操作可以定義多個AFTER觸發器,可以中sp_settriggerorder指定第一個和最後一個AFTER觸發器的觸發順序,其他的則不確定。
INSTEAD OF 觸發器可在表上或者視圖上定義,每個觸發操作只能定義一個INSTEAD OF 觸發器。INSTEAD OF觸發器主要是用來替換觸發的操作(增,刪,改),即不執行觸發操作,而是執行觸發器。
觸發器語法:
CREATE TRIGGER 觸發器名
ON {表名|視圖名}
{FOR|AFTER|INSTEAD OF} {[INSERT][,][UPDATE][,][DELETE]}
AS
SQL語句塊
RETURN

Note

一旦某操作觸發了某個觸發器,系統就會將該操作與該操作的觸發器作爲一個事物進行提交或回滾。
系統爲觸發器提供兩張表,即inserted和deleted表,其表結構與定義觸發器的表結構一致,以便程序員編程時應用。當觸發操作是“插入”時,新數據會寫入到inserted;當觸發操作是“刪除”時,刪除的操作會保存在deleted表中;當觸發操作是“修改”時,會將先刪除的數據保存在deleted中,將新修改的數據保存在inserted中。

由刪除操作激發的主表刪除觸發器實例:

create trigger studentDel
on student 
after delete
as
if @@rowcount = 0 return
delete selectCourse from selectCourse as select,deleted d
where select.studentId=d.id
return

主表修改操作觸發器:

create trigger studentUp
on student
after update
as
declare @num_rows INT
select @num_rows =@@rowcount
if @num_rows=0 return
if update(id)
begin
    if @num_rows > 1
    begin
        RAISERROR 53333
        ROLBACK TRANSACTION
        RETURN
    end
        update selectCourse  set studentId=in.id from selectCourse as select,inserted as in,delectd as del
where select.studentId=del.id
    end
return

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table.Some uses for triggers are to perform checks of values to be inserted into a tabole or to perform caculations on values involved in an update.

A trigger is defined to activate when a statement inserts,updates or deletes row in the associated table.These row operations are trigger events.For example,rowscan be inserted by INSERT or LOAD DATA statements, and an insert trigger activates for each inserted row.A triger can be set to active either before or adter the trigger event.For ecample,you can have a trigger activate before each row that is inserted into a table or after each row that is updated.

Imporant

MySQL triggers activate only for changes made to tables by SQL statements.They do not activate for changes in views,nor by changes to tables made by APIS that do not transmit SQL statements to the MySQL Server.This means that triggers are not activated in the following tow cases:
- Triggers are not activated changes in INFORMATION_SCHEMA or performance_schema tables,because these tables are actually views.
- Triggers are not activated by updates made using the NDB API.

The following sections describe the syntax for creating and dropping triggers,show some examples of how to use them,and indicate how to obtain trigger metadata.

Trigger Syntax and Examples

To create a trigger or drop a trigger, use the CREATE TRIGGER or DROP TRIGGER statement.

create table account (acct_num int,amount decimal(10,2));
create trigger ins_sum 
before insert 
on account 
for each row 
set @sum=@sum+NEW.amount;

The statement following for each row defines the trigger body;that is ,the statement to execute each time the trigger activates,which occurs once for each row affected by the triggering event.In the example, the trigger body is a simple SET that accumulates into a user variable the values inserted into the amount column.The statement refers to the column as NEW.amount which means “the value of the amount column to be inserted into the new row.”
To use the trigger ,set the accumulator variable to zero, execute an INSERT statement,and then see what value the variable has afterward:

set @sum =0;
insert into account values(137,14,98),(141,1937.50)(97,-100.00);

In this case, the value of @sum after the INSERT statement has executed is 14.98+1937.50-100,or 1852.48.

To destroy the trigger,use a DROP TRIGGER statement .You must specify the schema name if the trigger is not in the default schema:
DROP TRIGGER test.ins_sum;
If you drop a table,any triggers for the table are also dropped.
Trigger names exist in the schema namespace,meaning that all triggers must have unique names within a schema.Triggers in different schemas can have the same name.
It is possible to define multiple triggers for a given table that have the same trigger event and action time.For example,you can have two BEFORE UPDATE triggers for a table.By default,triggers that have the same trigger event and action time activate in the order they were created.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章