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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:55:16+00:00 2026-05-23T17:55:16+00:00

What is the best way to insert rows into tables with references 1 to

  • 0

What is the best way to insert rows into tables with references 1 to 1 of each other?

I mean, in a MySQL 5.5 and tables InnoDB, I have a database design similar to the following
enter image description here

The problem arises when we try to insert rows in table1 and table2. Since there is no multi-table insert in MySQL, I can not insert a row becouse the foreign keys are NOT NULL fields in both tables and should be inserted simultaneously in both.

Which is the bes way to solve this problem?

I have in mind 3 possible solutions, but I want to know if there are more than these or which is the best and why.

  1. Set the foreign key field as NULLABLE and after insert one row in a table, insert the other one and afterwards, update de first one.

  2. Just as indicated above but with an special value like -1. First, insert in one table with foreign key = -1 that is equivalent to NULL but avoiding set the field as NULLABLE. Afterwards, we insert the row in the other table and update the first one inserted.

  3. Create a relational table between both though it is not really necessary because it is a 1 to 1 ratio

Thanks!!

EDIT
I briefly explain what I need this circular relationship: It is a denormalization from the parent table to one of its childs. It is made in order of high performance to have always the reference of the best ranked child from a parent table.

  • 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-05-23T17:55:16+00:00Added an answer on May 23, 2026 at 5:55 pm

    I’ll make this an answer as I feel this is a design flaw.

    First, if the two tables are in true 1:1 relationship, why don’t you just have one table?


    Second, if it’s not a true 1:1 relationship but a supertype-subtype problem, you don’t need this circular foreign keys either. Lets say table1 is Employee and table2 is Customer. Off course most customers are not employees (and vice-versa). But sometimes a customer may be an employee too. This can be solved having 3 tables:

    Person
    ------
    id
    PRIMARY KEY: id
    
    Employee
    --------
    personid
    lastname
    firstname
    ... other data
    PRIMARY KEY: personid
    FOREIGN KEY: personid
        REFERENCES Person(id)
    
    Customer
    --------
    personid
    creditCardNumber
    ... other data
    PRIMARY KEY: personid
    FOREIGN KEY: personid
        REFERENCES Person(id)
    

    In the scenario you describe you have two tables Parent and Child having 1:N relationship. Then, you want to store somehow the best performing (based on a defined calculation) child for every parent.

    Would this work?:

    Parent
    ------
    id
    PRIMARY KEY: id
    
    Child
    -----
    id
    parentid
    ... other data
    PRIMARY KEY: id
    FOREIGN KEY: parentid
        REFERENCES Parent(id)
    UNIQUE KEY: (id, parentid)             --- needed for the FK below
    
    BestChild
    ---------
    parentid
    childid
    ... other data
    PRIMARY KEY: parentid
    FOREIGN KEY: (childid, parentid)
        REFERENCES Child(id, parentid)
    

    This way, you enforce the wanted referential integrity (every BestChild is a Child, every Parent has only one BestChild) and there is no circular path in the References. The reference to the best child is stored in the extra table and not in the Parent table.

    You can find BestChild for every Parent by joining:

    Parent
      JOIN BestChild
        ON Parent.id = BestChild.parentid
      JOIN Child
        ON BestChild.childid = Child.id
    

    Additionally, if you want to store best children for multiple performance tests (for different types of tests, or tests in various dates), you can add a test field, and alter the Primary Key to (test, parentid):

    BestChild
    ---------
    testid
    parentid
    childid
    ... other data
    PRIMARY KEY: (testid, parentid)
    FOREIGN KEY: (childid, parentid)
        REFERENCES Child(id, parentid)
    FOREIGN KEY: testid
        REFERENCES Test(id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best/most time efficient way to insert 1000 rows into one table
What's the best way to insert an element into a page's HEAD directly before
I am wondering what's the best way to insert customization hooks into my application.
I'm trying to figure out the best way to insert a record into a
The best way to store images into MySQL is by storing the image location
What is the best way to shred XML data into various database columns? So
What is the best plain javascript way of inserting X rows into a table
What is the best way to insert alot of text into a word document?
I am wondering the best way to insert a multidimensional array of values into
does anyone know the best way to insert a column in a datatable at

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.