Dear all,
I have to write a query wherein i need to allocate a ID
(unique key) for a particular record which is not being used / is not being generated / does not exist in database.
In short, I need to generate an id
for a particular record and show it on print screen.
E. g.:
ID Name 1 abc 2 def 5 ghi
So, the thing is that it should return ID=3
as the next immediate which is not being generated yet, and after this generation of the id
, I will store this data back to database table.
It's not an HW: I am doing a project, and I have a requirement where I need to write this query, so I need some help to achieve this.
So please guide me how to make this query, or how to achieve this.
Thanks.
I am not able to add comments,, so thats why i am writing my comments here.. I am using MySQL as the database..
My steps would be like this:-
1) Retrieve the id from the database table which is not being used..
2) As their are no. of users (website based project), so i want no concurrency to happen,, so if one ID is generated to one user, then it should lock the database, until the same user recieves the id and store the record for that id.. After that, the other user can retrieve the ID whichever is not existing.. (Major requirement)..
How can i achive all these things in MySQL,, Also i suppose Quassnoi's answer will be worth,, but its not working in MySQL.. so plz explain the bit about the query as it is new to me.. and will this query work in MySQL..