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 8887491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:49:19+00:00 2026-06-14T21:49:19+00:00

I need to create a relationship between ReportID which is part of a composite

  • 0

I need to create a relationship between ReportID which is part of a composite primary key.
This is my code written for Microsoft SQL Server:

CREATE TABLE Tableone
(
    ReportID varchar UNIQUE,
    Date date,
    Line ommited

PRIMARY KEY (ReportNumber, Date)
)

CREATE TABLE Tabletwo
(
    Line omitted
    Line ommited
    ReportID varchar UNIQUE FOREIGN KEY REFERENCES Tableone(ReportID),
    Line ommited

    PRIMARY KEY (XX, XXX, ReportID)
)

UPDATE: We have specifically been asked to set both ReportID and Date as the primary key in Tableone but to just reference ReportID in Tabletwo.

Thanks for your time.

  • 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-14T21:49:20+00:00Added an answer on June 14, 2026 at 9:49 pm

    If you have a composite primary key, then all foreign key constraints that reference it must use all columns of the composite primary key.

    So in your case – TableTwo must reference TableOne(ReportID, Date)

    The only way around this would be to create a UNIQUE INDEX on TableOne (ReportID) – then you can create a foreign key reference to that unique constraint alone.

    But that then begs the question: why isn’t ReportID alone the primary key, if it’s already unique (since you can put a UNIQUE INDEX on it) and NOT NULL (since it’s part of the compound PK) ?

    Step 1: create the UNIQUE INDEX:

    CREATE UNIQUE NONCLUSTERED INDEX UIX_TableOne ON dbo.TableOne(ReportID);
    

    Step 2: create the foreign key from TableTwo:

    ALTER TABLE dbo.TableTwo
    ADD CONSTRAINT FK_TableTwo_TableOne
    FOREIGN KEY(ReportID) REFERENCES dbo.TableOne(ReportID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two objects I need to create a relationship between in Core Data.
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
In SugarCRM 6.0.0 I need to create a relationship between an Account and a
I have a one-to-many relationship between two tables: table1: NUMBER users_id (primary key) field2
I use MS SQL 2008 R2, I need create a Table with a CHECK
Need to create a custom DNS name server using C which will check against
I have two tables that I'm trying to create a relationship between so I
I have an existing DB with a very simple one-way foreign key relationship between
I want do a one-to-many relationship between two tables using a join table. This
I have a many-to-many relationship between dimension D and measure M. I need to

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.