I’m just wondering, as a hypothetical example, what would be the best way to layout a table for the following scenario:
Let’s say I’m writing an app for tracking student attendance. At the beginning of every year, I want to add all the students in (I’ll do this manually – now, should a student ID be assigned to each one here? Let’s call that table Students). Now, each day, I’m going to display all the Students in table Students and will allow the user to choose attendance.
So, how should I lay my table out? (If you don’t understand what I mean, I mean what data should be entered in each column, row…) For example, maybe have a Students table with Student IDs and for each student every day create a new row in the Attendance table with Column 1: Student ID, Column 2: Date, Column 3: Status (present/absent). However, that doesn’t seem to be very efficient. What do you think?
UPDATE: From all these first answers, it seems that one student is in each row in the student-attendance table (where he/she is designated as present/absent), but what if I were to include more than one student id per row, say for all students absent on some day? Would that be better or worse (probably it’s ambiguous)? Actually, I’m starting to think efficiency would be decreased because the only actions this move even helps can be easily accomplished already. Hmm…
STUDENTStableSTUDENT_ID, pkFIRST_NAMELAST_NAMESTUDENT_ATTENDANCEtableSTUDENT_ID, pk, fkABSENT_DATE, pkThere’s not need for an
IS_ABSENTcolumn – having a date indicates that both that the student was absent, and on what date. There’s likely to be less days absent than attended, so only store the absent dates.Making the primary key to be composite of the two columns ensures that you won’t have duplicates.
Then you are either storing the additional student_ids as either a comma separated list in a single column, or additional columns for each additional student_id. Additional columns for each student_id would never work – you’d be adding a column for every new student, every year. Concatenating a list of student_ids is more realistic, but will be a pain to pull out details if you want to report on a specific student or group of students. Due to character limits, it runs the risk of not being able to store every student_id that could be absent with a single column.
I recommend using the
STUDENT_ATTENDANCEtable I suggested.