删除最后一个模型后,将创建的下一个模型将被分配已删除的模型 pk,这意味着指向已删除模型的任何链接现在都将指向新模型。
那么我们应该使用 PK 还是创建 UUID? 例如用户配置文件,某些高级配置文件在删除后将指向一些全新的配置文件。
- 这个网站,stack-overflow 使用 PK 来提问,我想知道我写的内容是否也适用于这里
最佳答案
从最简单的意义上来说,数据库使用 PK 字段的计数器,并在添加新记录时递增该计数器。删除/更新操作不会修改计数器。所以数据库不可能两次使用相同的值。
编辑:哎呀,这似乎是 Sqlite 的一个功能。详细信息在这里:http://sqlite.org/autoinc.html
引用文档:
If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically. The usual algorithm is to give the newly created row a ROWID that is one larger than the largest ROWID in the table prior to the insert.
还有:
The normal ROWID selection algorithm described above will generate monotonically increasing unique ROWIDs as long as you never use the maximum ROWID value and you never delete the entry in the table with the largest ROWID.
关于mysql - pk 作为绝对 id 有效吗? ( Django ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9699811/