AutoIncrement fields on databases without autoincrement field

后端 未结 5 522
孤街浪徒
孤街浪徒 2020-12-05 12:00

In MS Sql Server is easy create autoincrement fields. In my systems I stopped to use autoincrement fields for primary keys, and now I use Guid\'s. It was awesome, I\'ve got

5条回答
  •  隐瞒了意图╮
    2020-12-05 12:53

    The traditional solution is to have a table of ids that look something like this

    CREATE TABLE ids (
      tablename VARCHAR(32) NOT NULL PRIMARY KEY,
      nextid INTEGER
    )
    

    which s populated with one row per table when you create the database.

    You then do a select to get the next next id for the table you are inserting into, increment it and then update the table with the new id. Obviously, there are locking issues here, but for databases with moderate insert rates it works well. And it is completely portable.

提交回复
热议问题