database - SQL design for similar tables with few column differences -
i working on application deals questions , answers. answers can of different types ranging int, text, image, option, etc.. , wonder if practice separate different tables below?
question questionanswer -------- ------------- int id int questionid int typeid int answerid textanswer imageanswer optionanswer intanswer ---------- ----------- ------------ --------- int answerid int answerid int answerid int answerid nvarchar value binary value int value int value int optiontypeid
i thought aggregating 1 single answer table filled nullable fields , afraid might introduce bugs if not careful. idea or design practice in separating different types of answers? or perhaps there better approach ? thanks!
the question of 1 table (for answers), 4 tables (for each type of answer), or 5 tables (one answers, 1 specific each type) depends on 2 factors.
the important is: how data used? second is: database using?
let me dispense second part first. databases (notably postgres) have built-in support table inheritance. can read [documentation]; 1; want.
for other databases how choose? well, have table answerid
in it. suggests want single entity, automatically generated primary key.
so, store information in table or not? 1 argument of putting columns in single table simplicity: data in 1 place. can enforce null
-ness of values using check
constraints. 1 downside adding new type requires modifying table.
another alternative have each of specific tables. can use answerid
primary key, having foreign key answers
. makes each add new types. however, can cumbersome ensure answer has 1 type. joins bit more complicated.
under circumstances, tend go 1 table , proliferation of columns, if knew expected range of types in advance.
Comments
Post a Comment