EMPTY FOREIGN KEY PROBLEM

Hi All,

I have a parent table :

TMPARENT :

1. MemberId
2. MemberName

TMCHILD :

1. MemberId
2. SponserId
3. UplineMemberId

Key : MemberId
Foreign Key : SponserId, UplineMemberId of MemberId at TMPARENT.

Error at creation :

There are no primary or candidate keys in the referenced table 'TMPARENT' that match the referencing column list in the foreign key 'FK_TMCHILD_SponserId'. Could not create constraint. See previous errors.

What is the problem ?

Thank you.


LVL 1
emi_sastraAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You need to make TMPARENT.MemberId a primary key, before you can establish a FK relationship to it in another table.
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
emi_sastraAuthor Commented:
It works.

Another problem.

strSQLCommand = "CREATE TABLE " & strTableFile & "(" & _
                        "RankId VarChar(3) DEFAULT 'ID' PRIMARY KEY," & _
                        "RankName VarChar(30) DEFAULT ''," & _
                         "CrtId VarChar(10) DEFAULT ''," & _
                        "CrtDate DateTime," & _
                        "UpdId VarChar(10) DEFAULT ''," & _
                        "UpdDate DateTime)"

 strSQLCommand = "CREATE TABLE " & strTableFile & "(" & _
                        "MemberId Char(8) DEFAULT '' PRIMARY KEY ," & _
                        "HighestRankId VarChar(3) DEFAULT 'ID'," & _
                        "CrtId Char(10) DEFAULT ''," & _
                        "CrtDate DateTime," & _
                        "UpdId Char(10)DEFAULT ''," & _
                        "UpdDate DateTime)"


RankId is not the same data type as referencing column HighestRankId

What's wrong ?

Thank you.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
They both are varchar(3)'s, so no idea.  
Just out of curiosity, what's the purpose of establishing a default value of 'ID' on a primary key?

I also don't see where you're referencing HighestRankId to RankId in the above code, like ...
"HighestRankId VarChar(3) DEFAULT 'ID' REFERENCES othertablename.RankId," & _
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

emi_sastraAuthor Commented:
ALTER TABLE TMMEMBERDATA ADD CONSTRAINT FK_TMMEMBERDATA_HighestRankId  FOREIGN KEY(HighestRankId) REFERENCES TMRANKQUALIFICATION(RankId)

I have remove 'ID' from  PRIMARY KEY.

What's wrong ?

Thank you.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
What exactly is the error message you are getting..
0
emi_sastraAuthor Commented:
RankId is not the same data type as referencing column HighestRankId

Thank you.
0
emi_sastraAuthor Commented:
Column TMRANKQUALIFICATION.RankId is not the same data type as referencing column HighestRankId in foreign key FK_TMMEMBERDATA_HighestRankId


Thank you.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No clue.  You might want to drop constraint and re-create.

Also, I'm going off the grid for a couple of hours, so any expert that wants to pick up from here go ahead..
0
emi_sastraAuthor Commented:
Ok. I have no idea either.

Thank you.

0
Scott PletcherSenior DBACommented:
The foreign key being referenced doesn't necessarily need to be a primary key.  A unique index is enough.

Btw, there are two bytes of overhead for a varchar column (to store the actual length), so instead of varchar(3) you should just use char(3).  char(3) also processes slightly faster than varchar(3).
0
Scott PletcherSenior DBACommented:
As for the mismatched data types, script the tables from SQL Server and verify that the datatypes are what you think they should be.
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 2005

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.