What's the best way to store the days of the week an event takes place on in a relational database?

前端 未结 6 1503
独厮守ぢ
独厮守ぢ 2020-12-23 11:47

We\'re writing a records management product for schools and one of the requirements is the ability to manage course schedules. I haven\'t looked at the code for how we deal

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 11:53

    I would avoid the string option for the sense of purity: it adds an extra layer of encoding/decoding that you do not need. It may also mess you up in the case of internationalization.

    Since the number of days in a week is 7, I would keep seven columns, perhaps boolean. This will also facilitate subsequent queries. This will also be useful if the tool is ever used in countries where the workweek starts on different days.

    I would avoid the lookup because that would be over-normalization. Unless your set of lookup items is not obvious or could possibly change, it's overkill. In the case of days-of-the-week (unlike US states, for example), I would sleep soundly with the fixed set.

    Considering the data domain, I don't think that a bitfield would achieve any significant space savings for you and would just make your code more complex.

    Finally, a word of warning about the domain: a lot of schools do weird things with their schedules where they "swap days" to balance out an equal number of weekdays of each type over the semester despite holidays. I am not clear about your system, but perhaps the best approach would be to store a table of the actual dates in which the course is expected to take place. This way, if there are two Tuesdays in a week, the teacher can get paid for showing up twice, and the teacher for the Thursday that was canceled will not pay.

提交回复
热议问题