Subject | Referential integrity |
---|---|
Author | Alfred Thomas |
Post date | 2004-05-04T08:08:43Z |
Hi
I have the following tables:
CREATE TABLE "Attachment"
(
"AttachmentID" SMALLINT NOT NULL, //Primary key
"NoteID" INTEGER NOT NULL, //Primary key
"Attachment" BLOB SUB_TYPE 0 SEGMENT SIZE 80,
"Description" VARCHAR(50),
"FileName" VARCHAR(50)
);
CREATE TABLE "Notes"
(
"NoteID" INTEGER NOT NULL, //Primary key
"Description" VARCHAR(250),
"NoteDate" TIMESTAMP
);
I want the children(Attachment) to be deleted when the parent(Notes) are
deleted.
First I tried a Foreign key constraint:
ALTER TABLE "Attachment" ADD CONSTRAINT "FK_Attachment_Notes" FOREIGN KEY
("NoteID") REFERENCES "Notes" ("NoteID") ON DELETE CASCADE;
And then a trigger:
CREATE TRIGGER "Notes_BD0" FOR "Notes"
ACTIVE BEFORE DELETE POSITION 0
AS
begin
delete from "Attachment" where "Attachment"."NoteID" = "Notes"."NoteID";
end
But whenever I delete Notes the children(Attachment) isn't deleted.
Can anyone show me the correct way?
Regards
Alfred Thomas
I have the following tables:
CREATE TABLE "Attachment"
(
"AttachmentID" SMALLINT NOT NULL, //Primary key
"NoteID" INTEGER NOT NULL, //Primary key
"Attachment" BLOB SUB_TYPE 0 SEGMENT SIZE 80,
"Description" VARCHAR(50),
"FileName" VARCHAR(50)
);
CREATE TABLE "Notes"
(
"NoteID" INTEGER NOT NULL, //Primary key
"Description" VARCHAR(250),
"NoteDate" TIMESTAMP
);
I want the children(Attachment) to be deleted when the parent(Notes) are
deleted.
First I tried a Foreign key constraint:
ALTER TABLE "Attachment" ADD CONSTRAINT "FK_Attachment_Notes" FOREIGN KEY
("NoteID") REFERENCES "Notes" ("NoteID") ON DELETE CASCADE;
And then a trigger:
CREATE TRIGGER "Notes_BD0" FOR "Notes"
ACTIVE BEFORE DELETE POSITION 0
AS
begin
delete from "Attachment" where "Attachment"."NoteID" = "Notes"."NoteID";
end
But whenever I delete Notes the children(Attachment) isn't deleted.
Can anyone show me the correct way?
Regards
Alfred Thomas