Auto Increment after delete in MySQL

后端 未结 17 2129
醉酒成梦
醉酒成梦 2020-11-22 09:50

I have a MySQL table with a primary key field that has AUTO_INCREMENT on. After reading other posts on here I\'ve noticed people with the same problem and with varied answer

17条回答
  •  再見小時候
    2020-11-22 10:29

    What you are trying to do is very dangerous. Think about this carefully. There is a very good reason for the default behaviour of auto increment.

    Consider this:

    A record is deleted in one table that has a relationship with another table. The corresponding record in the second table cannot be deleted for auditing reasons. This record becomes orphaned from the first table. If a new record is inserted into the first table, and a sequential primary key is used, this record is now linked to the orphan. Obviously, this is bad. By using an auto incremented PK, an id that has never been used before is always guaranteed. This means that orphans remain orphans, which is correct.

提交回复
热议问题