0

I am working on a web application. It will have multiple users. I am using mysql as database.

In my application i am fetching the latest id (from the database, using max(id)) , and then generating the next id for the new registration. This approach is incorrect, as the id might change between the time i update an id and i ask for the latest id. I googled up , and found "last_insert_id()"

But, i tried "SELECT last_insert_id() FROM rdtype ", but its gives the same number of 0 as the no of records.

How should i proceed?

Ravi.
  • 674
  • 2
  • 9
  • 21

2 Answers2

1

You should use AUTO_INCREMENT attribute for id. MySQL will handle incrementing ID at each insert. The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows.

Check the MySQL reference at http://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html

jdiver
  • 2,228
  • 1
  • 19
  • 20
0

I am using Hibernate for Database connection and works. There are 2 columns in my db, one is ID (generator type is increment, via hibernate), and the other is Registration id. I am getting the max id, then getting the registration id for that max id, then adding 1 to that registration id to get the new registration Id.... Moreover, i need to get this new registration id before saving it to the database.