{"id":513,"date":"2011-08-08T13:10:02","date_gmt":"2011-08-08T13:10:02","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=513"},"modified":"2023-10-10T01:43:56","modified_gmt":"2023-10-10T08:43:56","slug":"mysql-last_insert_id","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-functions\/mysql-last_insert_id\/","title":{"rendered":"MySQL LAST_INSERT_ID Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the MySQL <code>LAST_INSERT_ID()<\/code> function to return the first automatically generated integer successfully inserted for an&nbsp;<code class=\"literal\">AUTO_INCREMENT<\/code> column.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL LAST_INSERT_ID() function<\/h2>\n\n\n\n<p>In database design, we often use a surrogate key to generate unique <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-int\/\">integer<\/a> values&nbsp;for the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-primary-key\/\">primary key<\/a> column of a table by using the <code>AUTO_INCREMENT<\/code> attribute:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name(\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n    ...,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(<span class=\"hljs-keyword\">id<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert a row into the table<\/a> without specifying a value for the <code>id<\/code> column, MySQL automatically generates a sequential unique integer for the <code>id<\/code> column.<\/p>\n\n\n\n<p>The <code>LAST_INSERT_ID()<\/code> function returns the first automatically generated integer ( <code>BIGINT UNSIGNED<\/code>) successfully inserted for an&nbsp;<code class=\"literal\">AUTO_INCREMENT<\/code> column.<\/p>\n\n\n\n<p>If you <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">insert multiple rows<\/a> into the table using a single <code>INSERT<\/code> statement, the <code>LAST_INSERT_ID()<\/code> function returns the first automatically generated value only.<\/p>\n\n\n\n<p>If the insertion fails, the result returned by the <code>LAST_INSERT_ID()<\/code> remain unchanged.<\/p>\n\n\n\n<p>The <code>LAST_INSERT_ID()<\/code> function works based on the client-independent principle. It means the value returned by the <code>LAST_INSERT_ID()<\/code> function for a specific client is the value generated by that client only to ensure that each client can obtain its own unique ID.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL LAST_INSERT_ID function examples<\/h2>\n\n\n\n<p>Let&#8217;s look at an example of using MySQL <code>LAST_INSERT_ID<\/code> &nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using MySQL LAST_INSERT_ID() function to get value when inserting one row into a table<\/h3>\n\n\n\n<p>First,&nbsp;<a title=\"MySQL CREATE TABLE\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a new table<\/a> named <code>messages<\/code> that has the <code>id<\/code> column as the primary key and its value is automatically generated:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> messages(\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    description <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">250<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, insert a new row into the <code>messages<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> messages(description)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'MySQL last_insert_id'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third,&nbsp;use the <code>LAST_INSERT_ID<\/code> function to get the inserted value of the <code>id<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">LAST_INSERT_ID<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"129\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png\" alt=\"MySQL LAST_INSERT_ID Function Example\" class=\"wp-image-9083\"\/><\/figure>\n\n\n\n<p>Fourth, attempt to insert a null value into the <code>description<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> messages(description) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-literal\">NULL<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 1048. Column 'description' cannot be null<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, use the <code>LAST_INSERT_ID<\/code> function to get the last automatically inserted value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">LAST_INSERT_ID<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"129\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png\" alt=\"MySQL LAST_INSERT_ID Function Example\" class=\"wp-image-9083\"\/><\/figure>\n\n\n\n<p>The result is unchanged.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using MySQL LAST_INSERT_ID() function to get value when inserting multiple rows into a table<\/h3>\n\n\n\n<p>First, insert three rows into the <code>messages<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> messages(description) \n<span class=\"hljs-keyword\">VALUES<\/span>\n    (<span class=\"hljs-string\">'Insert multiple rows'<\/span>), \n    (<span class=\"hljs-string\">'LAST_INSERT_ID() example'<\/span>), \n    (<span class=\"hljs-string\">'MySQL AUTO_INCREMENT'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, query data from the <code>messages<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> messages;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"204\" height=\"99\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example-2.png\" alt=\"\" class=\"wp-image-9085\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example-2.png 204w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example-2-200x97.png 200w\" sizes=\"auto, (max-width: 204px) 100vw, 204px\" \/><\/figure>\n\n\n\n<p>Third, use the <code>LAST_INSERT_ID()<\/code> function to get the inserted value:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">LAST_INSERT_ID<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"128\" height=\"39\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-insert-multiple-rows.png\" alt=\"MySQL LAST_INSERT_ID Function - insert multiple rows\" class=\"wp-image-9086\"\/><\/figure>\n\n\n\n<p>As you can see clearly from the output, the <code>LAST_INSERT_ID()<\/code> function returns the generated value of the first row successfully inserted, not the last row.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using MySQL LAST_INSERT_ID() function in a stored procedure<\/h3>\n\n\n\n<p>First, create two tables <code>accounts<\/code> and <code>phones<\/code> for testing:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> accounts (\n    account_id <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    first_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    last_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> phones (\n    phone_id <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n    account_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    phone <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">25<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    description <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (phone_id , account_id),\n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (account_id)\n        <span class=\"hljs-keyword\">REFERENCES<\/span> accounts (account_id)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/getting-started-with-mysql-stored-procedures\/\">create a stored procedure<\/a> that inserts an account with a phone number into both tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">DELIMITER $$\n\n<span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PROCEDURE<\/span> CreateAccount(\n    fname <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>), \n    lname <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>),\n    phone <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">25<\/span>),\n    description <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>)\n)\n<span class=\"hljs-keyword\">BEGIN<\/span>\n    <span class=\"hljs-keyword\">DECLARE<\/span> l_account_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>;\n    \n    <span class=\"hljs-keyword\">START<\/span> <span class=\"hljs-keyword\">TRANSACTION<\/span>;\n    <span class=\"hljs-comment\">-- Insert account data<\/span>\n    <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> accounts(first_name, last_name)\n    <span class=\"hljs-keyword\">VALUES<\/span>(fname, lname);\n    \n    <span class=\"hljs-comment\">-- get account id<\/span>\n    <span class=\"hljs-keyword\">SET<\/span> l_account_id = <span class=\"hljs-keyword\">LAST_INSERT_ID<\/span>();\n    \n    <span class=\"hljs-comment\">-- insert phone for the account<\/span>\n    IF l_account_id &gt; 0 THEN\n\t<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> phones(account_id, phone, description)\n        <span class=\"hljs-keyword\">VALUES<\/span>(l_account_id,phone,description);\n        <span class=\"hljs-comment\">-- commit<\/span>\n        <span class=\"hljs-keyword\">COMMIT<\/span>;\n     ELSE\n\t<span class=\"hljs-keyword\">ROLLBACK<\/span>;\n    <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n<span class=\"hljs-keyword\">END<\/span>$$\n\nDELIMITER ;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The stored procedure inserts a row into the <code>accounts<\/code> table, get the account id using the <code>LAST_INSERT_ID()<\/code> function, and use this account id to insert a phone into the <code>phones<\/code> table. <\/p>\n\n\n\n<p>A row in the <code>phones<\/code> table should only exist if there is a corresponding row in the <code>accounts<\/code> table, therefore, we wrap the insert statements in a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-transactions\/\">transaction<\/a>.<\/p>\n\n\n\n<p>Third, call the stored procedure <code>CreateAccount<\/code> to create a new account with a phone number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CALL<\/span> CreateAccount(\n    <span class=\"hljs-string\">'John'<\/span>,\n    <span class=\"hljs-string\">'Doe'<\/span>,\n    <span class=\"hljs-string\">'(408)-456-4567'<\/span>,\n    <span class=\"hljs-string\">'Emergency Contact'<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, query data from the <code>accounts<\/code> and <code>phones<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> accounts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"226\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-accounts-table.png\" alt=\"MySQL LAST_INSERT_ID - Stored procedure accounts table\" class=\"wp-image-9090\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-accounts-table.png 226w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-accounts-table-200x35.png 200w\" sizes=\"auto, (max-width: 226px) 100vw, 226px\" \/><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> phones;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"354\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-phones-table.png\" alt=\"MySQL LAST_INSERT_ID - Stored procedure phones table\" class=\"wp-image-9092\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-phones-table.png 354w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Stored-procedure-phones-table-200x21.png 200w\" sizes=\"auto, (max-width: 354px) 100vw, 354px\" \/><\/figure>\n\n\n\n<p>It works as expected.<\/p>\n\n\n\n<p>Finally, attempt to create a new account with the value of the last name is null:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CALL<\/span> CreateAccount(\n   <span class=\"hljs-string\">'Jane'<\/span>, \n    <span class=\"hljs-literal\">null<\/span> ,\n    <span class=\"hljs-string\">'(408)-456-1111'<\/span>,\n    <span class=\"hljs-string\">'Emergency Contact'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 1048. Column 'last_name' cannot be null<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the MySQL <code>LAST_INSERT_ID<\/code> function to get the first automatically generated integer successfully inserted for an <code>AUTO_INCREMENT<\/code> column.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"513\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-functions\/mysql-last_insert_id\/\"\n\t\t\t\tdata-post-title=\"MySQL LAST_INSERT_ID Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"513\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-functions\/mysql-last_insert_id\/\"\n\t\t\t\tdata-post-title=\"MySQL LAST_INSERT_ID Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>in this tutorial, you will learn how to use the MySQL LAST_INSERT_ID() function to returns the first automatically generated integer successfully inserted for an\u00a0AUTO_INCREMENT column.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":499,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-513","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL LAST_INSERT_ID Function<\/title>\n<meta name=\"description\" content=\"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL LAST_INSERT_ID Function\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-10T08:43:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx\",\"name\":\"MySQL LAST_INSERT_ID Function\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-LAST_INSERT_ID-Function-Example.png\",\"datePublished\":\"2011-08-08T13:10:02+00:00\",\"dateModified\":\"2023-10-10T08:43:56+00:00\",\"description\":\"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-LAST_INSERT_ID-Function-Example.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/09\\\/MySQL-LAST_INSERT_ID-Function-Example.png\",\"width\":129,\"height\":38,\"caption\":\"MySQL LAST_INSERT_ID Function Example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-last_insert_id.aspx#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Functions\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-functions.aspx\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL LAST_INSERT_ID Function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL LAST_INSERT_ID Function","description":"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx","og_locale":"en_US","og_type":"article","og_title":"MySQL LAST_INSERT_ID Function","og_description":"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx","og_site_name":"MySQL Tutorial","article_modified_time":"2023-10-10T08:43:56+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx","url":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx","name":"MySQL LAST_INSERT_ID Function","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png","datePublished":"2011-08-08T13:10:02+00:00","dateModified":"2023-10-10T08:43:56+00:00","description":"This tutorial shows how to use the MySQL LAST_INSERT_ID() function to return the first automatically generated integer inserted for an\u00a0AUTO_INCREMENT column.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/09\/MySQL-LAST_INSERT_ID-Function-Example.png","width":129,"height":38,"caption":"MySQL LAST_INSERT_ID Function Example"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-last_insert_id.aspx#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Functions","item":"https:\/\/www.mysqltutorial.org\/mysql-functions.aspx"},{"@type":"ListItem","position":3,"name":"MySQL LAST_INSERT_ID Function"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=513"}],"version-history":[{"count":2,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/513\/revisions"}],"predecessor-version":[{"id":10931,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/513\/revisions\/10931"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/499"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}