X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 8AD79D1D2E7 for ; Sat, 31 Jan 2004 15:25:57 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 48141-09 for ; Sat, 31 Jan 2004 11:26:00 -0400 (AST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 421C1D1D2F7 for ; Sat, 31 Jan 2004 11:25:56 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 114C5357F7; Sat, 31 Jan 2004 07:26:01 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 0FD2E357F3; Sat, 31 Jan 2004 07:26:01 -0800 (PST) Date: Sat, 31 Jan 2004 07:26:01 -0800 (PST) From: Stephan Szabo To: beyaRecords - The home Urban music Cc: pgsql-sql@postgresql.org Subject: Re: Mechanics of Update:Cascade In-Reply-To: <3968B922-53DB-11D8-A6EB-0003938366A4@beya-records.com> Message-ID: <20040131072341.B91270@megazone.bigpanda.com> References: <3968B922-53DB-11D8-A6EB-0003938366A4@beya-records.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/291 X-Sequence-Number: 16684 On Sat, 31 Jan 2004, beyaRecords - The home Urban music wrote: > Hi, > could someone please explain to me the mechanics of an UpDate:Cascade? > Delete:Cascade I fully understand but not Update. I have 2 tables A and > B. On B I have created a foreign key on user_id in both B and A for > Update and Delete cascade. If I delete from A it deletes all from B. If > I update A what happens in B? Basically it means that if you update the referenced key in A, the associated referencing values in B changes as well so as to attempt to keep the associations the same. Here's an example: sszabo=# create table a(a int primary key, b int); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a" CREATE TABLE sszabo=# create table b(a_key int references a on update cascade, b int); CREATE TABLE sszabo=# insert into a values (3, 4); INSERT 535937 1 sszabo=# insert into a values (4, 5); INSERT 535938 1 sszabo=# insert into b values (3,104); INSERT 535939 1 sszabo=# insert into b values (4,105); INSERT 535940 1 sszabo=# select * from a; a | b ---+--- 3 | 4 4 | 5 (2 rows) sszabo=# select * from b; a_key | b -------+----- 3 | 104 4 | 105 (2 rows) sszabo=# update a set a=a+100; UPDATE 2 sszabo=# select * from a; a | b -----+--- 103 | 4 104 | 5 (2 rows) sszabo=# select * from b; a_key | b -------+----- 103 | 104 104 | 105 (2 rows)