cfsavant
02-09-02, 14:32
| I have in excerpt from some db manual which explains the pro's and con's of different methods of primary key generation. The particular method explained uses a separate table named next_key to hold the next value for the primary key. I have three questions about the code in the excerpt below. 1. why is it that the update increments the next key in the next_key table and then the select increments it as well. 2. In this approach, isn't it possible that after the update occurs, the lock on the row is released and when the select statement below executes some other process already has a lock on the table? The following is an excerpt from an article from Sybase found at: http://www.wallaceis.com/resource/article/Sybase/Surrogate%20Primary%20Key%20Generation.htm --------------------------------------------------------------- Next Key Table A more versatile approach is to introduce a separate table to store the next key value. 1> begin tran 2> declare @next_key 3> update next_key set next_key = next_key + 1 4> select @next_key = next_key + 1 5> from next_key 6> insert into data_table (key_column, ...) 7> values (@next_key, ...) 8> commit tran 9> go The SQL is atomic and uniqueness is guaranteed. Notice that the update statement precedes the select statement. The update first acquires an exclusive lock on the table ensuring that no other processes can obtain a next key value from the table. In the above example, the transaction includes both the generation of the key and the insert into the data table. The transaction is completely single-threaded. Consequently, there are no gaps in the sequences of key values inserted into the data table. |