删除更新元组的sqlite触发器与另一个表的值?

时间:2016-05-26 19:05:57

标签: sql sqlite triggers sql-update

我有两张桌子: 客户(id,name,advisorID)和 员工(id,name,managerID)

从员工表中删除员工时,如何触发更新客户顾问ID为已删除员工managerID的值?

到目前为止,我有:

CREATE TRIGGER updateAdvisor BEFORE DELETE ON Employee
   BEGIN
      UPDATE Customer set Customer.advisorID = Employee.managerID 
      from Employee
      WHERE Employee.managerID IN(
          SELECT managerID
          FROM Employee)

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

UPDATE语句适用于单个表;你必须用correlated subquery

查找新值
UPDATE Customer
SET advisorID = (SELECT managerID
                 FROM Employee
                 WHERE Employee = OLD.EmployeeID)
...

此外,您只想更新那些实际拥有待删除员工的客户作为顾问:

...
WHERE advisorID = OLD.EmployeeID;

答案 1 :(得分:0)

这很棒!

CREATE TRIGGER updateAdvisor BEFORE DELETE ON employee
BEGIN
UPDATE customer
SET advisorID = (SELECT managerID FROM employee WHERE employeeID = old.employeeID)
WHERE advisorID = old.employeeID;
END;