Questions tagged [last-insert-id]

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

That said, this functionality is not ANSI. Sequences are now ANSI, supported by DB2, Oracle, PostgreSQL, and SQL Server "Denali"). The ANSI equivalent to LAST_INSERT_ID would be: CURRVAL ( NEXTVAL is used to get the next value).

Documentation:

215 questions
1
vote
2 answers

How can I get the last insert value from MySQL database and update a field in Access?

I have a complex database involving human remains, so the Access forms to input the data are also quite complicated. I have several subforms (Skeleton_index, Element_index, and a few others). The two subforms I mentioned by name send data to their…
kelkka
  • 874
  • 7
  • 18
1
vote
2 answers

Why is setting SQL variable is creating an error?

Following SQL statement is creating an error with message : "Message: Fatal error encountered during command execution." "Inner exception: Parameter '@LastUserID' must be defined." If I directly use LAST_INSERT_ID() instead of LastUserID, it always…
PCG
  • 2,049
  • 5
  • 24
  • 42
1
vote
2 answers

C# Running 3 MYSQL insert queries with single button press - Data from textboxes and listview

I was wondering if you can help me. Firstly I wanted to say that this is my first application in C# that I am created so please accept that my code is not perfect. What I want to achieve: My application will be a Car Repair Management App. I have…
1
vote
1 answer

MySQL: Insert data from one file into two tables, one with auto_increment id, and one join table

I've got data with 500M+ records, in a file with 2 fields, blob and c_id. There are also two other files with the same data in other formats: A file with 90M unique blobs. A file with a blob, and a comma-separated list of c_ids per record. I've got…
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
1
vote
1 answer

Copy Row Get Last Insert ID

I am copying a row in one of the tables in my database and trying to get the last inserted ID. Copying the row works fine, and the AI field updates properly, but it is not returning the last insert ID (although it does return true - or if I echo…
HG123
  • 23
  • 5
1
vote
1 answer

PDO inserting last insert id into two tables

I'm attempting to insert the same insertId into a couple of link tables, however I've discovered that apparently the insert id gets reset to zero after executing the first method. These methods are separate as they get reused in other places. …
nomistic
  • 2,902
  • 4
  • 20
  • 36
1
vote
1 answer

Insert rows for an m:m relationship (pupil-evening_activity) consistently

I have three tables in my database (MySQL/InnoDB): pupil, evening_activity and pupil_evening_activity. As you can guess there is an m:m relationship between pupil and evening_activity. I have a HTML form to insert new pupils and associate to them…
tirenweb
  • 30,963
  • 73
  • 183
  • 303
1
vote
2 answers

Symfony - Get last id with doctrine

I have just developed a controller that consists of creating a subject with the ability to add one or more questions. It works, but I'm not really satisfied with the quality of my code: Indeed when I want to create a new topic, I can not retrieve…
David Fournier
  • 11
  • 1
  • 1
  • 2
1
vote
0 answers

Using LAST_INSERT_ID() over two functions which both use execute();

One submit button. Two tables. On clicking submit a marker is created, along with its lng and lat values. This information is added to marker table. The marker ID is a FK in the submissions table. Documentation for LAST_INSERT_ID(); says: The ID…
Dan
  • 951
  • 1
  • 23
  • 46
1
vote
1 answer

return mysql last insert id as a link variable

I have the following php pages. I need to return the last inserted id of the INSERT statement to my php page as a link variable. My code is as follows: room_reservation.php
user3889963
  • 477
  • 3
  • 6
  • 17
1
vote
0 answers

How to get LAST_INSERT_ID() if the insert occurred in a trigger?

I thought I was witnessing some odd behavior. Upon inserting a record, LAST_INSERT_ID() is giving me incorrect results. Then I read http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id For stored functions and…
user1032531
  • 24,767
  • 68
  • 217
  • 387
1
vote
0 answers

How to get last insert row in laravel 5,my id is autoincrementing

My tid is autoincrementing, i want to get that tid. or I want to get the last inserted row. I need to use this tid in another table. Please help me class Transaction extends Model { protected $table = 'tbl_transaction'; public function…
Visal Varghese
  • 436
  • 5
  • 14
1
vote
0 answers

MySQL: obtain primary key value generated by before-insert trigger

In MySQL, is there any way to obtain a primary key value generated by a before-insert trigger? I know that select last_insert_id() can return the first value of an auto_increment column from a previous insert, but I can't seem to get it to work…
XDR
  • 4,070
  • 3
  • 30
  • 54
1
vote
0 answers

MySQL Loop with last_insert_id()

I'm trying to migrate one DB into another using only MySQL, it's two WordPress databases and I want to put all the posts of a specific post type (table wp_posts) and all their respective post metas (table wp_postmeta, uses wp_posts ID). My logic was…
Dogmatics
  • 67
  • 8
1
vote
2 answers

MySQL LAST_INSERT_ID Query

I've had a look at the official documentation but I'm still a bit confused. Say I have a procedure and it runs and performs an insert and then I request LAST_INSERT_ID(), am I getting the last insert id from the insert just done by my instance of…
llanato
  • 2,508
  • 6
  • 37
  • 59