Skip to content
Advertisement

SQL: Insert into table and return new index?

I have a database with auto-increment index. When I inserting a new item in the table, I don’t know the index number. Is there a way to return new index of my item simultaneously?

I could assume that the new index would be = max-index + 1. Is there some standard way to solve this?

Thanks for help.

Advertisement

Answer

For MySQL, you use the LAST_INSERT_ID() function, like this:

INSERT INTO tbl (auto,text) VALUES(NULL,'text');
SELECT LAST_INSERT_ID();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement