menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Databases

>

Using LAST...
source image

Dev

1M

read

421

img
dot

Image Credit: Dev

Using LAST_INSERT_ID() in MySQL to Insert Related Data

  • When using AUTO_INCREMENT in MySQL, you often need to retrieve the ID of a newly inserted record. By using LAST_INSERT_ID(), you can easily obtain the ID generated from the most recent INSERT statement, making easier to insert related data.
  • Inserting Related Data Using LAST_INSERT_ID(): Let's take the following two tables as an example...
  • SQL Example: Use LAST_INSERT_ID() immediately after INSERT to retrieve the newly generated id. Use the obtained id to insert related records into order_items.
  • What Happens When ROLLBACK is Used? If ROLLBACK is executed, the AUTO_INCREMENT value is not reset, even though the insert operation is undone.

Read Full Article

like

25 Likes

For uninterrupted reading, download the app