Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreign Key problem

Status
Not open for further replies.

cpitc

IS-IT--Management
Dec 20, 2009
77
GB
I am running a sql script to try and create a FK in a table.
I am using this script

ALTER TABLE employee
ADD FOREIGN KEY (JobNum)
REFERENCES Emp_Jobs (JobNum)

but no matter what I do it keeps giving me this error message,

Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__employee__JobNum__023D5A04".
The conflict occurred in database "Colins Main", table "dbo.Emp_Jobs", column 'JobNum'.

Does anyone know what this is and how I can solve it. Thanks
 
It is telling you that there is an JobNum in your Employee table that DOES NOT exist in your Emp_Jobs table.
 
This will help you find those jobnum values
Code:
select e.*
from Employee e
   Left Join emp_jobs ej ON e.jobnum = ej.jobnum
where ej.jobnum is null
 
Ok thanks thats perfect. Found the problem and sorted thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top