3

My database is going to store words, so the Word table is the most important. The Rhyme table lists which words a particular word perfectly rhymes with. It consists of three fields:

RhymeID - primary key.
WordID - foreign key linking to Word table
Rhyme - a non key integer which refers to a different record in the Word table.

E.g. 'cat' has WordID 009, 'bat' has WordID 014 and 'fat' has WordID 035, two records in the Rhyme table might look like this

RhymeId - WordId - Rhyme
001 - 009 (cat) - 014 (bat)
002 - 009 (cat) - 035 (fat)

The question is, should I make WordId a primary key to form a composite key with RhymeId? Do I need it to uniquely identify each record? I have no practical experience with databases, this one is still in the planning stage.

4
  • 3
    If I'm reading your schema correctly, it is different than what I would think to make. I would think you would have tables Word(word_id, word), Ryhme(rhyme_id, sound), and RhymeWord(word_id, rhyme_id). The RhymeWord key can then be a composite of the word_id and rhyme_id. The problem with how you set it up is how do you know that bat rhymes with fat without entering a bunch of "duplicate" data or writing the bat-fat query differently than the cat-bat query.
    – WuHoUnited
    Commented Jul 1, 2014 at 16:56
  • See dba.stackexchange.com/q/8334/13333 Commented Jul 1, 2014 at 17:02
  • Probably a better question for the DBA SE site.
    – Bernard
    Commented Jul 1, 2014 at 22:15
  • As @WuHoUnited points out you will need extra rows to link "bat" to "fat" but worse you will then need to add a second row with WordId and Rhyme reversed, and so on for every pair you identify. This will get infeasibly large very quickly. Commented Jul 2, 2014 at 11:54

2 Answers 2

5

First make it clear than Rhyme is also a FK

You should do either of the following:

  • Make RhymeId the PK and create an composite unique index with WordID and Rhyme so no combination of two words appears twice.
  • Get rid of RhymeId and make a composite PK with WordID and Rhyme

I preffer the second option because you need to keep only one index instead of two.

1
  • 1
    A many-to-many relationship table is the best place for a composite key.
    – JeffO
    Commented Jul 2, 2014 at 10:06
-1

Two concepts from the DB design world apply here. They are "natural keys" and "surrogate keys". A natural key is that combination of columns, the values of which can be used to distinguish the row which contains them from all other rows in the table i.e. that combination of values is unique in the table. Importantly these columns appear "naturally" in the user's understanding of the problem. A surrogate key is a value invented to stand in for the natural key, usually for technical or performance reasons. Once you create a surrogate key it is normal practice to use that as the foreign key in all other tables.

In your example "cat" wold be the natural key of the Word table and "009" is the surrogate key for that natural key.

So, for your Rhyme table the natural key would be WordID and Rhyme (yes, even though they are both themselves surrogate keys from another table) and RhymeID is the surrogate key.

If other tables will have a foreign key pointing back to Rhyme then I would definietly suggest giving it a surrogate key. If it won't then the surrogate is 50% bloat for zero value in what is likely to be an exceedingly large table; I would suggest omitting it.

2
  • Why the downvote? Answer seems fine to me
    – Lou
    Commented Jul 2, 2014 at 17:32
  • Yes, an explanation would be nice, please. Commented Jul 5, 2014 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.