Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 81FBD18E38D9 for ; Thu, 12 Jan 2012 12:56:49 -0400 (AST) Received: from smtp107.prem.mail.sp1.yahoo.com ([98.136.44.62]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RlNxG-0001uj-Q7 for pgsql-sql@postgresql.org; Thu, 12 Jan 2012 16:56:49 +0000 Received: (qmail 97896 invoked from network); 12 Jan 2012 16:56:32 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=G5Y3D8fxaVPfk/dNuAJDhHhsCE7vyWwNvTrvE9kUrgVpBVnXHb/E+gn76f5PLZJbtOYOZrV5+x+IVOMU4oUeXxKyf9qL/UiUDQYKzmH/zDLxlgjv9ILx3DPKEq/sjHogF0IkriDJMd6OtVkY9g6hvajld2urT/GviD0llqyaKgU= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1326387392; bh=XA7x4DlsHxshGX95BEetBVQ29zaOdldZ4j4bGQIMNgc=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:Thread-Index:Content-Language; b=DpW49bBCzE4dveect78hB+YEIOxzJKreg6OG4POVgZ75PKoTgWF3a1It/l8o/lqtRuyP3hC/mTn9975A2HCYbLjOHmEp9E2D+Yhwnrnvr9BrzoM/fGDuQ/4WGIomfl0tbKBbWUztDOuTsiAomUy6Nip5FrSx3or2tX6sGzRCOM8= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: J_zztW4VM1l.nXOXCrwW.PapxGR2pMoyIgp_xx.VssgQDuJ XMCCEM2dHcq42hywraU9kzSYNy2aCbmcjF._QuCILMzth0EQCrbBswQpCke. rt_Ob8brwL02m05QI89x1.WPDKQxuOiDCCz68nbTWtGZHqXLXOYhTFW28jp. oJCZ7DCM7hf57iUQuQd96ixqTLkazFTcK_gg_l4C5NIC3E4HtsjctJbESlpu kw6OE9Br4pO1moo4Ssg9qH7Xa55BrCOA2Yb_dGtzeplv5U_.KKQYsnw56h.m ty_E6tXsMhniiytfiwFmVA5_yggXqmDjWF4RC01d_aJbX61_fbzimaQuCZmL ZfdDo2mALeG5587BbvErGpu_XIUelAqzy1HzIiuTbLiWJzqdOdcl7_.bdhMn VWnWiFA-- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp107.prem.mail.sp1.yahoo.com with SMTP; 12 Jan 2012 08:56:32 -0800 PST From: "David Johnston" To: "'Carlos Mennens'" , "'PostgreSQL \(SQL\)'" References: In-Reply-To: Subject: Re: Unable To Modify Table Date: Thu, 12 Jan 2012 11:56:25 -0500 Message-ID: <016a01ccd14b$1f40baf0$5dc230d0$@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQGWi74kGEZ3ig7om2OmmMWFDJ8cXZZ1fHhw Content-Language: en-us X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201201/49 X-Sequence-Number: 36220 -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Carlos Mennens Sent: Thursday, January 12, 2012 11:43 AM To: PostgreSQL (SQL) Subject: [SQL] Unable To Modify Table I seem to have an issue where I can't modify a table due to another tables foreign key association: [CODE]trinity=# \d developers Table "public.developers" Column | Type | Modifiers --------------+----------------+----------- id | character(10) | not null name | character(50) | not null address | character(50) | city | character(50) | state | character(2) | zip | character(10) | country | character(50) | phone | character(50) | email | character(255) | Indexes: "developers_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "orders" CONSTRAINT "fk_orders_developers" FOREIGN KEY (id) REFERENCES developers(id) [/CODE] Now I want to change the formatting of field data in 'id' in table 'developers': [CODE]trinity=# SELECT id FROM developers; id ------------ 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 (109 rows) [/CODE] Now when I try and change the values before I alter the field TYPE, I get an error that another table (orders) with a foreign key associated with public.developers 'id' field still has old values therefor can't change / modify the 'developers' table. [CODE]trinity=# UPDATE developers SET id = '1000' WHERE id = '1000000001'; ERROR: update or delete on table "developers" violates foreign key constraint "fk_orders_developers" on table "orders" DETAIL: Key (id)=(1000000001) is still referenced from table "orders". [/CODE] How does one accomplish my goal? Is this difficult to change or once that foreign key is created, are you stuck with that particular constraint? --------------------------------------------------------------- There are two possible actions you can take with respect to an existing Primary Key; you can UPDATE it or you can DELETE it. When you define a FOREIGN KEY you can specify what you want to happen if the corresponding PRIMARY KEY is UPDATEd or DELETEd. Read the documentation on FOREIGN KEY in detail to understand why you are seeing that error and what modifications you can make to the FOREIGN KEY on "orders" to obtain different behavior. Keep in mind, also, that the TYPE of the PRIMARY KEY and FOREIGN KEY must match. Contrary to my earlier advice assigning a sequential ID (thus using a numeric TYPE) is one of the exceptions where you can use a number even though you cannot meaningfully perform arithmetic on the values. The reason you would use a numeric value instead of a character is that the value itself is arbitrary and the space required to store a number is less than the space required to store a string of the same length. There are many points-of-view regarding whether to use "serial" PRIMARY KEYs but regardless of whether you add one or not you should try and define a UNIQUE constraint on the table by using meaningful values. However, for things like Orders this is generally not possible and so you would want to generate a sequential identifier for every record. David J.