SQL database - when to use a separate table vs a column of an existing one? -
so pretty new sql , databases in general(only designed simple 1 minimal site), , i'm trying work out best way design models heavily db driven site. take example, user uploaded gallery. have gallery table sensible columns date uploaded, name, etc., , galleries can belong 1 category, of there not many (at 6). should have category column of gallery table? or have separate table categories , have many 1 relationship between category , gallery tables? things in views sorting galleries in category date uploaded, there performance/convenience difference between these? having category column of gallery table seems easier deal me, i'm not sure best practice. thanks.
first of all, need understand conceptual difference.
as rule of thumb, safe consider following equivalence:
table ~~~ entity
column ~~~ attribute
so, when need add new piece of data, in relation entity (an existing table), question can ask is:
is piece of data attribute of entity?
if answer yes, need new column.
for instance, have table describing student entity:
table student: [pk] id [fk] idclass name surname
say want add gpa of each student. attribute of student, can add gpa column in student table.
if wish define department each student, see department is not attribute of student. department entity, exists , has own attributes outside scope of student.
therefore, attribute of student affiliation department, not department itself.
so, create new department table, , use department.id
fk in students table.
i hope helps. cheers.
Comments
Post a Comment