Publishing System Settings Logout Login Register
Basic Database Trigger
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 19th, 2010
1543 views
MySQL

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,
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
karabibe

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top