Re: Check/unique constraint question

Поиск
Список
Период
Сортировка
От Volkan YAZICI
Тема Re: Check/unique constraint question
Дата
Msg-id 20060305100526.GA214@alamut
обсуждение исходный текст
Ответ на Re: Check/unique constraint question  ("Nikolay Samokhvalov" <samokhvalov@gmail.com>)
Список pgsql-sql
On Mar 05 12:02, Nikolay Samokhvalov wrote:
> Unfortunately, at the moment Postgres doesn't support subqueries in
> CHECK constraints

I don't know how feasible this is but, it's possible to hide subqueries
that will be used in constraints in procedures. Here's an alternative
method to Nikolay's:

CREATE TABLE where_check (active bool, id int);

CREATE OR REPLACE FUNCTION check_id (bool, int) RETURNS bool AS ' SELECT CASE   WHEN $1 THEN NOT EXISTS (SELECT 1
                    FROM where_check AS W                            WHERE W.active IS TRUE AND W.id = $2)   ELSE TRUE
END;
' LANGUAGE SQL;

-- A partial index like
-- CREATE INDEX active_id_idx ON where_check (id)
--   WHERE active IS TRUE;
-- should speed up above query

ALTER TABLE where_check ADD CONSTRAINT idchk CHECK (check_id(active, id));

test=# INSERT INTO where_check VALUES (TRUE, 2);
INSERT 0 1
test=# INSERT INTO where_check VALUES (FALSE, 2);
INSERT 0 1
test=# INSERT INTO where_check VALUES (TRUE, 2);
ERROR:  new row for relation "where_check" violates check constraint
"idchk"


Regards.


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

Предыдущее
От: "Nikolay Samokhvalov"
Дата:
Сообщение: Re: Check/unique constraint question
Следующее
От: Michael Glaesemann
Дата:
Сообщение: Re: Check/unique constraint question