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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:23:49+00:00 2026-06-04T07:23:49+00:00

I have two tables – 1st is place and 2nd is location. Place can

  • 0

I have two tables – 1st is place and 2nd is location. Place can have zero or many locations. Place PK is id, and location PK is two component key place_id + id.

enter image description here

In third table (ex. invoice) I could have place which doesn’t have any location.

The question is: how to set FK in invoice table?

  1. I can’t use only place.id as FK because I want to know location.
  2. If I use locations PK which consists of place_id + id columns – then the relationship to table location is not consistent – some place can have 0 location.

So, the main problem is that some place can have 0 locations.

Yes, I might to set surrogate PK in location table, and set place_id FK, location_id FK NULL. But, the requirement is that place and location keys are natural (based on company organizational structure). Second idea is to have one to one/many association and put in location id value 0 for place which have zero locations and hide this location in user interface. Actually, then I need to put for every place location 0 (no location).

Thanks.

  • 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-04T07:23:50+00:00Added an answer on June 4, 2026 at 7:23 am

    You should be able to simply “merge” these two FKs together:

    enter image description here

    CREATE TABLE invoice (
        id INT PRIMARY KEY,
        place_id INT NOT NULL,
        location_id INT NULL,
        FOREIGN KEY (place_id) REFERENCES place (id),
        FOREIGN KEY (place_id, location_id) REFERENCES location (place_id, id)
    );
    

    The DBMS will ignore a FK with at least one NULL:

    • The place_id is NOT NULL so the first FK will always be in force.
    • If location_id is NULL, the second FK will simply be ignored.

    You can play with it in this SQL Fiddle.

    (In fact, the very existence of natural key is what enables you to do this – if location had a surrogate key, you could not guarantee that the two FKs point to the same place.)


    In an unlikely case your DBMS treats NULLs in a funny way so a FK with a NULL is not ignored, you can completely separate these two FKs, and use CHECK to ensure they belong to the same location:

    CREATE TABLE invoice (
        id INT PRIMARY KEY,
        place_id INT NOT NULL,
        location_place_id INT NULL,
        location_id INT NULL,
        FOREIGN KEY (place_id) REFERENCES place (id),
        FOREIGN KEY (location_place_id, location_id) REFERENCES location (place_id, id),
        CHECK (place_id = location_place_id)
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables with a foreign key constraint how can I delete rows
I have two tables connected with one to many relationship. Parent Table is a
I have two tables. Table1 contains companies whose locations are georeferenced with lat/lng coordinates
I have two tables like so: create table doi ( id number primary key
I have two tables which have a column in common, how can I retrieve
I have two tables: product and sale . How can I write a SQL
I have two tables TableA , TableB with many to one relationship defined in
I have two tables USERS and CALENDER, in calender table user place his availability
I have two tables in one-to-many relation: product -> orders In the Products table
I have two tables Like Below CREATE TABLE projectlist(ProjectId INT NOT NULL PRIMARY KEY,

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.