April 26, 2005 at 10:02 am
Can Anyone help me to draft a script in Query Analyzer to establish a relationship between two tables. A primary key table "center" with key clinicphone and a foreign key table "records" with foreign key clinicphone.
I'm not sure where to begin.....
thanks!
Kristin
April 26, 2005 at 10:09 am
ALTER TABLE dbo.records ADD CONSTRAINT
FK_records_center_FKNAME FOREIGN KEY
(
clinicphone
) REFERENCES dbo.center
(
clinicphone
)
April 26, 2005 at 10:19 am
This is the error I get...what do you make of it??
Server: Msg 547, Level 16, State 1, Line 1
ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_records_center_FKNAME'. The conflict occurred in database 'MROresults', table 'center', column 'clinicphone'.
Kristin
April 26, 2005 at 11:23 am
that means that the syntax is correct, try doing a left join from the child table to the parent table to find missing keys. Correct those (either by recreating the parents or by deleting the orphaned rows).
April 26, 2005 at 11:27 am
hahahah! you lost me how do I do that?
sorry!!!!!
Kristin
April 26, 2005 at 11:36 am
Select DISTINCT R.clinicphone from dbo.records R left outer join dbo.center C on R.clinicphone = C.clinicphone where C.clinicphone is null
This will give you a list of clinicphones that exists in records but not in center. You will have to either delete those records, set their values to null, or create new centers with those ids. Or you could update the current data if it's been changed in one table and not the other.
Once you get no records from this query you'll be able to create the relationship.
April 26, 2005 at 11:44 am
ok, think I got it....did this:
SELECT center.clinicname, center.clinicphone, records.recordnum
FROM records
LEFT JOIN center on
center.clinicphone = records.clinicphone
WHERE center.clinicphone is null
then it executed successfully.
thanks for your help!!!!!
kristin
Kristin
April 26, 2005 at 12:06 pm
What executed successfully?
April 27, 2005 at 9:21 am
the error you received is because child records exist which are not in the parent/referenced table;
run this query:
SELECT CLINICPHONE AS MISSINGCLINICPHONE,* FROM DBO.CENTER WHERE CLINICPHONE IS NOT NULL AND CLINICPHONE NOT IN (SELECT DISTINCT CLINICPHONE FROM DBO.RECORDS WHERE CLINICPHONE IS NOT NULL)
that will list all records in CENTER which do not have a value to reference in RECORDS
From there, you will need to decide whether to create the refernce records in RECORD, fix existing data in CENTER, or do something else altogether.
hope that helps
Lowell
April 27, 2005 at 9:39 am
right....the query I have listed above gave me all the records that were "lost" or or did not have a value that could reference my primary key table, center.
Once I did that, I was able to fix my original problem which was to establish the relationship. Then I used your alter table script
ALTER TABLE dbo.records ADD CONSTRAINT
FK_records_center_FKNAME FOREIGN KEY
(
clinicphone
) REFERENCES dbo.center
(
clinicphone
)
which "executed successfully" (that is what I was referencing above)
thanks again for all your help. I learned alot through this.
Kristin
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply