Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z3B8Y-0003JF-7I for pgsql-sql@arkaria.postgresql.org; Thu, 11 Jun 2015 22:39:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z3B8V-0002ZW-VB for pgsql-sql@arkaria.postgresql.org; Thu, 11 Jun 2015 22:39:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z3B8T-0002ZL-Oe for pgsql-sql@postgresql.org; Thu, 11 Jun 2015 22:39:46 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z391o-0000OX-4m for pgsql-sql@postgresql.org; Thu, 11 Jun 2015 20:24:46 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 4361820949 for ; Thu, 11 Jun 2015 16:24:42 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Thu, 11 Jun 2015 16:24:42 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h=cc :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=DNpEmVEY6xxaILgWCMLbHt4YvGo=; b=W0EW71 04Ik+Yc6RBegjKt+uSOJn7lrnOKJ1t+TDPO2UIYzGLb7UxU78HeAz7yFnYHukUBk ObuakvEb8h51c2P4G6ybT3z3G6b1g+IpBqGZYP3f+4KAxkSSI4n4Jlki3cljdI6X gMj7mGlZ+StbW+XKpcTVCo8yNZgmarLc/vVO0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=DNpEmVEY6xxaILg WCMLbHt4YvGo=; b=CNv+JKUX8ebbTTLOtXm5OgH636Pa2GknXqtz9BFBqmACfrf 19V1yA7aM2r6WDQKQBvISJhcSEp2RgTZkQjj6ahWCuIQz+vH9vx7jIc7m8nu16Wt ILtetoGDn94hQRJu+Kk6uUFcidY41viqRNicHUqt3DKs+W3iOA2Q0xRxgmRA= X-Sasl-enc: gGEM5+9CLlnZbS/nKqK2YEQ1yQmMn68y2tjfYcKd3qL7 1434054281 Received: from killi.site (unknown [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id BA0CFC0001E; Thu, 11 Jun 2015 16:24:41 -0400 (EDT) Message-ID: <5579EE88.3020507@aklaver.com> Date: Thu, 11 Jun 2015 13:24:40 -0700 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Jon Forsyth CC: pgsql-sql@postgresql.org Subject: Re: Renumber Primary Keys and Update the same as Foreign Keys References: <5578CD85.2050901@aklaver.com> 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/11/2015 01:02 PM, Jon Forsyth wrote: > Thanks for the response. Here is the simplified table schema before the > new 'question' table and media tables are added: > > CREATE TABLE oral_question ( > > oral_question_id integer NOT NULL, > > audio_prompt_file_path character varying(250) NOT NULL, > > text_prompt text NOT NULL, > > ); > > CREATE TABLE essay_question ( > > essay_question_id integer NOT NULL, > > text_prompt text NOT NULL, > > ); > > CREATE TABLE oral_question_response ( > > oral_question_response_id integer NOT NULL, > > audio_response_file_path character varying(250) NOT NULL, > > oral_question_id integer NOT NULL, > > ); > > CREATE TABLE essay_question_response ( > > essay_question_response_id integer NOT NULL, > > response_text text NOT NULL, > > essay_question_id integer NOT NULL, > > ); > > > And after the 'question' table is added: > > > CREATE TABLE question ( > > question_id integer NOT NULL, > > ); > > > Then same as above except this new field is on the essay_question and > oral_question tables: > > question_id integer NOT NULL, I am not seeing the PRIMARY KEYS on the above or even a UNIQUE index, so are the duplicates within the table or between the tables? Assuming the parent table is question and the childs are essay_question and oral_question the question_id could be added to each as FK that points back to question. What I cannot see from here is how you know which essay_question and oral_question point to the same question? > > > Thanks -Jon > > > On Wed, Jun 10, 2015 at 5:51 PM, Adrian Klaver > > wrote: > > 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 > > -- 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