Re: delete cascade question - Mailing list pgsql-admin

From Greg Sabino Mullane
Subject Re: delete cascade question
Date
Msg-id 4c1150790b46c0b4d7dead564c4b99aa@biglumber.com
Whole thread Raw
In response to delete cascade question  (Glenn MacGregor <gtm@highstreetnetworks.com>)
Responses Re: delete cascade question  (Glenn MacGregor <gtm@highstreetnetworks.com>)
List pgsql-admin
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> I am using a cascade delete on the following table:
> ...
> 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 have
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 greg@turnstep.com
PGP Key: 0x14964AC8 200404262305
-----BEGIN PGP SIGNATURE-----

iD8DBQFAjc4QvJuQZxSWSsgRAtS8AKCjEkiZ5uFn6y88eqQX9/OoT/TbcACgs1qJ
FnC9Q9O0qkljz7sLTY7Czhw=
=RPTN
-----END PGP SIGNATURE-----



pgsql-admin by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: [PERFORM] is a good practice to create an index on the oid?
Next
From: Dave Cramer
Date:
Subject: Re: [JDBC] [PERFORM] is a good practice to create an index on the