Re: table constraints

Поиск
Список
Период
Сортировка
От Bruno Wolff III
Тема Re: table constraints
Дата
Msg-id 20050228202016.GA32034@wolff.to
обсуждение исходный текст
Ответ на table constraints  ("Casey T. Deccio" <ctdecci@sandia.gov>)
Ответы Re: table constraints  ("Casey T. Deccio" <ctdecci@sandia.gov>)
Список pgsql-sql
On Mon, Feb 28, 2005 at 11:28:30 -0800, "Casey T. Deccio" <ctdecci@sandia.gov> wrote:
> 
> In this case each bldg has an owner associated to it, and each animal
> lives in some bldg.  Each owner has exactly one own favorite animal out
> of all the bldgs owned by him.  So the constraint added to each zoo row
> is that the boolean field 'favorite' is true for exactly once for each
> group of animals in the zoo that have a common owner.

One way to do this kind of thing is to add a UNIQUE key to the table
that holds the animal - table relation of the animal and table and add
a UNIQUE key for the building table of the building and owner.
Then in the owner table add favorite animal building and favorite
animal columns with the property NOT NULL. Then add two deferable
foreign keys (owner, favorite animal building) to the building table
and (favorite animal building, favorite animal) to the animal - building
table. When modifying data you want to change favorites before changing
the underlying tables (where a building or animal change affects someones
favorites).

P.S. in your example you used varchar(50) for the text strings. Using 'text'
is better unless there is a business rule limiting the name lengths to 50
or you expect to port the application to another rdbms.


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

Предыдущее
От: "Joel Fradkin"
Дата:
Сообщение: Re: diference in dates in minutes
Следующее
От: "Casey T. Deccio"
Дата:
Сообщение: Re: table constraints