pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jason Godden <jasongodden@optushome.com.au>
To: pgsql-general@postgresql.org
Subject: Re: importing db as text files
Date: Thu, 14 Aug 2003 17:52:37 +1000
Message-ID: <200308141752.37482.jasongodden@optushome.com.au> (raw)
In-Reply-To: <20030813205234.0420bb9f.expect@ihubbell.com>
References: <20030813141420.1db05267.expect@ihubbell.com>
	<200308140734.56108.jasongodden@optushome.com.au>
	<20030813205234.0420bb9f.expect@ihubbell.com>

On Thu, 14 Aug 2003 01:52 pm, you wrote:

> On Thu, 14 Aug 2003 07:34:55 +1000
>
> Jason Godden <jasongodden@optushome.com.au> wrote:
> > Hi expect,
> >
> > Best way in my opinion is to use the copy table command.  This way Pg
> > will actually 'massage' the data (string escapes and all) for you.
>
> I guess we're of the same opinion.  I did use the copy table command.
> I believe the problem is that pg is unable to use the default value when a
> value is not present.

PG is behaving correctly IMO:

create table data (data1 int4 not null,	data2 int4,data3 int4 not null default 
10);

insert into data values (2,null,default) - OK
insert into data values (null,2,default) - Fail not null data1
insert into data values (2,null) - Fail? missing field?
insert into data (data1,data2) values (2,null) - OK data3 = default with 
explicit field nomination ^

copy from... (essentially becomes - although it does something a bit different 
behind the scenes): 

insert into data (data1,data2,data3) values (x,y,z)

if data3 is specified not null default 10 and you have a line in your import 
file which is translated thus:

2 2 \N - default null but you can nominate what that is
insert into data (data1,data2,data3) values (2,2,null);

this will fail because you are explicitly saying put null in a not null field.

So use an intermediatory table without not null constraints with copy from... 
then use a query:

insert into realtable (data1,data2,data3) select data1,
case when data2 is null then default else data2 end,
data3 from data where data1 is not null; <-because data1 has no option to be 
null or a default value etc...

Problem solved... I'd be curious as to how many ppl actually import their data 
STRAIGHT into their production tables without integrity checking.  Ofcourse 
if you massage and manage it externally such as the method Greg uses then 
you're in business too - either way I believe the pg copy syntax is correct 
and makes sense.  PG Copy CANT make a guess that you intend null or to skip 
that field so the default pops in there - null and default are two very 
different things. in fact null is oo (infinity?) different things...

Unless someone changes the insert behaviour in a later release then you will 
have to come up with a 'massaged' way (Greg/Perl, intermediatory tables and 
pl/pgsql functions).  But then think about this:

insert into data values (1,2,null)

which by the proposed new null behaviour suddenly becomes:

insert into data values (1,2,default) (say default = 10);

but in reality the user simply mucked up on data entry, didn't actually mean 
default and meant to insert 70 instead - they don't get a warning about it 
and your data integrity is screwed.  Not only that the db isn't paying strict 
attention to the intended SQL syntax and the constraint management is moved 
to the client - PG (and any other decent database - ie Oracle) is far above 
the MySQL 
cram-it-in-even-if-it-breaks-code-all-your-business-rules-in-your-client way 
of doing things!

Default should be used in an explicit sense IMHO:

insert into data (data1,data2) values (1,2) - now data3 becomes default and 
all is good

VERY different to: insert into data values (1,2,null);



view thread (32+ messages)  latest in thread

Message-ID: <200308141752.37482.jasongodden@optushome.com.au>
Permalink:  ../200308141752.37482.jasongodden@optushome.com.au/
Also on:    postgresql.org/message-id/200308141752.37482.jasongodden@optushome.com.au

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: jasongodden@optushome.com.au
  Subject: Re: importing db as text files
  In-Reply-To: <200308141752.37482.jasongodden@optushome.com.au>
  References: <20030813141420.1db05267.expect@ihubbell.com> <200308140734.56108.jasongodden@optushome.com.au> <20030813205234.0420bb9f.expect@ihubbell.com> <200308141752.37482.jasongodden@optushome.com.au>

* 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