First, Trigger
1. Triggers are stored as separate objects in the database,
2. Triggers do not need to be invoked, it is triggered by an event to run
3. Triggers cannot receive parameters
--Application of Trigger
For example: NET, net, Facebook, when you send a log, automatically notify friends, in fact, in addition to the log to do a start, and then write entries to the table.
--the efficiency of the trigger is very high
For example: forum post, each insert a post will want to the layout of the final posting time, the total number of posts synchronized updates, when the use of trigger efficiency will be very high.
Oracle uses Pl/sql to write triggers
1.--pl/sql general syntax for creating triggers
create [or replace] trigger trigger_name
{before | after}
{Insert | delete | update [of column[,column ...]]} on TABLE_NAME
[for each row]
[where condition]
--trigger_body;
Begin End
;
2.--Practice
--Question 3. Use: Old and: new operator
Create or replace trigger tri_update after update on employees for each
row
b Egin
dbms_output.put_line (' before update: ' | |:o ld.salary| | ' Updated: ' | |:new.salary;
End;
--Question 2. Write a trigger that prints ' hello '
create or replace trigger tri_update after
insert on EMP
when inserting records into the EMP table Begin
Dbms_output.put_line (' OK ');
End;
--Question 1. A HelloWorld-level trigger
-Creates a trigger that triggers the Create
or replace trigger tri_update after the update of the Employees table
Update on employees
to each row--you want to print an OK at the end of the last execution, remove the phrase from the
begin
dbms_output.put_line (' OK ');
End;
--Execute
Update employees
Set salary = salary+1
where department_id = 80
Third, the use of triggers in MySQL
--Suppose there are two tables board and article
CREATE TABLE board (
ID int primary key auto_increment,
name varchar (),
ar Ticlecount int
);
CREATE TABLE article (
ID int primary key auto_increment,
title varchar (m),
bid int references Board (ID)
);
--Create a trigger
delimiter $$
create trigger Insertarticle_trigger after insert on article for each
row< C16/>begin
Update Board set articlecount=articlecount+1
where id = new.bid;
End;
$$
delimiter;
This trigger inserts into
board values (null, ' Test_boardname ', 0)
when we perform an insert operation on the article table. INSERT into article values (NULL, ' Test_title ', 1);
-After this INSERT statement is executed, the value of the Articlecount field in the board table is returned to +1, which is completed by the trigger.
The above is a small set to introduce the Oracle use triggers and MySQL use triggers in the case of comparison, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!