更新后mysql触发器不起作用

标签 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 还很陌生,而且我在任何地方都找不到答案。

最佳答案

我认为你应该使用:

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/

相关文章:

mysql - 如何从出勤表中获取出勤率最高的student_id?

mysql - 变量 time_zone 不能设置为 NULL 的值

php - 下拉列表不显示数据

mysql - 更改 SQL 中的日期

sql - 拆分列中的字符串并在 sql 中进行透视

javascript - 使用 php mysql jquery ajax 更新选择框的值

mysql - 高效分类Mysql结构

mysql - 在一列中找出时间范围

sql - SELECT 列表中子查询内的不同 LISTAGG

mysql - 如何选择金额总和等于某个值并按特定列(发件人或收件人)分组的所有记录?