SQL Add foreign key to existing column
Asked Answered
E

7

127

If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint:

ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)

UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error:

Foreign key 'UserID' references invalid column 'UserID' in referencing table 'Employees'.

Excurved answered 30/4, 2012 at 19:34 Comment(2)
Could you provide your two table's schema?Thenna
refer this link #35197451Luwian
T
220

Error indicates that there is no UserID column in your Employees table. Try adding the column first and then re-run the statement.

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);
Tanning answered 30/4, 2012 at 19:37 Comment(7)
This was correct. Our DB was not updating our add column. This has been resolved but not that our column is established I still can not add the constraint. There are no primary or candidate keys in the referenced table 'ActiveDirectories' that match the referencing column list in the foreign key 'FK__Employees__UserI__04E4BC85'.Excurved
Looks like whichever column is referenced by FK__Employees__UserI__04E4BC85 is not defined as PRIMARY KEY or candidate key in ActiveDirectories table.Tanning
yea but it is definitely our PK in the ActiveDirectories tableExcurved
RESOLVED: There is a reason why you create ERDs and establish relationships prior to building. We had too many records in one table which caused an error trying to create the relationships to the other table. Thanks all.Excurved
refer this link #35197451Luwian
Thanks this worked, For future readers a Note: Please be sure that values of foreign keys of both the table match, otherwise you get this warning.Ketty
You can add ON DELETE CASCADE ON UPDATE CASCADEDelossantos
C
25

Maybe you got your columns backwards??

ALTER TABLE Employees
ADD FOREIGN KEY (UserID)           <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id)   <-- this needs to be a column of the ActiveDirectories table

Could it be that the column is called ID in the Employees table, and UserID in the ActiveDirectories table?

Then your command should be:

ALTER TABLE Employees
ADD FOREIGN KEY (ID)                   <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID)   <-- column in table "ActiveDirectories" 
Clomb answered 30/4, 2012 at 19:39 Comment(1)
I know its weird but unfortunately the name in the ActiveDirectory table idExcurved
D
5

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
Dit answered 23/6, 2015 at 13:11 Comment(0)
D
3

In the future.

ALTER TABLE Employees
ADD UserID int;

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);
Drunkometer answered 24/5, 2018 at 0:50 Comment(0)
W
1
ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id

ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyStudent 
FOREIGN KEY FacId
REFERENCES Student StuId
Washery answered 14/9, 2017 at 5:57 Comment(1)
You should provide some explanation with your answerFroissart
A
0

way of foreign key creation correct for ActiveDirectories(id), i think the main mistake is you didn't mentioned primary key for id in ActiveDirectories table

Antagonize answered 1/12, 2016 at 10:59 Comment(0)
I
0

If the table has already been created:

First do:

ALTER TABLE `table1_name` ADD UNIQUE( `column_name`);

Then:

ALTER TABLE `table1_name` ADD FOREIGN KEY (`column_name`) REFERENCES `table2_name`(`column_name`);
Ingrid answered 20/11, 2020 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.