Link to home
Start Free TrialLog in
Avatar of al4629740
al4629740Flag for United States of America

asked on

is it possible to setup a foreign key while data is in the table

I have a table where data already resides.  I am trying to setup a foreign key but got the following error message

'tblOrgRegistrations' table saved successfully
'tblOrgHours' table
- Unable to create relationship 'FK_tblOrgHours_tblOrgRegistrations'.  
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tblOrgHours_tblOrgRegistrations". The conflict occurred in database "CAPRegistration", table "dbo.tblOrgRegistrations", column 'RegID'.

Is it possible to setup a foreign key while data is already present for RegID?
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of al4629740

ASKER

Is there a way to resolve any conflicts/issues if not?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is there a way to still establish the foreign key even though EVERY record in the tblOrgRegistrations does not have a counterpart in the tblOrgHours?  Some of them simply don't exist yet.
If that does not work, can I temporarily create compliant data in the Hour table and then delete those records in the Hour table?
If the table has data, the data MUST conform to the constraint.
You could create "temporary data", but that kind of defeats the purpose of the constraint.

Better would be to create the data in the ancillary table before storing that primary data.
The problem is that all the students in the registration table do not have hours in the hour table yet.

What do you suggest?
Will each entry in the hours table be unique so that the primary key of each row is one of the columns in the students table?
Actually, your business rules seem to mandate that the value CAN be null as the student record is created (probably prior to registration) and the hours are populated after the child's attendance is known.

A constraint here that the column must be populated requires that the student's attendance be known before he is registered and that obviously won't work.
However if I want to delete the registration later...I don't want to be able to if they have hours
Ah.  Then you have the constraint reversed.  :)

You can't create attendance data unless there is a registration, and you can't delete a registration if there is recorded attendance.
So am I to make a foreign key in the Registration table?

The relationship is one registered record in the registration table to many records in the hour table.
Assuming that the hour table has a design where each row contains the primary key of the student, then that would be the correct relationship.  No time can be recorded unless a registration exists with the correct primary key and no Registration row can be deleted if the hours table contains a reference to it.
So hence, my original setup was correct.

 I was trying to setup a foreign key in the Hour table.  But it would not let me....
What statement are you trying to run and what error are you getting?
I'm trying to alter the database hour table and add a foreign key.  I am using the graphical interface and get the error as stated in my question:

'tblOrgRegistrations' table saved successfully
'tblOrgHours' table
- Unable to create relationship 'FK_tblOrgHours_tblOrgRegistrations'.  
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tblOrgHours_tblOrgRegistrations". The conflict occurred in database "CAPRegistration", table "dbo.tblOrgRegistrations", column 'RegID'.

Should I make the tblOrgHours table nullable?
Nullable won't necessarily help.

It looks like there are Hours data for a Registration that doesn't exists.
How can I locate it.
I think

SELECT r.*,h.*
FROM dbo.tblOrgHours h
             left JOIN tblOrgRegistrations r ON r.RegID = h.RegID  -- Change Foreign Key as appropriate
WHERE r.RegID IS NULL  

actually works
Yep.  That should show you the offending rows.
Please check my follow up question I am posting right now