这个触发器应该更新医院可用的空闲房间。当病人离开时,触发器会增加一个空闲房间。但它不起作用,我真的不明白为什么。我使用这些表作为这个触发器:
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 还很陌生,而且我在任何地方都找不到答案。
最佳答案
我认为你应该使用:
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;
@
并引用新行。
关于更新后mysql触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23829859/