简体   繁体   中英

Join two tables with null value

I've got table Visit with columns like VisitID , PatientID , DoctorID etc. and also column PrescriptionID . In table Prescription there are columns PrescriptionID and DrugID (together they are my primary key). One prescription can have many drugs, so there will be something like:

PrescriptionID: 1 DrugID: 38
PrescriptionID: 1 DrugID: 278
PrescriptionID: 1 DrugID: 7

In Visit table there will be inserted '1' value as PrescriptionID . But now I can't join those two tables with foreign key, because not every visit have prescription, so PrescriptionID can be null and I've got error that column must be primary key or must be unique. How can I join those tables in another way?

You seem to be talking about two distinct issues here.

One issue is, you want to allow nulls in a referencing column. That is easy, you just need to define the column as nullable and as a foreign key. What that key should reference is another question, which leads us to the other of the two issues I can see in your question.

The error about the column having to be primary key or unique has to do with the fact that the referencing table uses a single column to reference a compound (consisting of two columns) key. And you can't reference only part of the key, because a reference must be to a specific single row and your Visit.PrescriptionID value is likely to reference more than one (and rightly so, because the rows in the referenced table aren't really prescriptions, but prescription items). That is why you are told that the column must be primary key or unique.

Therefore, I suggest changing your schema as follows:

  1. Let your Prescription table contain only prescriptions as entities. Even if there are no other attribute than a key, let it be stored in its own table:

     CREATE TABLE Presciption ( PrescriptionID int IDENTITY /* just an assumption */ CONSTRAINT PK_Prescription PRIMARY KEY ); 
  2. Your present Prescription table should be renamed to something like PrescriptionItem or PrescriptionDrug , following your singular noun naming convention. Its PrescriptionID column would be a foreign key referencing Prescription.PrescriptionID , something like this:

     CREATE TABLE PresciptionDrug ( PrescriptionID int NOT NULL CONSTRAINT FK_PrescriptionDrug_Prescription FOREIGN KEY REFERENCING Prescription (PrescriptionID), DrugID int NOT NULL CONSTRAINT FK_PrescriptionDrug_Drug FOREIGN KEY REFERENCING Drug (DrugID), CONSTRAINT PK_PrescriptionDrug PRIMARY KEY (PrescriptionID, DrugID) ); 
  3. Now you can define the foreign on Visit.PrescriptionID like this:

     ALTER TABLE Visit ADD CONSTRAINT FK_Visit_Prescription FOREIGN KEY REFERENCING Prescription (PrescriptionID) ; 

    Don't forget to make sure the column is nullable if you want to make prescriptions optional for visits. (Nullable foreign keys are perfectly fine in SQL Server.)

I think the table structure should be

Visit (VisitID, Time, .....) --no prescriptionID
Prescription (PrescriptionID, ..... , VisitID) --VisitID as FK
PrescriptionDrugs(PrescriptionID, DrugID) -- Both columns as PK

Then your query would be

SELECT v.VisitID FROM Visit v
LEFT JOIN Prescription p ON v.VisitID = p.VisitID
LEFT JOIN PrescriptionDrugs pd ON p.PrescriptionID = pd.PrescriptionID

That would give you something like

VisitID        PrescriptionID      DrugID
101            ABC                 Anti-Bio
101            ABC                 Asprin
102            BAC                 Anti-Bio

Meaning 2 drugs for Visit 101 and 1 drug for Visit 102

Anyway, with your current schema, try this

select v.visitid, v.presid, p.drugid 
from visit v 
left join prescription p on v.presid = p.presid

Are you asking how to do:

select *
  from Visit as V left outer join
    Prescription as P on P.PrescriptionId = V.PrescriptionId

This will return all visits and any applicable prescriptions. Visits without prescriptions will result in output rows that have no prescription data.

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