Adding a Foreign Key

Started by Nonameover 24 years ago4 messagesgeneral
Jump to latest
#1Noname
tankgirl@worldonline.es

Hello everyone,

I know that a Primary Key can be added to a table before it has been created, doing this...

CREATE UNIQUE INDEX table_pkey ON table (atribute);

Well I wonder if a Foreign Key can also be added and what syntax does it follow.

Thankyou in advance.

Stay Safe & Happy.

:* TankGirl

#2Joe Conway
mail@joeconway.com
In reply to: Noname (#1)
Re: Adding a Foreign Key

Hello everyone,

I know that a Primary Key can be added to a table before it has been

created, doing this...

CREATE UNIQUE INDEX table_pkey ON table (atribute);

Well I wonder if a Foreign Key can also be added and what syntax does it

follow.

Thankyou in advance.

Stay Safe & Happy.

:* TankGirl

See:

http://www.postgresql.org/idocs/index.php?sql-altertable.html

The part you're interested in says:

To add a foreign key constraint to a table:
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address)
REFERENCES addresses(address) MATCH FULL

Hope this helps,

-- Joe

#3Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Noname (#1)
Re: Adding a Foreign Key

On Mon, 3 Sep 2001 tankgirl@worldonline.es wrote:

Hello everyone,

I know that a Primary Key can be added to a table before it has been created, doing this...

CREATE UNIQUE INDEX table_pkey ON table (atribute);

Well I wonder if a Foreign Key can also be added and what syntax does it follow.

alter table <table> add <table constraint definition>

ie something like:
alter table foo add constraint foo_fk foreign key(col)
references bar(barcol) on update cascade deferrable
initially deferred;

#4Peter Eisentraut
peter_e@gmx.net
In reply to: Noname (#1)
Re: Adding a Foreign Key

tankgirl@worldonline.es writes:

Well I wonder if a Foreign Key can also be added and what syntax does it follow.

ALTER TABLE foo ADD CONSTRAINT bar FOREIGN KEY (col) REFERENCES table(col) etc.

--
Peter Eisentraut peter_e@gmx.net http://funkturm.homeip.net/~peter