There are no primary or candidate keys in the referenced table

Hello Experts;

It's getting late and my brain is fried.  Help me with this error:

--Msg 1776, Level 16, State 0, Line 2
--There are no primary or candidate keys in the referenced table 'CMAS.JTA_Task_Competencies' that match the referencing column list in the foreign key 'FK_Competencies_iCompID'.
--Msg 1750, Level 16, State 0, Line 2
--Could not create constraint. See previous errors.

The attached file contains the Alter Statement and table structures.
BullDog.sql
wdbatesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anoo S PillaiCommented:
You should have a unique constraint on iCompID to create the foreign key.

Say for example , if you have an index like the following, The foreign key would have been created -
CREATE UNIQUE INDEX IX_JTA_Task_Competencies_iCompID  ON [CMAS].[JTA_Task_Competencies] ( iCompID )

If your business scenario does not permit a unique constraint like this, it would be difficult to enable the constraint using a plain single  column foreign key like the one that you have given.

BTW, from your naming convention, I think you are referring a wrong table in the constraint
ALTER TABLE [AMIS].[CompetencyJobBase] WITH CHECK ADD CONSTRAINT [FK_Competencies_iCompID] FOREIGN KEY([CompJobId])
REFERENCES [CMAS].[JTA_Task_Competencies] ([iCompID])
GO

Do you really mean to refer JTA_Task_Competencies ? (  or another table Competencies ? )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wdbatesAuthor Commented:
I had it backwards, thank you for pointing it out.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.