Auto generate serial numbers

前端 未结 2 679
南笙
南笙 2021-01-29 11:58

I wrote the count code below with the aim of having it produce auto-serial numbers for my data in place of the MySQL serial number which goes once the row is deleted. But when I

2条回答
  •  生来不讨喜
    2021-01-29 12:31

    In MySQL each table can have one and only one auto increment field. This is the correct way to generate a unique value like you have asked for each row.

    http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

    The method you have used is error prone. When you test it may work, but when it is used by multiple users it will result in duplicate ids. This is because if user and and user be request at the same time, they will both get the count at the same time, add 1 to it and then insert with the same value. Using an autoincrement field avoids this issue. It is also much faster and easier to use.

    [Edit]

    User asked if there was no other way to do this. There is of course otherways to do this, this however is the correct one in nearly all cases. Even if you need more than one per table, it is likely the best way to do this in MySQL is to create a second table to get another autoincrement.

    That said I will propose two of the many alternatives. The crux of the problem is the need for an atomic counter, that is a counter that does not suffer from the race condition described above, and will always return the next unique id. One such source of an atomic counter is memcache. Memcache provides an atomic increment that could be used as the source for a unique id. Like all solutions this has it's downfalls, if your memcache goes down you will loose your state, and the counter will start over, thus not being so unique.

    Another approach is to use MySQL locks. This can either be a table lock where in you lock the table, select the max id increment, insert and unlock the table, this requires the use of transactions as well and so will further complicate what could be very simple code. You can alternatively use MySQLs arbitrary application level locking scheme.

    https://dev.mysql.com/doc/refman/5.0/en/internal-locking.html https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock

    The complexity of implementing such an alternative is sufficient, that if you did not already know what I just explained, you should stick with autoincrement fields, they are very easy to use and fairly bullet proof.

提交回复
热议问题