Reasons not to use an auto-incrementing number for a primary key

前端 未结 16 2583
南旧
南旧 2020-12-29 06:02

I\'m currently working on someone else\'s database where the primary keys are generated via a lookup table which contains a list of table names and the last primary key used

相关标签:
16条回答
  • 2020-12-29 06:39

    Another potential reason is that you deliberately want random keys. This can be desirable if, say, you don't want nosey browsers leafing through every item you have in the database, but it's not critical enough to warrant actual authentication security measures.

    0 讨论(0)
  • 2020-12-29 06:39

    A useful side benefit of using a GUID primary key instead of an auto-incrementing one is that you can assign the PK value for a new row on the client side (in fact you have to do this in a replication scenario), sparing you the hassle of retrieving the PK of the row you just added on the server.

    One of the downsides of a GUID PK is that joins on a GUID field are slower (unless this has changed recently). Another upside of using GUIDs is that it's fun to try and explain to a non-technical manager why a GUID collision is rather unlikely.

    0 讨论(0)
  • 2020-12-29 06:42

    It's useful for clients to be able to pre-allocate a whole bunch of IDs to do a bulk insert without having to then update their local objects with the inserted IDs. Then there's the whole replication issue, as mentioned by Galwegian.

    0 讨论(0)
  • 2020-12-29 06:42

    I would prefer to use a GUID for most of the scenarios in which the post's current method makes any sense to me (replication being a possible one). If replication was the issue, such a stored procedure would have to be aware of the other server which would have to be linked to ensure key uniqueness, which would make it very brittle and probably a poor way of doing this.
    One situation where I use integer primary keys that are NOT auto-incrementing identities is the case of rarely-changed lookup tables that enforce foreign key constraints, that will have a corresponding enum in the data-consuming application. In that scenario, I want to ensure the enum mapping will be correct between development and deployment, especially if there will be multiple prod servers.

    0 讨论(0)
  • 2020-12-29 06:42

    Galwegian's answer is not necessarily true.

    With MySQL you can set a key offset for each database instance. If you combine this with a large enough increment it will for fine. I'm sure other vendors would have some sort of similar settings.

    Lets say we have 2 databases we want to replicate. We can set it up in the following way.

    increment = 2
    db1 - offset = 1
    db2 - offset = 2
    

    This means that

    db1 will have keys 1, 3, 5, 7....

    db2 will have keys 2, 4, 6, 8....

    Therefore we will not have key clashes on inserts.

    0 讨论(0)
  • 2020-12-29 06:46

    One benefit is that it can allow the database/SQL to be more cross-platform. The SQL can be exactly the same on SQL Server, Oracle, etc...

    0 讨论(0)
提交回复
热议问题