更新后的mysql触发器无效

时间:2014-05-23 12:54:55

标签: mysql sql

这个触发器应该更新医院可用的免费房间。当病人离开触发器时再增加一个免费房间。但它不起作用,我真的不明白为什么。我使用这些表格这个触发器:

create table Incidents
(primary key(incident_code),
foreign key(patient_code) references Patients(patient_code)on delete cascade on update cascade,
foreign key(section_code) references Sections(section_code)on delete cascade on update cascade,
foreign key(doctor_code) references Doctors(doctor_code)on delete cascade on update cascade,
incident_code int unsigned,
patient_code int unsigned,
section_code int unsigned,
doctor_code int unsigned,
import_day date not null,
export_day date,
incident_value real not null
check(datediff(export_day,import_day)>-1));

(primary key(section_code),
section_code int unsigned,
section_name varchar(30) not null,
total_rooms int not null,
free_rooms int not null
check(total_rooms>=free_rooms));

这是触发器代码:

delimiter @
create trigger FreeRooms1
after update on incidents
for each row
begin
 if (old.export_day=null and new.export_day != null)
 then
   update sections
   set free_rooms = free_rooms + 1   
   where sections.section_code = incidents.section_code;
 end if;
end;
@

这就是我尝试激活它的方式:

update incidents
set export_day = '2013/12/24'
where incident_code = 2;

select section_code,section_name,free_rooms
from sections;

我为触发器尝试了很多变化但是我做的任何改变都没有起作用。有什么帮助吗? 对不起,如果我的问题看起来很糟糕,我对mySQL相当新,我无法在任何地方找到答案。

1 个答案:

答案 0 :(得分:0)

我认为你应该使用:

delimiter @
create trigger FreeRooms1
after update on incidents
for each row
begin
 if (old.export_day IS NULL and new.export_day IS NOT NULL)
 then
   update sections
   set free_rooms = free_rooms + 1   
   where sections.section_code = NEW.section_code;
 end if;
end;
@

并引用新行。