Greg Sabino Mullane
2004-04-27 03:04:51 UTC
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
to show us your actual tables and contraints. In general, "on delete cascade"
refers to what happens to foreign key rows when the referenced key is
deleted. Similarly, you can set a constraint as "on update cascade"
in which case the changes in the parent table are also made in the child
tables. Here is a quick example that may help:
CREATE TABLE "myparent" (
"a" INTEGER,
"b" TEXT unique
);
CREATE TABLE "mychild" (
"c" INTEGER,
"d" TEXT NOT NULL
);
ALTER TABLE "mychild" ADD CONSTRAINT "mychild_d_fk"
FOREIGN KEY (d) REFERENCES myparent(b)
ON DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO myparent(a,b) VALUES (1,'Garibaldi');
INSERT INTO myparent(a,b) VALUES (2,'Zathras');
INSERT INTO myparent(a,b) VALUES (3,'G''Kar');
- -- This fails, because the foreign key constraint catches the typo:
INSERT INTO mychild(c,d) VALUES (1,'Garabaldi');
INSERT INTO mychild(c,d) VALUES (9,'Garibaldi');
INSERT INTO mychild(c,d) VALUES (10,'Zathras');
INSERT INTO mychild(c,d) VALUES (11,'Zathras');
SELECT * FROM mychild;
greg=# SELECT * FROM mychild;
c | d
- ----+-----------
9 | Garibaldi
10 | Zathras
11 | Zathras
UPDATE myparent SET b = 'Chief' WHERE b='Garibaldi';
- -- ON UPDATE CASCADE has changed the name in both tables:
SELECT * FROM mychild;
c | d
- ----+---------
10 | Zathras
11 | Zathras
9 | Chief
DELETE FROM myparent WHERE b = 'Zathras';
- -- ON DELETE CASCADE has removed all the Zathras's
SELECT * FROM mychild;
c | d
- ---+-------
9 | Chief
- --
Greg Sabino Mullane ***@turnstep.com
PGP Key: 0x14964AC8 200404262305
-----BEGIN PGP SIGNATURE-----
iD8DBQFAjc4QvJuQZxSWSsgRAtS8AKCjEkiZ5uFn6y88eqQX9/OoT/TbcACgs1qJ
FnC9Q9O0qkljz7sLTY7Czhw=
=RPTN
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ***@postgresql.org)
Hash: SHA1
...
So the delete cascade states if I am deleting a row whose vname
matches the parentname delete those rows and it works fine.
...
This works, so I tried to put that in the cascade but it failed.
Not sure what you mean by "put that in the cascade." You will haveSo the delete cascade states if I am deleting a row whose vname
matches the parentname delete those rows and it works fine.
...
This works, so I tried to put that in the cascade but it failed.
to show us your actual tables and contraints. In general, "on delete cascade"
refers to what happens to foreign key rows when the referenced key is
deleted. Similarly, you can set a constraint as "on update cascade"
in which case the changes in the parent table are also made in the child
tables. Here is a quick example that may help:
CREATE TABLE "myparent" (
"a" INTEGER,
"b" TEXT unique
);
CREATE TABLE "mychild" (
"c" INTEGER,
"d" TEXT NOT NULL
);
ALTER TABLE "mychild" ADD CONSTRAINT "mychild_d_fk"
FOREIGN KEY (d) REFERENCES myparent(b)
ON DELETE CASCADE ON UPDATE CASCADE;
INSERT INTO myparent(a,b) VALUES (1,'Garibaldi');
INSERT INTO myparent(a,b) VALUES (2,'Zathras');
INSERT INTO myparent(a,b) VALUES (3,'G''Kar');
- -- This fails, because the foreign key constraint catches the typo:
INSERT INTO mychild(c,d) VALUES (1,'Garabaldi');
INSERT INTO mychild(c,d) VALUES (9,'Garibaldi');
INSERT INTO mychild(c,d) VALUES (10,'Zathras');
INSERT INTO mychild(c,d) VALUES (11,'Zathras');
SELECT * FROM mychild;
greg=# SELECT * FROM mychild;
c | d
- ----+-----------
9 | Garibaldi
10 | Zathras
11 | Zathras
UPDATE myparent SET b = 'Chief' WHERE b='Garibaldi';
- -- ON UPDATE CASCADE has changed the name in both tables:
SELECT * FROM mychild;
c | d
- ----+---------
10 | Zathras
11 | Zathras
9 | Chief
DELETE FROM myparent WHERE b = 'Zathras';
- -- ON DELETE CASCADE has removed all the Zathras's
SELECT * FROM mychild;
c | d
- ---+-------
9 | Chief
- --
Greg Sabino Mullane ***@turnstep.com
PGP Key: 0x14964AC8 200404262305
-----BEGIN PGP SIGNATURE-----
iD8DBQFAjc4QvJuQZxSWSsgRAtS8AKCjEkiZ5uFn6y88eqQX9/OoT/TbcACgs1qJ
FnC9Q9O0qkljz7sLTY7Czhw=
=RPTN
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ***@postgresql.org)