IT Community - Software Programming, Web Development and Technical Support

Trigger in MySql

This is a discussion on Trigger in MySql within the Database Support forums, part of the Web Development category; Hi, I want to implement trigger in mysql for my site. I am very new to this. can anyone guide ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-18-2008, 11:49 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Trigger in MySql

Hi,
I want to implement trigger in mysql for my site. I am very new to this.

can anyone guide me to do this?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-25-2008, 05:05 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

So finally i have found something to do with triggers

Code:
DELIMITER $$

CREATE TRIGGER newsCounter
AFTER INSERT ON News
FOR EACH ROW BEGIN
 INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);
END;
$$
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-25-2008, 05:06 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

Triggers are programmable events that react to queries and reside directly on the database server. Triggers can be executed before or after INSERT, UPDATE or DELETE statements.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-25-2008, 05:07 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

There are several reasons to use triggers. Triggers can automate a lot of stuff that you may have been doing by hand before. The main reason I use triggers is to maintain the integrity of one table that might rely on another table. For example, if TableA references TableB and a value from TableB gets deleted, you could have a trigger setup to handle the data in TableA. Perhaps the data in TableA should be reset to a generic value or maybe the data should just be deleted completely.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-25-2008, 05:17 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

You can only have one trigger per event, per table. In other words, you can't have two triggers that occur AFTER INSERT ON NEWS, but you can have one BEFORE INSERT ON NEWS and another AFTER INSERT ON NEWS.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-25-2008, 05:18 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

You also can't apply a single trigger to multiple events. For example, in Microsoft SQL you can create one trigger that will run after an INSERT or an UPDATE. Unfortunately in MySQL 5.0, you must create an individual trigger for both the INSERT and UPDATE. If you do plan on having the same code run on an INSERT and an UPDATE, it might be better to have your triggers call a stored procedure, so you can keep your code centralized.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-12-2008, 02:34 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

Build Table

You may want to run the following CREATE TABLE statements on a test database, if you plan on following along with the examples. The examples use three tables: NewsCategories, News and NewsCount.

Code:
CREATE TABLE NewsCategories
( catID int not null auto_increment, catName varchar(32), primary key(catID));

CREATE TABLE News
( newsID int not null auto_increment, catID int not null, title
varchar(32) not null, txt blob, primary key(newsID));

CREATE TABLE NewsCount
( newsItemCount int );
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-12-2008, 02:35 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

CREATING A TRIGGER

Triggers are relatively easy to create. Let's take a look at the basic syntax of the CREATE TRIGGER statement:

Code:
DELIMITER $$

CREATE TRIGGER newsCounter
AFTER INSERT ON News
FOR EACH ROW BEGIN
 INSERT INTO NewsCount (newsItemCount) (SELECT count(*) FROM News);
END;
$$
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 05-12-2008, 02:37 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

The above trigger will occur after an insert takes place on the 'News' table. When a new item is added to the News table, an INSERT is triggered, adding the count of news items to the NewsCount table.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 05-12-2008, 02:38 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Trigger in MySql

There's a couple of basics you need to understand about the syntax. First, the trigger can occur either before or after your query. If you create a trigger that handles INSERTs, you can set it up to run before the database has had the record added, or after. If you want to manipulate data based on a newly inserted AUTO_INCREMENT value, you would have to run it AFTER the INSERT. The NEW keyword acts like a virtual table of the values that are being manipulated on UPDATE and INSERT triggers.
For example, NEW.Title corresponds to the Title field that is replacing (or has replaced) the old value. There is also an OLD keyword that is similar to the NEW keyword, only it uses the old value that had been, or is about to be, updated. The OLD keyword can only be applied to UPDATE and DELETE triggers.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I setup a clock with defined alarm times which trigger another event? kingmaker HTML, CSS and Javascript Coding Techniques 1 01-19-2008 03:18 AM
Can we have a commit statement inside a trigger? if no why cant we? Mramesh Database Support 6 09-11-2007 02:08 AM
Run an exe from an oracle trigger H2o Database Support 5 08-09-2007 10:55 AM
Storing the output from trigger in a text file sureshbabu Database Support 3 07-27-2007 05:44 AM
how to create trigger in MYSQL Jeyaseelansarc Database Support 5 07-10-2007 10:53 PM


All times are GMT -7. The time now is 08:53 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0