Re: Table Merge Successful, Primary Keys Missing

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: operationsengineer1(at)yahoo(dot)com, pgsql-novice(at)postgresql(dot)org
Subject: Re: Table Merge Successful, Primary Keys Missing
Date: 2006-06-26 20:21:54
Message-ID: 20060626202154.67245.qmail@web31814.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

> > select *
> > into t_temp_final
> > from t_temp_output
> > union
> > SELECT * FROM t_product
> > ;
> the current t_product table entries have product_id
> values. the temp table doesn't. so i need to add
> product_ids wher where product_id IS NULL.
>
> if i knew how to create a serial field (and control
> its values to start at a level greater than the
> highest t_product product_id value) in an insert into
> temp table, i could create product_ids when i create
> the insert into temp table and then merge everything
> together - complete with product_ids in place.

When you define a column to use a serial, you are really just using a nifty short-cut.
What actually happens, (and you could manually do all of this by hand) is:

1st. a sequence is created (it is just a "kind" of storage are that keeps track of the last
number used. in your case, it is the last number use as a primary key.) your automated sequence
is name something like "a_id_sequence"

2nd. your create table statement would actuall be read as:
create table a ( id integer default nextval(a_id_sequence) primary key).

http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL
notice section 8.1.4

so, long story short. you can directly call the nextval() of the sequence with you insert your
temp table.

select nextval(what_ever_your_sequence_name_is), colA, colB, colC, ...
into t_temp_final
from t_temp_output
union
SELECT * FROM t_product
;

just be sure that the column name maps to the correct destination columns.

Regards,

Richard Broersma Jr.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2006-06-26 20:32:24 Re: Variable array sizes with PQexecParams
Previous Message operationsengineer1 2006-06-26 20:01:34 Re: Table Merge Successful, Primary Keys Missing