pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Emre Hasegeli <emre.hasegeli@tart.com.tr>
To: Andreas <maps.on@gmx.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: Question on imports with foreign keys
Date: Thu, 08 Dec 2011 10:39:49 +0200
Message-ID: <op.v55w0nozk2xoe5@tart-hasegeli-development> (raw)
In-Reply-To: <4EE05DD3.2080308@gmx.net>
References: <4EE05DD3.2080308@gmx.net>

On Thu, 08 Dec 2011 08:48:51 +0200, Andreas <maps.on@gmx.net> wrote:

> Hi,
>
> suppose you need to import a csv with standard ciolums like name,  
> adress, phone, ... and some additional text columns that need to be  
> split off into referenced tables.
>
> Those lookup-tables will only be needed for a project with limited life  
> time so I create a schema that might be called "project_x". There I  
> create the necessary lookup tables.
>
> The core of the import will be added to the customers table with  
> unlimited livespan. The customers table has a PKey id which is a serial.
>
> I don't want to add FKey columns into customers for the new  
> lookup-tables so I create another table in project_x "projectinfos" that  
> stores those FKeys and another FKey that references customers.id.
>
> First question: Is this a stupid aproach?

It is logical to reduce dependency from the temporary schema to the  
customer table which is on the permanent schema.

>
> If not:
> How is the easiest way to to find the customer.id of the new customers  
> so I can insert the projectinfos?
>

It is easy to select rows not related with another table. One of the  
following queries can be used.

> Select * from "customers" where id not in (select "customerId" from  
> "projectinfos")

> Select * from "customers" left join "projectinfos" on "customers"."id" =  
> "projectinfos"."customerId" where "projectinfos"."customerid" is null

> Select * from "customers" where not exists (select true from  
> "projectinfos" where "customers".id = "customerId")



view thread (5+ messages)  latest in thread

Message-ID: <op.v55w0nozk2xoe5@tart-hasegeli-development>
Permalink:  ../op.v55w0nozk2xoe5@tart-hasegeli-development/
Also on:    postgresql.org/message-id/op.v55w0nozk2xoe5@tart-hasegeli-development

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: emre.hasegeli@tart.com.tr, maps.on@gmx.net
  Subject: Re: Question on imports with foreign keys
  In-Reply-To: <op.v55w0nozk2xoe5@tart-hasegeli-development>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox