Re: Question about Foreign key!

From: Patrick Welche <prlw1(at)newn(dot)cam(dot)ac(dot)uk>
To: Fariba Noorbakhsh <fNoorbakhsh(at)tecways(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Question about Foreign key!
Date: 2001-07-27 15:51:52
Message-ID: 20010727165152.A19967@quartz.newn.cam.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jul 27, 2001 at 04:33:24PM +0200, Fariba Noorbakhsh wrote:
> Hi everybody,
>
> Is there anyway to define foreign Key ,.. (constraints) after creating
> table?
> As far I know, you can't do it with ALTER table!!!

template1=# \h alter table
Command: ALTER TABLE
...
ALTER TABLE table
ADD table constraint definition
...

eg:
Table "tblPerson"
Attribute | Type | Modifier
---------------------------+--------------------------+----------
ID | integer |
...
Table "tblAddress"
Attribute | Type | Modifier
------------+--------------------------+----------
ID | integer |
PersonID | integer |
...

create unique index tblperson_pkey on "tblPerson"("ID");
alter table "tblAddress"
add constraint addfk
foreign key("PersonID") references "tblPerson"("ID") match full;

Hope this helps,

Patrick

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ryan C. Bonham 2001-07-27 16:59:36 Visual Basic and PostgreSQL ODBC
Previous Message Lee Harr 2001-07-27 14:33:37 Re: Better backtrace (wasRe: pqReadData() -- backend closed the channel unexpectedly.)