Обсуждение: Adding Records With SERIAL Primary Key

Поиск
Список
Период
Сортировка

Adding Records With SERIAL Primary Key

От
Rich Shepard
Дата:
   I may have missed this in the docs; if so, please tell me where to find the
answer there. If not, I still need to learn how to resolve this situation.

   The database schema has been designed and the tables are ready to be
created. Once they exist, I want to load data into the tables in batch mode
(rather than one at a time, manually). The core table has a SERIAL data type
field as the primary key. How is this field assigned values? Then, how do I
load the related tables so they reference the proper records?

Thanks,

Rich

--
Dr. Richard B. Shepard, President
Applied Ecosystem Services, Inc. (TM)
<http://www.appl-ecosys.com>   Voice: 503-667-4517   Fax: 503-667-8863

Re: Adding Records With SERIAL Primary Key

От
Scott Marlowe
Дата:
On Tue, 2005-05-03 at 11:58, Rich Shepard wrote:
>    I may have missed this in the docs; if so, please tell me where to find the
> answer there. If not, I still need to learn how to resolve this situation.
>
>    The database schema has been designed and the tables are ready to be
> created. Once they exist, I want to load data into the tables in batch mode
> (rather than one at a time, manually). The core table has a SERIAL data type
> field as the primary key. How is this field assigned values? Then, how do I
> load the related tables so they reference the proper records?

The normal way it's added is with a DEFAULT clause.  This means it can
be overridden by inserting it by hand. Changing this to an after trigger
can ensure that it is always inserted no matter what the user app tries
to do.

When loading in data using the copy command, one can set the columns to
be copied, and the system will insert the serial value for you if you
leave that column out of the list.

If your data already has a value assigned, you can load those in, and
then use setval() to set the value of the associated sequence.

Re: Adding Records With SERIAL Primary Key

От
Rich Shepard
Дата:
On Tue, 3 May 2005, Scott Marlowe wrote:

> When loading in data using the copy command, one can set the columns to be
> copied, and the system will insert the serial value for you if you leave
> that column out of the list.

Scott,

   Aha! I thought this would be the case, but I was not sure.

Many thanks,

Rich

--
Dr. Richard B. Shepard, President
Applied Ecosystem Services, Inc. (TM)
<http://www.appl-ecosys.com>   Voice: 503-667-4517   Fax: 503-667-8863