last day (15 days later) » 

10:13
1
Q: Database Design for lookup tables

user75ponicI have a transaction table called questionnaire and a question lookup table called question_codes. questionnaire qst_id - number (primary key) question_type - number (foreign key) question_category - number (foreign key) question_codes code_id - number (primary key) code_name - String co...

You can add a new foreign key between questionnaire and question_codes.
@RuudvandeBeeten So no need to have a new table for product_type?
"Both tables are linked with qst_id" What is that supposed to mean? I don't see a qst_id in the 2nd table. Is there any foreign key between these 2 existing tables?
@ypercube Yes both tables are linked, e.g. question_type corresponding code_id will be stored in questionnaire table. Same the case with question_category.
Can you show us the CREATE TABLE statements (including the foreign key definitions) and some sample data?
10:13
@ypercube I have updated my question with table description and sample records. Thanks
Do you mean you have multiple foreign key columns and one and the same table & column that those FKs are referencing?
From your SQL Fiddle, it appears the answer to my question is "yes". That's a terrible design. You should have distinct tables for distinct entities. There should be a Category table, or perhaps even two: question_category, product_category, same for types and other things. You shouldn't mix all those in one table.
@AndriyM yes multiple foreign key to question_codes to get the code_id and code_desc.
@AndriyM question_codes is only a lookup table so still do I need to have multiple tables like one for question_type and another for question_category?
@Polppan: As Andriy assumes and your sample shows, both question_type and question_category could have foreign key constraints referencing the same table: question_code (code_id) (Check the fiddle if that's a valid query.) This is usually not good because the look up data (which presumably are different for "type" and "category" are mixed up.
You could have rows inserted for example with insert into questionnaire values (3,5,1); which would do not make any sense. See Fiddle-2
@ypercube Query is valid. So it is best practice to have two lookup table for question_type and question_category even though it contains only lookup information? For my unserstanding, why and what is the reasoning for this? Thanks
@Polppan: Yes, separate tables for different things. The example by Ypercube shows that with your design you would be allowed to insert references into wrong columns. For instance, you would be able to insert a type into the category column and the other way round. How did you come up with this design in the first place? Is this an exercise, a working project, or something else?
10:13
It is a start of a prototype @AndriyM
Oh I see. Perhaps, "terrible" was a bit too harsh on my side and if the supporting team is happy with the present idea, good for you. It's just that I would hate to be on that team. :) My issue with this design is mainly about the referential integrity. Although the server would prohibit you from inserting a non-existent reference into any of the reference columns, it would allow you to insert one pointing to an entirely different thing than the column is supposed to hold, just as I said above and as the already mentioned example shows.
So, again, fix the design (by allocating separate tables for different entities), and perhaps it will then be much easier for you to answer your original question yourself.
I agree with @AndriyM. It would be a more self explaining database design when you create separate tables.

last day (15 days later) »