Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z2pme-0001jN-Gi for pgsql-sql@arkaria.postgresql.org; Wed, 10 Jun 2015 23:51:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z2pmd-0002tU-Km for pgsql-sql@arkaria.postgresql.org; Wed, 10 Jun 2015 23:51:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z2pmb-0002tL-72 for pgsql-sql@postgresql.org; Wed, 10 Jun 2015 23:51:45 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z2pmS-0006T7-KG for pgsql-sql@postgresql.org; Wed, 10 Jun 2015 23:51:44 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 9F3A5204C7 for ; Wed, 10 Jun 2015 19:51:34 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute3.internal (MEProxy); Wed, 10 Jun 2015 19:51:34 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=y6HLw6McP+sGexAIdNc9ENoEJcQ=; b=LGYkna HWodFV2Of0vUsAcfXtUQJ7MfzP0KUuiGQcs1L2MDMY9fMBOmaXvK8MH4MhOHyBOK cKkVupWucmaTcK0fC70yKqcC3+KeXJvH6569nv3tUQbj+e98hrdeRIRAyILU5unH gzY52GHCWL472EdgJ/psnCvRp7fETPfJHN9Ss= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=y6HLw6McP+sGexA IdNc9ENoEJcQ=; b=DY2GDtWfojzKTDWJ0OhPIZATHL/3SXkrdUaiBa2iO6il4YZ uAYwh6IM4mgkaWAbPnM/NVSFwS5caYnbFr++aE8C6J6BCA9nv3X3bvBX8FFo2hEX ozS3WPphz0tV0zWtjWlEfqHqrqCfCJmVkddYj+VN8dRSYKuIhours643gbxs= X-Sasl-enc: OcdTH2awm3nZeG/8ME0QvVkKxXHu+OBonBr0YflHfjg7 1433980294 Received: from [192.168.1.2] (unknown [174.21.228.230]) by mail.messagingengine.com (Postfix) with ESMTPA id 1C95568007E; Wed, 10 Jun 2015 19:51:34 -0400 (EDT) Message-ID: <5578CD85.2050901@aklaver.com> Date: Wed, 10 Jun 2015 16:51:33 -0700 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Jon Forsyth , pgsql-sql@postgresql.org Subject: Re: Renumber Primary Keys and Update the same as Foreign Keys References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 06/10/2015 04:05 PM, Jon Forsyth wrote: > Hello all, > > I need to make a change to my schema such that the primary key index > numbers would change on multiple tables which are also used as foreign > keys in multiple tables. I want to update the foreign keys to the new > primary key index number of each record. I would prefer to do so using > SQL statements. > > My database is storing different kinds of questions in separate > tables--1. 'essay_questions' and 2. 'oral_questions' (more question > type tables are anticipated). To simplify relationships, I have created > a parent table called 'questions' that will have a one-to-one > relationship with each question type table using the same primary key on > 'question' and 'essay_question' (same for 'question' and > 'oral_question') for a given record. I will then associate different > media items (videos, sound files, images) with the parent question table > in a many-to-many relationship (many media items can belong to one > question). As it stands, the different question tables have duplicate > primary keys with respect to each other, so combining them into the > parent question table will require a change to several or all primary > keys. Additionally, I have live data where two tables 1. > 'essay_question_response' and 2. 'oral_question_response' are associated > in a many-to-many with their corresponding question tables which will > need the foreign keys updated after the change to primary keys. > > Any suggestions? Post the actual schema definitions here, as I not entirely following the above. In the meantime you might to look here: http://www.postgresql.org/docs/9.4/interactive/sql-createtable.html Search on REFERENCES. In particular ON UPDATE CASCADE. Could be you already have the solution in place. Seeing the schema definitions would help us answer that. > > Thanks, > > Jon -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql