Skip site navigation (1) Skip section navigation (2)

Re: delete cascade question

From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: delete cascade question
Date: 2004-04-27 03:04:51
Message-ID: 4c1150790b46c0b4d7dead564c4b99aa@biglumber.com (view raw or flat)
Thread:
Lists: 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(at)turnstep(dot)com
PGP Key: 0x14964AC8 200404262305
-----BEGIN PGP SIGNATURE-----
 
iD8DBQFAjc4QvJuQZxSWSsgRAtS8AKCjEkiZ5uFn6y88eqQX9/OoT/TbcACgs1qJ
FnC9Q9O0qkljz7sLTY7Czhw=
=RPTN
-----END PGP SIGNATURE-----



In response to

Responses

pgsql-admin by date

Next:From: kandiah ratnavaleDate: 2004-04-27 06:17:28
Subject: installation of postgresql-7.2.3-2PGDG
Previous:From: Christopher Kings-LynneDate: 2004-04-27 02:48:01
Subject: Re: [PERFORM] is a good practice to create an index on the oid?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group