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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:09:49+00:00 2026-05-10T17:09:49+00:00

In mysql, how do I get the primary key used for an insert operation,

  • 0

In mysql, how do I get the primary key used for an insert operation, when it is autoincrementing.

Basically, i want the new autoincremented value to be returned when the statement completes.

Thanks!

  • 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. 2026-05-10T17:09:49+00:00Added an answer on May 10, 2026 at 5:09 pm

    Your clarification comment says that you’re interested in making sure that LAST_INSERT_ID() doesn’t give the wrong result if another concurrent INSERT happens. Rest assured that it is safe to use LAST_INSERT_ID() regardless of other concurrent activity. LAST_INSERT_ID() returns only the most recent ID generated during the current session.

    You can try it yourself:

    1. Open two shell windows, run mysql client in each and connect to database.
    2. Shell 1: INSERT into a table with an AUTO_INCREMENT key.
    3. Shell 1: SELECT LAST_INSERT_ID(), see result.
    4. Shell 2: INSERT into the same table.
    5. Shell 2: SELECT LAST_INSERT_ID(), see result different from shell 1.
    6. Shell 1: SELECT LAST_INSERT_ID() again, see a repeat of earlier result.

    If you think about it, this is the only way that makes sense. All databases that support auto-incrementing key mechanisms must act this way. If the result depends on a race condition with other clients possibly INSERTing concurrently, then there would be no dependable way to get the last inserted ID value in your current session.

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

Sidebar

Related Questions

How get data from mysql database? I use ASP.NET and C#.Previously, I used LINQ
How can I insert a NULL value into a database field (MySQL) with char(2)?
I use MYSQL to create a table with combined primary key. For example, col_a
I am having trouble with mysql triggers assigning primary key values. Consider my tables:
I've stored image information in my mysql database like below... id, int(10), PRIMARY KEY
when i try to insert this code i get an mysql error syntax CREATE
I'm just trying to get MySQL to store time in GMT... I've read the
I get a mysql error: #update (ActiveRecord::StatementInvalid) Mysql::Error: #HY000Got error 139 from storage engine:
I'm trying to get the mysql gem installed with a fresh install of Snow
How do I get my mysql database to return 0 if the neighborhood in

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.