2014-05-23 3 views
0

이 트리거는 병원에서 사용 가능한 무료 객실을 업데이트해야합니다. 환자가 방아쇠를 당기면 방이 하나 더 추가됩니다.하지만 방금 작동하지 않아 실제로 이유를 이해할 수 없습니다.업데이트 후 mysql 트리거가 작동하지 않습니다

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; 
@ 

을하고 새 행을 참조 :

+1

'='또는'! ='를 사용하여'NULL '을 테스트 할 수 없습니다.'old.export_day가 null이고 new.export_day가 null이 아닌 경우'를 사용해야합니다. –

+0

고마워요. 이 특별한 문법은 당신의 도움없이 그것을 결코 발견하지 못했습니다. – DieCriminal

+0

설명서를 확인하십시오 : http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html –

답변

0

내가 사용한다고 생각합니다.

+0

고맙습니다. 귀하의 답변은 다음과 같습니다 : old.export_day = null new.export_day! = null 이는 다음과 같아야합니다. old.export_day가 null이고 new.export_day가 null이 아닙니다. export_day.But 만 변경 했으므로 섹션 코드에서 new를 사용해야한다는 것을 알지 못했습니다. 그것은 어쨌든 전체 행을 업데이 트하는 것 같습니다. – DieCriminal

+0

그에 따라 답변을 편집했습니다. – GhostGambler

관련 문제