We help IT Professionals succeed at work.

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.


Comment
Watch Question

SQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015
Commented:
You need to make TMPARENT.MemberId a primary key, before you can establish a FK relationship to it in another table.

Author

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.
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
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," & _

Author

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.
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
What exactly is the error message you are getting..

Author

Commented:
RankId is not the same data type as referencing column HighestRankId

Thank you.

Author

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


Thank you.
Jim HornSQL Server Data Dude
Most Valuable Expert 2013
Author of the Year 2015

Commented:
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..

Author

Commented:
Ok. I have no idea either.

Thank you.

Scott PletcherSenior DBA
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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).
Scott PletcherSenior DBA
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
As for the mismatched data types, script the tables from SQL Server and verify that the datatypes are what you think they should be.