Re: [GENERAL] COPY with default values won't work?

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Charles Tassell <ctassell(at)isn(dot)net>, pgsql-general(at)postgreSQL(dot)org
Subject: Re: [GENERAL] COPY with default values won't work?
Date: 1999-03-24 13:14:39
Message-ID: l03110708b31e8e95c16d@[147.233.150.135]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 07:15 +0200 on 24/03/1999, Charles Tassell wrote:

>
> I'm trying to copy data into the following table:
>
> CREATE SEQUENCE seq_account_type_ndx;
>
> CREATE TABLE accounts (
> Account_Type_NDX int4 not null default
> nextval('seq_account_type_ndx'),
> Account_Name Text
> );
>
> Using this as a datafile:
> \N|Box
> \N|NetSurfer120
> \N|eMailer
> \N|eMailerLite
>
> I've tried writing the code in C using libpq, using the copy command as the
> postgres super user, or using \copy as my normal user. NONE will work with
> the "not null" in there, and if I remove it, it just inserts a null value
> into account_type_ndx, without using the default. I've also tried
> switching the default to a number (ie default 12) instead of the nextval of
> the sequence, with no better luck.
>
> Here is the copy command I tend to use:
> COPY accounts from stdin USING delimiters '|'
> or \COPY accounts from '/tmp/datafile.txt' USING delimiters '|'
>
> Any ideas?

I thought the above would work, too, but apparently it doesn't.

So, two possible solutions:

A) Update with the sequence after you have copied.

1) Create the table without the NOT NULL.
2) Make the copy
3) Use
UPDATE accounts
SET Account_Type_NDX = nextval( 'seq_account_type_ndx' );
4) Vacuum.

B) Copy into a separate table and insert.

1) Create the table, including the NOT NULL and everything.
2) Create a temporary table, with all the same fields, without NOT NULL.
3) Copy into the temporary table.
4) Use:
INSERT INTO accounts ( Account_Name )
SELECT Account_Name FROM temp_accounts;
5) Drop the temp_accounts table.

Variation: Create the temp_accounts table without the Account_Type_NDX
field. It's null anyway. Have your copy files without the "\N|" part.
Saves the transfer of three bytes per row and the insertion of a null
value per row. Makes things a wee bit faster.

My personal favourite is plan (B), because it allows building the table
with the "NOT NULL" constraint, and does not require you to remember the
name of the sequence. The general principle here is:

1) Look at your table and decide which fields should be inserted from
an external data source, and which from an internal data source
(these are usually the fields that have a default value).

2) Create a temporary table that contains only the fields that need to
be fed externally.

3) Copy your data into that table. The copy files need not have any
NULL value unless it truely stands for "no value here".

4) Insert into your real table using a SELECT statement. The INSERT
clause should include only the names of "external source" fields.
This will cause the internal ones to be filled from the default
source.

This method allows also the use of functions and stuff when populating the
table.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ordini 1999-03-24 13:25:06 Re: [INTERFACES] Problem using Having in a sub-query wit the Count function.
Previous Message Brett W. McCoy 1999-03-24 12:08:34 Re: [GENERAL] COPY with default values won't work?