views:

235

answers:

2

Hi all, I didn't write any trigger for my works. Now i want to know how to write trigger . And where it write . Is it possible to write trigger as sql query in phpmyadmin ..

Please help me to write a simple trigger...

I tried like below

Create Trigger sales_bi_trg
BEFORE INSERT ON sales
FOR EACH ROW
BEGIN
DECLARE num_row INTEGER ;
DECLARE tot_rows INTEGER ;
SELECT COUNT(*)
INTO tot_rows
FROM sales
WHERE employee_id = NEW.employee_id ;
   IF num_row > 0 THEN
       UPDATE perfomance 
    SET total_sales = NEW.sale_amt + total_sales,
    ave_sale = total_sales/(tot_rows + 1)
    WHERE employee_id = NEW.employee_id ;
   ELSE
   INSERT INTO perfomance
(employee_id, name, total_sales,ave_sale)
VALUES (NEW.employee_id, NEW.name, NEW.sale_amt, NEW.sale_amt) ;
   END IF ;

Thanks in advance Nisanth

A: 

You should take a look at the MySQL documentation for triggers.

http://dev.mysql.com/doc/refman/5.5/en/triggers.html

ar
+2  A: 

In phpMyAdmin you can create the trigger in the SQL window.

You may have to set the delimieter to something like "$$" instead of the default ";". You can change this easily from the bottom of the SQL window.

Delimiter in phpMyAdmin

In addition, make sure you close your trigger block with the END command, which is missing from your example.

Daniel Vassallo
not related to question or your answer - png screenshots will look better with less size ;-)
zerkms
Hi Daniel ,If u don't mind can u write a simple code . I tried lot of time but got some sql syntax error
Nisanth
@Nisanth: What syntax error are you getting?
Daniel Vassallo
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 20 This was my error
Nisanth
@Nisanth: I was going to comment about the `VALUES ()` syntax error, but now I see that you've updated your answer. In addition, make sure you close the trigger with the `END` command, which is missing from your example.
Daniel Vassallo
Yes.. Got it that was the problem . i forgot to close the trigger with END .. thank u very much Daniel
Nisanth