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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:42:07+00:00 2026-05-23T14:42:07+00:00

How can I achieve an insert query on 2 tables that will insert the

  • 0

How can I achieve an insert query on 2 tables that will insert the primary key set from one table as a foreign key into the second table.

Here’s a quick example of what I’m trying to do, but I’d like this to be one query, perhaps a join.

INSERT INTO Table1 (col1, col2) VALUES ( val1, val2 )
INSERT INTO Table2 (foreign_key_column) VALUES (parimary_key_from_table1_insert)

I’d like this to be one join query.
I’ve made some attempts but I can’t get this to work correctly.

  • 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-23T14:42:08+00:00Added an answer on May 23, 2026 at 2:42 pm

    Despite what others have answered, this absolutely is possible, although it takes 2 queries made consecutively with the same connection (to maintain the session state).

    Here’s the mysql solution (with executable test code below):

    INSERT INTO Table1 (col1, col2) VALUES ( val1, val2 );
    INSERT INTO Table2 (foreign_key_column) VALUES (LAST_INSERT_ID());
    

    Note: These should be executed using a single connection.

    Here’s the test code:

    create table tab1 (id int auto_increment primary key, note text);
    create table tab2 (id int auto_increment primary key, tab2_id int references tab1, note text);
    insert into tab1 values (null, 'row 1');
    insert into tab2 values (null, LAST_INSERT_ID(), 'row 1');
    select * from tab1;
    select * from tab2;
    mysql> select * from tab1;
    +----+-------+
    | id | note  |
    +----+-------+
    |  1 | row 1 |
    +----+-------+
    1 row in set (0.00 sec)
    
    mysql> select * from tab2;
    +----+---------+-------+
    | id | tab2_id | note  |
    +----+---------+-------+
    |  1 |       1 | row 1 |
    +----+---------+-------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In mysql, you can insert multiple rows to a table in one query for
I am trying to retieve data from a MySQL table and insert into a
Kindly give me some example that how we can use INSERT INTO ALL STATEMENT
Anyone know of a control for asp.net that can achieve this either server or
As title; is there any function that can achieve this?
How can I achieve this? I am trying to load content from the database
I want to insert certain values from an existing database into another. I know
As you can see, I have got 2 tables here, table 1 and table
I have several transactions to insert form data into oracle table. if (InsertQuarterly() ==
I have a PHP script that inserts rows into a table based on selected

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.