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

  • Home
  • SEARCH
  • 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 3666332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:52:03+00:00 2026-05-19T01:52:03+00:00

Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`

  • 0

Suppose I have a simple MySQL table that looks like this:

CREATE TABLE  `my_table` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 8 ) NOT NULL
) ENGINE = MYISAM ;

I want to create two rows that are related to each other. I’ve considered doing it two ways.


First strategy: Adding a “related_to” field to the table.

I am using AUTO_INCREMENT to calculate the ID, I’ll need 5 steps to do this.

  1. INSERT INTO my_table (name) VALUES (‘abc’);
  2. Retrieve insert ID (let’s call it $first_id)
  3. INSERT INTO my_table (name, related_to) VALUES (‘xyz’, ‘$last_id’);
  4. Retrieve insert ID (let’s call it $second_id)
  5. UPDATE my_table SET related_to = ‘$second_id’ WHERE id = ‘$first_id’;

Second strategy: A separate table linking with both IDs. Can do this in 5 steps as well.

  1. INSERT INTO my_table (name) VALUES (‘abc’);
  2. Retrieve insert ID (let’s call it $first_id)
  3. INSERT INTO my_table (name) VALUES (‘xyz’);
  4. Retrieve insert ID (let’s call it $second_id)
  5. INSERT INTO link_table (item1,item2) VALUES (‘$first_id’, ‘$second_id’);

Which way would be best in terms of efficiency, or is there an even more efficient way of doing it that I am missing?

Thanks for your advice.

  • 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-19T01:52:04+00:00Added an answer on May 19, 2026 at 1:52 am

    Using a third column will be more efficient in terms of processing – queries will run slightly faster.
    Using a separate relationship will be more efficient in terms of memory usage – your database will occupy slightly less memory.

    I’d be tempted to go for the relationship table because it results in a normalized design, although it will be a bit harder to work with. It will avoid any issues you might have where the related_to values become out of sync. For example, A and B are related to each other. A has related_to=B, B has related_to=A. This is duplicate data so requires a bit more management to keep consistent if you use a third column.

    Depending on the volume of your data, it’s unlikely that you’ll have any real performance issues with either of these designs (the difference will be in terms of milliseconds), so you should probably take whichever feels more comfortable.

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

Sidebar

Related Questions

Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose I have a simple XHTML document that uses a custom namespace for attributes:
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
I have a MySQL table that contains the columns id , created_date , and
I have a simple question that keeps me up all night :( . Suppose
I have a simple question. Is there a way ( using reflections I suppose
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have an engine pointing at MySQL database: engine = create_engine('mysql://arthurdent:answer42@localhost/dtdb', echo=True) I
Let us suppose I have to deal with lots of grandmothers that have lots

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.