Hi!

Let’s say I have a questions system and the writers of questions always add at least one but maybe more clues for the question.

Would it be better design to have each question have its own table for clues, even though the vast majority of the time the questions only have 1 clue? (ie is it inefficient to create like a zillion tables for a database?) Or would it be better to have a “clues” table, where each clue stores which question ID the clue applies to? (ie are later queries linear in time based on the amount of clues in the table which would be bad?)

Thanks for your help! And I’d appreciate motivations for the answers too so I will understand better.

5 points

As others have said, you want a clues table.

The clues table needs a question_id column which is obviously a foreign key linking to the id column of the questions table.

Why?

  1. You will have only have one clues table, not hundreds
  2. You can fetch all the clues for any question really easily by just retrieving all clues from the clues table with a question_id matching the id of your question
  3. Other less important stuff, but you can do funky things like automatically delete clues when the associated question is deleted from the questions table (using ON DELETE CASCADE or your dbs equivilant).
permalink
report
reply
2 points

Thanks for taking the time to write this and educate me.

permalink
report
parent
reply

No worries. I’m happy to help

permalink
report
parent
reply
5 points

Definitely a clues table with an id to a question. The first idea doesn’t really make sense.

permalink
report
reply
1 point

Thanks for the help

permalink
report
parent
reply
3 points

The latter. Creating millions of dynamic tables for this use case is not what SQL databases are designed for.

If you create a foreign key relationship from the clues table (column questionID) to the question table (column ID), the database will even guard for you that each clue actually has a valid question associated with it. What’s more, if you setup cascading deletes 9n that foreign key relationship, you only need to delete a question row and the clues will automatically be deleted for you. As you can see, this type of relationship is best modeled this way. There are many more reasons why you should do this, but I’m hoping this gives a beginners overview.

permalink
report
reply
2 points

Thanks for sharing your knowledge

permalink
report
parent
reply
2 points
*

You can create one table for all the clues and then do a 1 to n relationship. You create a collumn for the question ID in the clue table. So one question can have more than one clue and each clue knows to which question it belongs.

permalink
report
reply
1 point

Thanks for helping

permalink
report
parent
reply
1 point

Unified clue table. It minimises repetition of data and will allow for generalisable queries later on (rather than having to rewrite queries for new questions).

I think trad database design says you should have these tables: Questions Clues Table that only links questions and clues table

Which also means you can reuse clues for different questions too

permalink
report
reply
1 point

Thanks for the help

permalink
report
parent
reply

For all your programming needs

!coding@lemmygrad.ml

Create post

A community to discuss programming and or related topics

Community stats

  • 2

    Monthly active users

  • 37

    Posts

  • 37

    Comments

Community moderators