简体   繁体   中英

Is there a way to define the relationship between 2 tables using 2 non-unique columns columns using SQL

During a practical interview exercise on databases I was given 2 files in CSV one for drug dispensation showing unique instances of patient (the patient_id column is unique) accessing medicine with the following items

drug_dispensation (facility_code,patient_id,weight,date,DOB)

And another CSV file facility details showing unique instances of facilities where each patient accessed medicine

Facilities_details(facility_name, facility_code, district)

The idea was to design a database with 2 tables and establish a relationship between the two. Looking at the structure of the 2 files, the common columns were the facility_code columns. However, entries in facilities_code for facility_details were not all unique in that 2 health centres shared the same Code "MON".

Since we had to import the data after creating the relationship, there was an error on foreign key constraint saying the two tables could not be related because there was no unique referencing column in facility_details.

ALTER TABLE drug_dispensation
ADD CONSTRAINT FOREIGN KEY (facility_code) REFERENCES facility_details(facility_code)

error:There are no primary or candidate keys in the referenced table facility_details that match the referencing column list

Is there a way a relationship can be established between the two tables?

The structure and set-up sounds mostly reaonsable, so presumably part of what you're being assessed against is how you deal with invalid data.

You've done all the right things in identifying the duplicate causing your failure, now you need to make a call as to how to treat it.

For example delete one of them, or amend the facility_code if it's obvious what it should be - and ensure whatever changes you make are documented.

You could either remove the constraint before loading the data, then amend it, then add constraint... or you could load to a staging table as it is, amend data there, then load to destination table.

I would comment that you said the patient_id column is unique in drug_dispensation, but it looks set up to allow multiple records per patient (eg on a different date and/ or at a different facility). I think it would make most sense to have three tables - Patient_details (patient_id, DOB... etc), Facility_details (facility_name, facility_code, district) and drug_dispensation (facility_code, patient_id, date). Weight could either go in patient_details or drug_dispensation depending on whether it's taken at the time of dispensation or is a fixed value for that patient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM