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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:17:20+00:00 2026-05-12T18:17:20+00:00

I have two MySQL database tables that are meant to hold data for eshop

  • 0

I have two MySQL database tables that are meant to hold data for eshop orders. They’re built as such (extremely simplified version):

CREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment
PRIMARY KEY  (`id`)
);

CREATE TABLE `order_items` (
  `id` int(11) NOT NULL auto_increment,
  `orderID` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
)

The relationship between the two is that orders.id corresponds to order_items.orderID.

I’m using a transaction to place a new order, however have a problem preserving the above relationship. In order to get the new order id. I have to commit the orders INSERT query, get the autoincremented id and then start another transaction for the order items. Which pretty much defeats the point of using transactions.

I could insert the new order in the orders table and then try something like

INSERT INTO order_items(orderID) VALUES(LAST_INSERT_ID()) 

which I assume would work. However after the first order item is inserted LAST_INSERT_ID() would stop returning the order id and instead return the order item id making it impossible to use this query to insert another order item.

Is there a way to make this whole thing work within a single transaction or should I give up and use a procedure instead?

  • 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-12T18:17:21+00:00Added an answer on May 12, 2026 at 6:17 pm

    WOuld this work?:

    INSER QUERY;
    SET @insertid = LAST_INSERT_ID();
    INSERT INTO `order_items` SET `OrderID` = @insertid;
    

    All in one statement. You will have to double check the syntax

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

Sidebar

Related Questions

I have a web application that relies on a MySQL database, for which I
The actual question is a little more complex than that, so here goes. I
I have an interesting encryption problem at hand. I do not know if it
This is an architecture question, but its solution lies in ColdFusion and MySQL structure--or
Say you’re designing an application that, by requirement, allows a user the flexibility of
Suppose I have a application form which contain lot of boxes like email,username,age etc...I
I am working on a database schema, and am trying to make some decisions
I am kinda new to database designing so i ask for some advices or
I've been beating my brains in over this and I'm making no progress. I'm

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.