avoid key duplication for database insertion
<p>im not sure if the title describes my problem , 
i have table A that needs to use a unique key with each record,as following :</p>

<p>1.it takes the key from a table called counters(assume its 500).
2.it checks whether the last value is really used in table A (this is because someone might book the value then not complete to use it),(if used increment the value ,otherwise take it)</p>

<p>business wise the process is done through 2 steps: 
1.client click book id so that he/she have the key.
2.client fill the rest of the information and save , so that a new row is inserted to table A.</p>

<p>more than one client might be using the system concurrently , so tow of them might book same value </p>

<p>assume that user1 book 500 ,then user2 book 500 (user1 have not used it yet so user2 will take same key) ,then user1 inserted row into table A, then user2 inserted row into tableA </p>

<p>this case will cause a serious problem because both are using same key which is supposed to be unique </p>

<p>would you please suggest a solution for this </p>