编写表的触发器以自动更新另一个表

时间:2016-12-18 16:13:22

标签: sql oracle plsql triggers

我有这些表(Oracle):

Doctors (Doctor_ID (PK),
         Doctor_Name,
         DoB,
         Specialization)

Doctors_At_Work (Doctor_ID (PK),
                 The_Date (PK),
                 Hour_Start (PK),
                 Hour_Stop,
                 Room)

Consultations_Intervals (Doctor_ID (PK),
                        The_Date (PK),
                        Start_Hour_Consult (PK),
                        Stop_Hour_Consut,
                        Room)

OBS: A consultation last for only 30 minutes.

我的任务是创建Doctors_At_Work的必要触发器(插入/更新/删除),以便自动更新Consultations_Intervals表。

到目前为止我做了什么:

Create sequence seq_id_doctor START with 1 INCREMENT BY 1 ORDER NOCACHE;

CREATE OR REPLACE trigger t_1
        BEFORE INSERT ON Doctors_At_Work FOR EACH ROW
BEGIN
   :NEW.Doctor_ID:=seq_id_doctor.NextValue;
   :NEW.The_Date:=CURRENT_TIMESTAMP;
   :NEW.Hour_Start:=Select Extract (Hour from CURRENT_TIMESTAMP);
END;
/

CREATE OR REPLACE trigger t_2
        AFTER INSERT OR UPDATE ON Consultations_Intervals FOR EACH ROW
BEGIN
INSERT INTO Consultations_Intervals VALUES 
(:NEW.Doctor_ID, :NEW.The_Date, :NEW.Hour_Start, :NEW.Hour_Start + 
  interval '30' minute, :NEW.Room);
END;
/

它出了什么问题?我该如何解决这个问题? (如果有其他想法)

1 个答案:

答案 0 :(得分:1)

您需要在Uses ShellApi begin ShellExecute (0, 'Open', 'link website', '', '', SW_SHOWNORMAL); end; 表上使用简单的触发器。见下面的例子:

表:

Doctors_At_work

触发:

create  table Doctors_At_Work (Doctor_ID number Primary key,
                 The_Date date ,
                 Hour_Start date     ,
                 Hour_Stop date,
                 Room number);

create table Consultations_Intervals (Doctor_ID number,
                        The_Date date ,
                        Start_Hour_Consult number,
                        Stop_Hour_Consut number,
                        Room number);

输出:

create or replace trigger t_1
   before insert or update or delete on doctors_at_work
   for each row
begin
   insert into consultations_intervals (doctor_id,
                                        the_date,
                                        start_hour_consult,
                                        stop_hour_consut,
                                        room)
        values (:new.doctor_id,
                current_timestamp,
                extract (minute from current_timestamp),
                extract (minute from current_timestamp),
                :new.room);
end;
/