Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Foreign key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-11-04, 13:35
rmill9681 rmill9681 is offline
Registered User
 
Join Date: Sep 2004
Posts: 21
Foreign key

Hello,
I need to add a column to a table. The new column is a foreign key which references another table's column.

how can you do this? Does it need to be done in two steps, like:

alter table table1
add new_col_name datatype

then

alter table table1
add table constraint.

If so, what is the syntax for adding a foreign key contraint which references another table.

thank you
Reply With Quote
  #2 (permalink)  
Old 10-11-04, 13:56
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

Yes, it has to be done in two steps, just as you said.

To add a foreign key constraint :

Code:
ALTER TABLE table1 ADD CONSTRAINT fk_table1_table2 FOREIGN KEY (field1) REFERENCES table2(field2);
Where field1 is the column in table1 which is referenced by the column field2 in table2. If you have multi-column FK constraints, just put them in the right order, separated by commas :
Code:
ALTER TABLE table1 ADD CONSTRAINT fk_table1_table2 FOREIGN KEY (field11, field12) REFERENCES table2(field21, field22);


Regards,

RBARAER
Reply With Quote
  #3 (permalink)  
Old 10-11-04, 14:05
rmill9681 rmill9681 is offline
Registered User
 
Join Date: Sep 2004
Posts: 21
thanks! what does the fk_table1_table2 mean? Is it just a lable?
Reply With Quote
  #4 (permalink)  
Old 10-11-04, 14:15
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
It can be done in one step, at least it can on Oracle:
Code:
alter table table1 add (new_col_name references table2(keycol));

or (to give the constraint a specific name):

Code:
alter table table1 add (new_col_name constraint table1_table2_fk references table2(keycol));
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 10-11-04, 14:22
rmill9681 rmill9681 is offline
Registered User
 
Join Date: Sep 2004
Posts: 21
cool, I'll try that also, but the first code worked.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On