Calculate next Primary Key - of specific format

后端 未结 3 1184
萌比男神i
萌比男神i 2020-12-21 11:58

I have a table which holds a list of IDs, and various other columns such as IDName.

The Primary Key of the table is the ID itself, however it does not auto_increment

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 12:23

    Unless your application is in very high demand the number of collisions will be very low. So I would simply retry if a key error is raised.

    select coalesce(max(id), 27770000) + 1
    from myTable
    where id / 10000 = 2777
    

    The coalesce is there in case that level/code does not yet exist.

提交回复
热议问题