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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:22:34+00:00 2026-06-10T15:22:34+00:00

Let’s suppose I have a following table: UID int (11) not null, FOREIGN KEY

  • 0

Let’s suppose I have a following table:

UID int (11) not null,  FOREIGN KEY (UID) REFERENCES users(ID),
OID int (11),  FOREIGN KEY (OID) REFERENCES orders(ID),
primary key(UID,OID)

Note the primary key.

And, I wanted to allow to have one NULL in order ID (OID) for each user ID (UID).

Though after setting the primary key it automatically makes my OID field NOT NULL.

As a work around I’m thinking to store 0 instead to mark it as an unrelated field.

So, now the question, is it possible to allow a foreign key constraint to accept also zeros (0) apart from the references of the specified field?

  • 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-10T15:22:35+00:00Added an answer on June 10, 2026 at 3:22 pm

    UNIQUE KEY constraint can be used in this case:

    CREATE TABLE users (id int not null, primary key (id));
    INSERT INTO users VALUES (1), (2), (3);
    
    CREATE TABLE orders (id int not null, primary key (id));
    INSERT INTO orders VALUES (1), (2), (3);
    
    CREATE TABLE users_orders (uid int NOT NULL, oid int DEFAULT NULL,
                     FOREIGN KEY (uid) REFERENCES users (id),
                     FOREIGN KEY (oid) REFERENCES orders (id),
                     UNIQUE KEY (uid, oid));
    
    INSERT INTO users_orders (uid) VALUES (1);
    

    Here’s a demo to play with.

    But be aware that you (probably) will still be able to insert more than one combination of specific UID - NULL in your table ( I assume here that you use InnoDB ). For example, this query…

    INSERT INTO users_orders o (uid) VALUES (1), (1);
    

    … will succeed as well (as it won’t break any existing constraints).

    If this should be strictly prohibited, you better use some dummy value both in users and orders table as well – to make the foreign key constraint in your intersection table usable.

    As a sidenote, I admit I’m a bit surprised by this situation. Intersection tables are usually designed so that they don’t have any NULLable fields: it’s LEFT (or RIGHT) JOIN in query that should be used to include users that don’t have any orders, for example. Could you explain a bit why this decision (to make OID nullable) was chosen at first hand?

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

Sidebar

Related Questions

Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the following classes : public class MyProductCode { private String
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have table with column 'URL' whrere I store urls like this
Let's consider the following scenario: I've a master table with a detail table. The
Let's suppose I have this piece of code. foreach(string value in myList) { 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.