Re: Multi-Column Constraints and Null Values

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Multi-Column Constraints and Null Values
Дата
Msg-id 9554.1146348161@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: Multi-Column Constraints and Null Values  (Bruno Wolff III <bruno@wolff.to>)
Список pgsql-sql
Bruno Wolff III <bruno@wolff.to> writes:
> To block multiple entries where one of the columns is null he needs to
> add extra checks. One way to do this is to add a partial index for each column
> with the condition that the other column IS NULL. This will cover everything
> but the case where both columns are null. If this case isn't allowed, then
> a row constraint can be used to block it. If it is allowed, the only thing that
> comes to mind is making an SQL function that counts the number of rows where
> both columns are null and calling that function in a row constraint and
> checking that the result is <= 1.

Nah, it's easy:
CREATE UNIQUE INDEX fooi ON foo ((0))  WHERE col1 IS NULL AND col2 IS NULL;

So you'd need a total of four unique indexes (3 of 'em partial) to
enforce the condition for two columns.  The real problem with this
is that it doesn't scale well for more than two columns :-(
        regards, tom lane


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

Предыдущее
От: Bruno Wolff III
Дата:
Сообщение: Re: Multi-Column Constraints and Null Values
Следующее
От: "Tornroth, Phill"
Дата:
Сообщение: Re: Multi-Column Constraints and Null Values