Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When a new row is inserted into a table in a relational database, you often need to know the value of the id column for the new row so that it can be used in another table or for interactive forums or other purposes. MySQL provides a function called last insert id that provides the most recently generated value of an auto-increment column. For example, here in this insert album SQL function you notice we have this simple query inserted into the album. The question marks are for the parameters and those parameters are filled in here in the execute call with title artist, label, and released.
And then down here there's a call to lastInsertId. It has a slightly different form for the. Postgress database but it gets the last inserted ID. It'll get the ID of this column after it's been inserted into the database and that's returned. It's eventually used in this album edit, function up here. Which gets past that ID for building the Album Edit form. And so, if I come out here to the application and I insert a new album, 1 January 2014.
And press add album you'll notice that here is this form and it is pulled up this new inserted record and populated the form. And you'll notice that happens here with this fetch_album id call. And then we can add tracks and we can edit it, change it around and what have you. And then I can come back here and I can delete it if I want to. And this all happens because I'm able to keep track of that ID when I insert that new record in the database.
So MySQL's last insert ID function provides the value. Of the most recently generated auto-increment column, this is very useful and often necessary for creating relational database applications.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98243 Viewers
61 Video lessons · 85549 Viewers
71 Video lessons · 69504 Viewers
56 Video lessons · 101842 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.