Using a serial primary key as a foreign key in a second table

Поиск
Список
Период
Сортировка
От Nathaniel
Тема Using a serial primary key as a foreign key in a second table
Дата
Msg-id 866C2A2F-2992-4DEF-A0A2-CAC74CFEB6CD@yahoo.co.uk
обсуждение исходный текст
Ответы Re: Using a serial primary key as a foreign key in a second table  (Sean Davis <sdavis2@mail.nih.gov>)
Re: Using a serial primary key as a foreign key in a second table  (Bruno Wolff III <bruno@wolff.to>)
Список pgsql-novice
Hello folks, I'm an SQL novice indeed, but working on the premise
that "No question is too simple for this list" here goes:

This is just a dummy example to make the simple problem I'm trying to
solve as clear as possible. Let's say I have 2 tables: people and
companies.

Company records have a primary key "company_id" that is a serial, and
some other fields such as the company's name.

A person record is simply some text fields (like name) and a foreign
key, "company_id" which ties the person to a company. There is a
foreign key constraint on people that ensures that every person is
tied to a company which exists in the database.

I want to add a new person, "Bugs Bunny", but I know that the company
he work for, "Looney Tunes", is not in the database.

So, I want to add the company "Looney Tunes" (which is auto-assigned
a company_id value), and then extract that id value so that I can use
it in the company_id foreign key field of Bugs Bunny's person record.

What's the simple, multiple concurrent users-safe way of doing this
in postgreSQL?

That's all Folks,

Nat




___________________________________________________________
All new Yahoo! Mail "The new Interface is stunning in its simplicity and ease of use." - PC Magazine
http://uk.docs.yahoo.com/nowyoucan.html

В списке pgsql-novice по дате отправления:

Предыдущее
От: uldis
Дата:
Сообщение: update order
Следующее
От: Sean Davis
Дата:
Сообщение: Re: Using a serial primary key as a foreign key in a second table