Is it OK not to use a Primary Key When I don't Need one

前端 未结 12 1315
南笙
南笙 2020-12-17 23:06

If I don\'t need a primary key should I not add one to the database?

相关标签:
12条回答
  • 2020-12-17 23:27

    No, unless you can find an example of, "This database would work so much better if table_x didn't have a primary key."

    You can make an arguement to never use a primary key, if performance, data integrity, and normalization are not required. Security and backup/restore capabilities may not be needed, but eventually, you put on your big-boy pants and join the real world of database implementation.

    0 讨论(0)
  • 2020-12-17 23:27

    A primary key will always help with query performance. So if you ever need to query using the "key" to a "foreign key", or used as lookup then yes, craete a foreign key.

    0 讨论(0)
  • 2020-12-17 23:27

    Well...

    Each table in a relational DB needs a primary key. As already noted, a primary key is data that identies a record uniquely...

    You might get away with not having an "ID" field, if you have a N-M table that joins 2 different tables, but you can uniquely identifiy the record by the values from both columns you join. (Composite primary key)

    Having a table without an primary key is against the first normal form, and has nothing to do in a relational DB

    0 讨论(0)
  • 2020-12-17 23:29

    Yes, a table should ALWAYS have a primary key... unless you don't need to uniquely identify the records in it. (I like to make absolute statements and immediately contradict them)

    When would you not need to uniquely identify the records in a table? Almost never. I have done this before though for things like audit log tables. Data that won't be updated or deleted, and wont be constrained in any way. Essentially structured logging.

    0 讨论(0)
  • 2020-12-17 23:32

    If you don't need a primary key then don't use one. I usually have the need for primary keys, so I usually use them. If you have related tables you probably want primary and foreign keys.

    0 讨论(0)
  • 2020-12-17 23:32

    Yes, but only in the same sense that it's okay not to use a seatbelt if you're not planning to be in an accident. That is, it's a small price to pay for a big benefit when you need it, and even if you think you don't need it odds are you will in the future. The difference is you're a lot more likely to need a primary key than to get in a car accident.

    You should also know that some database systems create a primary key for you if you don't, so you're not saving that much in terms of what's going on in the engine.

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