In this tutorial, we will learn how to fix MySQL issue “MySQL LAST_INSERT_ID() returning 0”.
Are you wondering why LAST_INSERT_ID() returned 0? We shall investigate the cause and fix this issue.
![Fixing MySQL LAST_INSERT_ID() returning 0](https://www.tutorialkart.com/wp-content/uploads/2018/12/mysql-last-insert-id-0.png)
MySQL LAST_INSERT_ID() returns a non-negative id of the last inserted record when you have column with AUTO_INCREMENT attribute and the column is added to index.
We shall take an example table and investigate the process to solve the issue.
Consider the following students
table.
![MySQL - Solve LAST_INSERT_ID returns 0](https://www.tutorialkart.com/wp-content/uploads/2018/12/mysql-select-distinct-valules.png)
Now we shall add a column with AUTO_INCREMENT attribute.
ADVERTISEMENT
![MySQL add column with AUTO_INCREMENT PRIMARY KEY](https://www.tutorialkart.com/wp-content/uploads/2018/12/mysql-add-column-auto-increment-primary-key.png)
Try inserting a row to students
table.
![MySQL - Insert record into table](https://www.tutorialkart.com/wp-content/uploads/2018/12/mysql-insert-record-into-table.png)
Now as there is an AUTO_INCREMENT column that is added to INDEX, lets try the LAST_INSERT_ID() function.
![MySQL LAST_INSERT_ID() function solved](https://www.tutorialkart.com/wp-content/uploads/2018/12/mysql-last-insert-id-solved.png)