Home » SQL & PL/SQL » SQL & PL/SQL » Foreign key Primary key Relation (Oracle SQL developer)
Foreign key Primary key Relation [message #553237] Wed, 02 May 2012 11:10 Go to next message
newbie3
Messages: 23
Registered: September 2010
Junior Member
Hello Guys,

I am trying to establish PK-FK relationship between 2 tables. Table 1 has VACC_ID as a primary key. Table 2 has VACC_ID and SCH_ID as the primary keys. I am trying to add VACC_ID from Table 2 as a FK to Table 1. This is the code I used

ALTER TABLE Table_1
ADD CONSTRAINT FK_VACC_ID
FOREIGN KEY (VACC_ID)
REFERENCES Table_2(VACC_ID
)

I also tried to add it without writing the query and editing the Table 1. By default, I am getting 2 columns(both VACC_ID) in the local column being referenced to the 2 primary keys(VACC_ID and SCH_ID) of table 2. Please let me know, how to resolve this. I also uploaded the screeshot of the error, when I run the query. Thank You.
Re: Foreign key Primary key Relation [message #553238 is a reply to message #553237] Wed, 02 May 2012 11:12 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Foreign key Primary key Relation [message #553239 is a reply to message #553238] Wed, 02 May 2012 11:19 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
I think you've got the relationship the wrong way round.
I assume table_1 is the parent and table_2 is the child.
If so you should be altering table_2 and referencing table_1.


EDIT: typo

[Updated on: Wed, 02 May 2012 11:23]

Report message to a moderator

Re: Foreign key Primary key Relation [message #553240 is a reply to message #553237] Wed, 02 May 2012 11:19 Go to previous messageGo to next message
John Watson
Messages: 8984
Registered: January 2010
Location: Global Village
Senior Member
Are you doing this the wrong way round? A foreign key constraint is defined on the child table, not the parent.
Re: Foreign key Primary key Relation [message #553246 is a reply to message #553240] Wed, 02 May 2012 13:18 Go to previous message
newbie3
Messages: 23
Registered: September 2010
Junior Member
Thank You so much Guys.
Previous Topic: conversion of dates parameter into columns
Next Topic: How to get the dates between 2 dates excluding 'Saturday' and 'Sunday'
Goto Forum:
  


Current Time: Mon Aug 18 12:57:47 CDT 2025