Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:01:43+00:00 2026-06-10T17:01:43+00:00

I’ve got table Visit with columns like VisitID , PatientID , DoctorID etc. and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:01:45+00:00Added an answer on June 10, 2026 at 5:01 pm

    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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.