Re: referential integrity on existing table

Поиск
Список
Период
Сортировка
От Paul Laub
Тема Re: referential integrity on existing table
Дата
Msg-id 006b01c181dc$2c49b220$ec02520a@incyte.com
обсуждение исходный текст
Ответ на referential integrity on existing table  ("Adam Fisher" <black@cia.com.au>)
Список pgsql-general
> Hi Everyone
>
> My question is, is it possible to create a referential integrity constraint
> on an exisiting, already populated, table??
> My reasons for this are:

How about

    ALTER TABLE tablename ADD CONSTRAINT constraint_name FOREIGN KEY
        (childfield) REFERENCES parenttable (parentfield);

One downside: If anything goes wrong (some record violates referential
integrity), the command will fail without stating what record caused
the failure. So you might try something like the following first

    SELECT * FROM childtable WHERE NOT EXISTS
        (SELECT * FROM parenttable WHERE
            childtable.keyfield = parenttable.keyfield);

to locate potentially offending records.

Paul

>
> Adam Fisher




Paul B. Laub      http://astatine.incyte.com/laub      (650) 845-5411 (voice)
Incyte Genomics, Inc.   3160 Porter Dr.  Palo Alto, CA 94304 plaub@incyte.com
                       *** Incite genomics! ***


Вложения

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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: idle in transaction
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Vacuum errors and warnings