我有两个计划。如果从(appointments)计划中删除了约会并将其添加到(archive_appointments),我希望使用触发器
CREATE TABLE "APPOINTMENTS"
( "APPOINTMENTS_ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE NOT NULL ENABLE,
"PATIENT_FK" NUMBER,
"USER_FK" NUMBER,
"CLINIC_FK" NUMBER,
"DOCTOR_FK" NUMBER,
"APPOINTMENT_DATE" TIMESTAMP (6),
"APPOINTMENT_DETAILS" VARCHAR2(255 CHAR),
"ARCHIVE_FK" NUMBER,
CONSTRAINT "APPOINTMENTS_PK" PRIMARY KEY ("APPOINTMENTS_ID")
USING INDEX ENABLE
) ;
ALTER TABLE "APPOINTMENTS" ADD CONSTRAINT "APPOINTMENTS_ARCHIVE_FK" FOREIGN KEY ("ARCHIVE_FK")
REFERENCES "ARCHIVE_APPOINTMENTS" ("ARCHIVE_ID") ENABLE;
ALTER TABLE "APPOINTMENTS" ADD CONSTRAINT "APPOINTMENTS_CLINIC_FK" FOREIGN KEY ("CLINIC_FK")
REFERENCES "CLINICS" ("CLINIC_ID") ENABLE;
ALTER TABLE "APPOINTMENTS" ADD CONSTRAINT "APPOINTMENTS_DOCTOR_FK" FOREIGN KEY ("DOCTOR_FK")
REFERENCES "DOCTORS" ("DOCTOR_ID") ENABLE;
ALTER TABLE "APPOINTMENTS" ADD CONSTRAINT "APPOINTMENTS_PATIENT_FK" FOREIGN KEY ("PATIENT_FK")
REFERENCES "PATIENT" ("PATIENT_ID") ENABLE;
ALTER TABLE "APPOINTMENTS" ADD CONSTRAINT "APPOINTMENTS_USERS_FK" FOREIGN KEY ("USER_FK")
REFERENCES "USERS" ("USERS_ID") ENABLE;
CREATE TABLE "ARCHIVE_APPOINTMENTS"
( "ARCHIVE_ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE NOT NULL ENABLE,
"APPOINTMENTS_FK" NUMBER,
"PATIENT_FK" NUMBER,
"CLINIC_FK" NUMBER,
"DOCTOR_FK" NUMBER,
"ARCHIVE_DATE" DATE,
"ARCHIVE_STATUS" VARCHAR2(255),
CONSTRAINT "ARCHIVE_APPOINTMENTS_PK" PRIMARY KEY ("ARCHIVE_ID")
USING INDEX ENABLE
) ;
ALTER TABLE "ARCHIVE_APPOINTMENTS" ADD CONSTRAINT "ARCHIVE_APPOINTMENTS_FK" FOREIGN KEY ("APPOINTMENTS_FK")
REFERENCES "APPOINTMENTS" ("APPOINTMENTS_ID") ENABLE;
字符串
触发器:
CREATE OR REPLACE EDITIONABLE TRIGGER "DELETE_AND_INSERT_TRIGGER"
AFTER DELETE ON APPOINTMENTS
FOR EACH ROW
BEGIN
-- Step 1: Delete the corresponding record from the target table.
DELETE FROM APPOINTMENTS
WHERE APPOINTMENTS_id = :OLD.APPOINTMENTS_id;
-- Step 2: Insert the deleted data into the target table.
INSERT INTO archive_appointments
(appointments_fk, patient_fk, clinic_fk, doctor_fk, archive_date ,archive_status)
VALUES
(:old.appointments_id, :old.patient_fk, :old.clinic_fk, :old.doctor_fk, SYSDATE, 'DONE');
END;
/
型
但警报错误为:ORA-04098:触发器“*.APPOINTMENTS_T”无效,重新验证失败
我没有办法
1条答案
按热度按时间zdwk9cvp1#
我删除了这个例子中的所有外键约束:
create table
语句),archive_appointments
到appointments
创建外键没有意义,因为一旦从appointments
删除行,就没有什么可引用的了)从触发器开始:不要从已经受到该操作影响的表中删除; Oracle将自动执行此操作。
所以:首先是table:
字符串
触发器:
型
测试:
型
从
appointments
删除行:型
新表格的内容:
型