Re: importing db as text files - Mailing list pgsql-general
From | Jason Godden |
---|---|
Subject | Re: importing db as text files |
Date | |
Msg-id | 200308141752.37482.jasongodden@optushome.com.au Whole thread Raw |
In response to | Re: importing db as text files (expect <expect@ihubbell.com>) |
List | pgsql-general |
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);
pgsql-general by date: