基本触发工作

时间:2014-04-18 04:38:33

标签: mysql

我有两个表,员工和部门

员工表格包含以下列

Name
SSN
Salary
Dept_No
Supervisor_SSN

Department Table有以下列

Dept_Name
Dept_No
Total_Salary
Manager_SSN

我需要编写一个触发器,以便在插入新的Employee时,我们更新Departments Total薪水以包含新员工的薪水。这就是我想出来的,但显然是“在哪里”#39;在触发器中不允许。任何帮助表示赞赏。

USE `mydb`;
DELIMITER $$
CREATE TRIGGER `Employee_BINS` BEFORE INSERT ON `Employee` FOR EACH ROW
WHERE NEW.dno = department.Dno
SET department.total_sal = department.total_sal + NEW.salary;

我得到的错误是

syntax error, unexpected WHERE

2 个答案:

答案 0 :(得分:0)

FOR EACH ROW之后,您应该像编写存储过程一样编写正常的sql语句。

详细了解语法:https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

答案 1 :(得分:0)

您创建的正常SQL查询应该像sample format

一样提及

您应该提及delimiterfor each row

的正确格式
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
    -> FOR EACH ROW
    -> BEGIN
    ->     IF NEW.amount < 0 THEN
    ->         SET NEW.amount = 0;
    ->     ELSEIF NEW.amount > 100 THEN
    ->         SET NEW.amount = 100;
    ->     END IF;
    -> END;//
mysql> delimiter ; 

<强> https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

<强> https://dev.mysql.com/doc/refman/5.0/en/create-trigger.html