数据库中有两张表,employee 和department, employee有主键uid, 外键did参考department.did。我添加了一个触发器:
create trigger hrms_employee_isrt_department after insert on employee
for each row
begin
if new.did=hrms.department.did then update hrms.department set num = num + 1;
end if;
end;
目的是在添加新雇员后,自动在部门(department)表中更新该部门的人数
触发器添加成功了,但是之后就不能在employee中insert了,会报错:
Error Code:1109 Unknown table 'hrms.department' in field list
(如果删除触发器,一模一样的插入就可以执行了,说明不是插入语句的问题)
这问题怎么解决啊,求大神们帮帮忙啊。。。
------解决思路----------------------
UPDATE语法错误。
SQL code
create trigger hrms_employee_isrt_department after insert on employee
for each row
begin
update hrms set department set num = num + 1 where did=new.did;
end if;
------解决思路----------------------
SQL code
create trigger hrms_employee_isrt_department after insert on employee
for each row
begin
if new.did!=null
then
update department set num=num+1 where did=new.did
end if;
end;
--转自