0

I have a table storing words, correspondingly there is another table storing the explanations of the words. It it very common that a word might have many explanations. So the primary key of the explanation table absolutely cannot be merely the word id, but the both the word id and the explanation, composite key, can decide a unique explanation record.

The explanation table can take the composite key as its primary key, but is it always like this in practice? Personally I doubt, as I have seen many examples from my real experience that in such case like the explanation table, usually a new id field will be added as the primary key.

Question: I would like to know which solution is better? Why I always see an extra ID is used as a primary key instead of a composite key?

Rui
  • 3,454
  • 6
  • 37
  • 70
  • If the explanations of the words is super long, the composite key will be even longer. Then the DB will index it... Another point is if you have to write code to reference that index, again you will have to include the entire explanations in the code just to reference an object. As for a general answer, this has been debated many times here and on the web. See https://stackoverflow.com/questions/26078535/composite-primary-keys-is-it-good-or-bad – Nic3500 Jun 28 '19 at 20:30
  • In the Relational Model, we talk about 'structured data'. Tables, columns, keys and Foreign Keys capture that structure. Designing them comes after you understand the structure. If your 'explanations' are just text of arbitrary length, you don't have much structure. I don't see why you want to use a Relational database. – AntC Jun 28 '19 at 21:58
  • (Obviously--) This is a faq. Before considering posting please always google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names; read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. PS I just now merely picked the first hit googling your title with 'site:stackoverflow.com before:2010'. This is a question unfortunately asked many many times. – philipxy Jun 28 '19 at 22:03
  • 1
    Possible duplicate of [Composite primary keys versus unique object ID field](https://stackoverflow.com/questions/159087/composite-primary-keys-versus-unique-object-id-field) – philipxy Jun 28 '19 at 22:06

0 Answers0