Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMxTl-0008Oj-Jy for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 18:02:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kMxTk-00027K-76 for pgsql-general@arkaria.postgresql.org; Mon, 28 Sep 2020 18:02:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMxTi-00027C-PL for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 18:02:23 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kMxTf-0005zX-Mu for pgsql-general@lists.postgresql.org; Mon, 28 Sep 2020 18:02:21 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id D71DA5C0130; Mon, 28 Sep 2020 14:02:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 28 Sep 2020 14:02:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=0 XZvulMt+D/gx58bEYHZ1yxhE/qy0iQW5KLR9Z1H48w=; b=OMvvJBravlOpZWpZX FMB37/rdwae+DzxGTv2YIdK4O4vw4xzmCzQOvzWjvxydsM9JFow/agEP1MTvGIWB b/GnwRZuMSvJP6y/1Szzz9Lsy7Vym19+OIO96NdqKt0fupq+wFWKV1IhqaV+/x+D xQmnpCxtdhA5UlzgtLdFvlkqSy+F/thTSB126teP/9siLTuIr9TBb7E+C7t7oeOa hw2WipPyinp26fRwUB+1GSkt5XlD0oklbUEV/4EOmSIBW02OHaNzkp8+IvuYrbr2 KKKnvKfcjmoflYDC9ePCEovy+7443ZGQ+UDFU0Ost5/aO21UoYxCKJHXn0fd0veG BeABQ== DKIM-Signature: v=1; a=rsa-sha256; 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-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=0XZvulMt+D/gx58bEYHZ1yxhE/qy0iQW5KLR9Z1H4 8w=; b=kYBcdCMHCS4c3XXhovgn6vv9tytZuAmQIwMtNq0iVV2qIvgRhp/7yEge/ vc+nQcz1n9XJ+HUxJRuH5gGMMn1c1/Tam7ym1GHAMMRXjjM875pXNSpHjHMOIqUi UulC9wTmCz62SQl4fxeXnRPXifIAQa4aGXd2XSprVO7aFnfZYu0h3QlB1/gIXRJd J7dMH77UmVuu3Hkb2uofJ/D1MlJP7aFfdtFn70bbTeb3/tCPs44rYeepvrN2zI74 U4iL7wqSzTI6Syn+rBO6+c7j5HsrgpQudAmnLGa+5yZLuRWZ8KNBQ3rNAng2IgXI 1j69lY2s6e630HsXPlMCJ2xZDabww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvdeigdelkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedvgffh ueejjefhkeejleegtdegnecukfhppeejhedrudejvddrhedrvdefudenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 0C6563280060; Mon, 28 Sep 2020 14:02:16 -0400 (EDT) Subject: Re: Modifying database schema without losing data To: Rich Shepard , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <945982c5-197f-d3b3-5c15-010cff998bdc@aklaver.com> Date: Mon, 28 Sep 2020 11:02:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/28/20 10:15 AM, Rich Shepard wrote: > I've been developing a business tracking application for my own use and > it's > worked well up to now. But, I need to modify it by adding a table with > attributes from two other tables. I've not drawn a E-R diagram so I show > the > two existing tables here: > > CREATE TABLE Organizations ( >   org_id serial PRIMARY KEY, >   org_name varchar(64) DEFAULT '??' NOT NULL, >   org_addr1 varchar(64), >   org_addr2 varchar(64), >   org_city varchar(16), >   state_code char(2), >   org_postcode varchar(10), >   org_country char(2) DEFAULT 'US' NOT NULL, >   main_phone varchar(16), >   org_fax varchar(12), >   org_url varchar(64), >   industry varchar(24) DEFAULT 'Other' NOT NULL >            REFERENCES industries(ind_name) >            ON UPDATE CASCADE >            ON DELETE RESTRICT, >   status varchar(20) DEFAULT 'Opportunity' NOT NULL >          REFERENCES statusTypes(stat_name) >          ON UPDATE CASCADE >          ON DELETE RESTRICT, >   comment text ); > > CREATE TABLE People ( >   person_id serial PRIMARY KEY, >   lname varchar(15) NOT NULL, >   fname varchar(15) NOT NULL, >   job_title varchar(32), >   org_id int DEFAULT '0' NOT NULL >           REFERENCES Organizations(org_id) >           ON UPDATE CASCADE >           ON DELETE RESTRICT, >   site_name varchar(64), >   site_addr varchar(32), >   site_city varchar(16), >   state_code char(2), >   site_postcode varchar(10), >   site_country char(2) DEFAULT 'US' NOT NULL, >   direct_phone varchar(15), >   direct_fax varchar(15), >   cell_phone varchar(15), >   site_phone varchar(15), >   ext varchar(6), >   email varchar(64), >   active boolean DEFAULT TRUE NOT NULL, >   comment text > ); > > What I should have noticed when I designed this tool is that addresses and > phone/e-mail addresses can be duplicated when there's only a single > location. Now I have some prospective clients with multiple locations but I > have no names of individuals. So, I want to add a Location table with > addresses and contact information. Each row in that table will have a > serial PK > and will use a FK to reference the Organization table. People will now > reference the Locations table rather than the Organization table. > > There are data in each of these tables and my research in my books and on > the web have not provided any insights on how to modify the existing schema > and get date into their new appropriate table homes. > > I think the long way is to dump the database and manually move rows (using > emacs) from their current table to the new one, as appropriate, but > there're > probably much better ways to do this and I'm eager to learn. You could use INSERT INTO location(new_fields,) SELECT the_fields FROM the_table(s). > > Regards, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com