Re: Can I use check-constraint to set null?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Can I use check-constraint to set null?
Дата
Msg-id 18967.1281983564@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Can I use check-constraint to set null?  (Rikard Bosnjakovic <rikard.bosnjakovic@gmail.com>)
Список pgsql-novice
Rikard Bosnjakovic <rikard.bosnjakovic@gmail.com> writes:
> CREATE TABLE test (foo numeric(8,2) CHECK (foo >= 0.0));

> ecdb=> insert into test values (-2.2);
> ERROR:  new row for relation "test" violates check constraint "test_foo_check"

> This is expected.

> However, is there a way to insert a null when this check fails,
> instead of returning an error (and thus aborting the insert)?

You could write a BEFORE INSERT trigger that does something like

    IF NOT NEW.foo >= 0.0 THEN
        NEW.foo := NULL;
    END IF;

            regards, tom lane

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

Предыдущее
От: Leon Starr
Дата:
Сообщение: Re: How to trap invalid enum input exception?
Следующее
От: Andreas Kretschmer
Дата:
Сообщение: Re: Remote Connection to server: Connection Refused