![]() Database Trigger for Basic okay, the first i will explain about database trigger. Database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for keeping the integrity of the information on the database. For example, when a new record (representing a new worker) is added to the employees table, new records should be created also in the tables of the taxes, vacations, and salaries. A trigger is defined to activate when an INSERT, DELETE, or UPDATE statement executes for the associated table. A trigger can be set to activate either before or after the triggering statement. For example, you can have a trigger activate before each row that is inserted into a table or after each row that is updated. let'sstart with the following example:
create database mytrigger; create table test ( code varchar(5) not null,name varchar(35) not null, primary key (code)) create table test2( code varchar(5) not null,namevarchar(35) not null, primary key (code)) create table tran ( code varchar(5) not null, trancode varchar(5),summary double, primary key ncode(code,trancode))
Create the following trigger
delimiter $$ create trigger auto_insert_test2 before insert on test for each row begin insert into test2 (code,name) values (NEW.code,NEW.name); end$$ create trigger auto_update_test2 before update on test for each row begin update test2 set name=NEW.name where code=NEW.code; end$$ create trigger auto_delete_test2 before delete on test for each row begin delete from test2 where code=OLD.code; delete from tran where code=OLD.code; end$$ Result:
every time there was a change in the 'test' table,'test2' table will be changed automatically
Regards,
Karabibe,
|




















