MySQL触发器在哪里写
我没有为我的作品写任何触发器。现在我想知道如何编写触发器以及在哪里编写它。是否可以在 phpmyadmin 中将触发器编写为 SQL 查询。
请帮我写一个简单的触发器...
我尝试如下
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 ;
提前致谢 尼散特
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...
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 phpMyAdmin 中,您可以在 SQL 窗口中创建触发器。
您可能必须将分隔符设置为“$$”之类的内容,而不是默认的“;”。您可以从 SQL 窗口底部轻松更改此设置。
phpMyAdmin 中的分隔符 http://img52.imageshack.us/img52/9144/phpmyadmin.jpg
此外,请确保使用
END
命令关闭触发器块,该命令在您的示例中缺失。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 http://img52.imageshack.us/img52/9144/phpmyadmin.jpg
In addition, make sure you close your trigger block with the
END
command, which is missing from your example.您应该查看 MySQL 文档中的触发器。
http://dev.mysql.com/doc/refman/5.5/ en/triggers.html
You should take a look at the MySQL documentation for triggers.
http://dev.mysql.com/doc/refman/5.5/en/triggers.html