Conor8630
Conor8630

Reputation: 345

Adding a foreign key, getting errors

I am trying to add a foreign key but I am getting below error

Incorrect syntax near 'EmployeeID'

I am trying to create a foreign key relationship between Employee table and HolidayRequestForm table.

I have a column in both tables called EmployeeID. EmployeeID is the primary key in the employee table.

ALTER TABLE [dbo].[HolidayRequestForm]
ADD CONSTRAINT FK_EHRF
FOREIGN KEY [EmployeeID] REFERENCES [dbo].[Employee](EmployeeID);

Upvotes: 0

Views: 94

Answers (2)

iminiki
iminiki

Reputation: 2573

You should write:

ALTER TABLE [dbo].[HolidayRequestForm]
ADD CONSTRAINT FK_EHRF
FOREIGN KEY ([EmployeeID]) REFERENCES [dbo].[Employee](EmployeeID);

Upvotes: 5

Daniel Brughera
Daniel Brughera

Reputation: 1651

Foreign key columns must go into parenthesis

ALTER TABLE [dbo].[HolidayRequestForm]
ADD CONSTRAINT FK_EHRF
FOREIGN KEY ([EmployeeID]) REFERENCES [dbo].[Employee](EmployeeID);

Upvotes: 4

Related Questions