MySQL触发器在哪里写

发布于 2024-08-22 08:00:35 字数 699 浏览 2 评论 0原文

我没有为我的作品写任何触发器。现在我想知道如何编写触发器以及在哪里编写它。是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-08-29 08:00:35

在 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.

难理解 2024-08-29 08:00:35

您应该查看 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文