Automatic primary key increment without setting an identifier

I use the following syntax to insert new records (I don't want duplicate keys):

insert into tbl(key) values select max(key)+1 from tbl

Someone says that he will have a concurrency problem.
Is it correct?

SELECT -> LOCK table -> INSERT

or

LOCK table -> SELECT -> INSERT

Which of the above is correct?

+3
source share
3 answers

The selection locks the table, and you do everything in one statement, it looks like you should be fine (except maybe for the performance to calculate max (key)).

0
source

If this happens inside the transaction, everything will be fine.

+1
source

, GUID , "" .

+1

Source: https://habr.com/ru/post/1720781/


All Articles