What are the down sides of using a composite/compound primary key?

后端 未结 8 2201
说谎
说谎 2020-12-14 20:30

What are the down sides of using a composite/compound primary key?

相关标签:
8条回答
  • 2020-12-14 21:01

    I would recommend a generated primary key in those cases with a unique not null constraint on the natural composite key.

    If you use the natural key as primary then you will most likely have to reference both values in foreign key references to make sure you are identifying the correct record.

    0 讨论(0)
  • 2020-12-14 21:01

    The main downside of using a compound primary key, is that you will confuse the hell out of typical ORM code generators.

    0 讨论(0)
  • 2020-12-14 21:07

    Need more specificity.

    Taken too far, it can overcomplicate Inserts (Every key MUST exist) and documentation and your joined reads could be suspect if incomplete.

    Sometimes it can indicate a flawed data model (is a composite key REALLY what's described by the data?)

    I don't believe there is a performance cost...it just can go really wrong really easily.

    0 讨论(0)
  • 2020-12-14 21:08

    Take the example of a table with two candidate keys: one simple (single-column) and one compound (multi-column). Your question in that context seems to be, "What disadvantage may I suffer if I choose to promote one key to be 'primary' and I choose the compound key?"

    First, consider whether you actually need to promote a key at all: "the very existence of the PRIMARY KEY in SQL seems to be an historical accident of some kind. According to author Chris Date the earliest incarnations of SQL didn't have any key constraints and PRIMARY KEY was only later affffded to the SQL standards. The designers of the standard obviously took the term from E.F.Codd who invented it, even though Codd's original notion had been abandoned by that time! (Codd originally proposed that foreign keys must only reference one key - the primary key - but that idea was forgotten and ignored because it was widely recognised as a pointless limitation)." [source: David Portas' Blog: Down with Primary Keys?

    Second, what criteria would you apply to choose which key in a table should be 'primary'? In SQL, the choice of key PRIMARY KEY is arbitrary and product specific. In ACE/Jet (a.k.a. MS Access) the two main and often competing factors is whether you want to use PRIMARY KEY to favour clustering on disk or whether you want the columns comprising the key to appears as bold in the 'Relationships' picture in the MS Access user interface; I'm in the minority by thinking that index strategy trumps pretty picture :) In SQL Server, you can specify the clustered index independently of the PRIMARY KEY and there seems to be no product-specific advantage afforded. The only remaining advantage seems to be the fact you can omit the columns of the PRIMARY KEY when creating a foreign key in SQL DDL, being a SQL-92 Standard behaviour and anyhow doesn't seem such a big deal to me (perhaps another one of the things they added to the Standard because it was a feature already widespread in SQL products?) So, it's not a case of looking for drawbacks, rather, you should be looking to see what advantage, if any, your SQL product gives the PRIMARY KEY. Put another way, the only drawback to choosing the wrong key is that you may be missing out on a given advantage.

    Third, are you rather alluding to using an artificial/synthetic/surrogate key to implement in your physical model a candidate key from your logical model because you are concerned there will be performance penalties if you use the natural key in foreign keys and table joins? That's an entirely different question and largely depends on your 'religious' stance on the issue of natural keys in SQL.

    0 讨论(0)
  • 2020-12-14 21:09
    1. when you se it on a diagram are less readable
    2. when you use it on a query join are less readable
    3. when you use it on a foregein key you have to add a check constraint about all the attribute have to be null or not null (if only one is null the key is not checked)
    4. usualy need more storage when use it as foreign key
    5. some tool doesn't manage composite key
    0 讨论(0)
  • 2020-12-14 21:14

    There's nothing wrong with having a compound key per se, but a primary key should ideally be as small as possible (in terms of number of bytes required). If the primary key is long then this will cause non-clustered indexes to be bloated.

    Bear in mind that the order of the columns in the primary key is important. The first column should be as selective as possible i.e. as 'unique' as possible. Searches on the first column will be able to seek, but searches just on the second column will have to scan, unless there is also a non-clustered index on the second column.

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