SQL Add foreign key to existing column

foreign-key-relationship, sql-server-2008

Solution

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);

Problem

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'.

Original source

Related problems