我在哪里写MySQL触发器 [英] Where do I write MySQL trigger

查看:53
本文介绍了我在哪里写MySQL触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有为自己的作品写任何触发器.现在,我想知道如何编写触发器以及在哪里编写触发器.是否可以在phpmyadmin中将触发器编写为SQL查询.

I didn't write any trigger for my works. Now i want to know how to write trigger and where do I write it. Is it possible to write trigger as SQL query in phpmyadmin.

请帮助我编写一个简单的触发器...

Please help me to write a simple trigger...

我尝试如下

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 ;

先谢谢了 尼桑斯

推荐答案

在phpMyAdmin中,您可以在SQL窗口中创建触发器.

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

您可能必须将派尺设置为"$$"之类的值,而不是默认的;".您可以从SQL窗口的底部轻松更改此设置.

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.

phpMyAdmin中的分隔符http://img52.imageshack.us/img52/9144/phpmyadmin.jpg

此外,请确保使用END命令关闭触发块,该示例中没有此命令.

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

这篇关于我在哪里写MySQL触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆