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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:26:29+00:00 2026-05-13T20:26:29+00:00

I have a table which has several ID columns to other tables. I want

  • 0

I have a table which has several ID columns to other tables.

I want a foreign key to force integrity only if I put data in there. If I do an update at a later time to populate that column, then it should also check the constraint.

(This is likely database server dependant, I’m using MySQL & InnoDB table type)

I believe this is a reasonable expectation, but correct me if I am wrong.

  • 1 1 Answer
  • 1 View
  • 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-05-13T20:26:30+00:00Added an answer on May 13, 2026 at 8:26 pm

    Yes, you can enforce the constraint only when the value is not NULL. This can be easily tested with the following example:

    CREATE DATABASE t;
    USE t;
    
    CREATE TABLE parent (id INT NOT NULL,
                         PRIMARY KEY (id)
    ) ENGINE=INNODB;
    
    CREATE TABLE child (id INT NULL, 
                        parent_id INT NULL,
                        FOREIGN KEY (parent_id) REFERENCES parent(id)
    ) ENGINE=INNODB;
    
    
    INSERT INTO child (id, parent_id) VALUES (1, NULL);
    -- Query OK, 1 row affected (0.01 sec)
    
    
    INSERT INTO child (id, parent_id) VALUES (2, 1);
    
    -- ERROR 1452 (23000): Cannot add or update a child row: a foreign key 
    -- constraint fails (`t/child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY
    -- (`parent_id`) REFERENCES `parent` (`id`))
    

    The first insert will pass because we insert a NULL in the parent_id. The second insert fails because of the foreign key constraint, since we tried to insert a value that does not exist in the parent table.

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

Sidebar

Related Questions

I have a table which has several one to many relationships with other tables.
I have a table called scheduler_sched which has several columns, including a column called
I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
my table have several records which has the same MemberID. i want to result
I have a table in DB called TABLE_3 which has several columns....and I wanna
I have a Model1.edmx, which has several tables. Now, I do not want all
I have a table called records which has several columns, one of which is
I have working website in PHP with a MySQL backend which has several tables
i have a table which has many fields but i want to get count
I have a CSV file which contains an ID and several other columns. I

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.