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

前端 未结 6 1373
独厮守ぢ
独厮守ぢ 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 12:08

    A possible #4: Why does it need to be a single column? You could add 7 bit columns for each day of the week to the table. Writing SQL against it is simple, just test for a 1 in the column of your choice. And the app code reading from the database just hides this in a switch. I realize that this is not normal form and I usually spend quite a bit of time trying to undo such designs from previous programmers, but I somewhat doubt that we're going to add an eighth day to the week any time soon.

    To comment on the other solutions, I would probably groan if I encountered the lookup table. My first inclination as well was the bit field with a few custom database functions to help you write natural queries against that field easily.

    I'll be curious to read some of the other suggestions that people come up with.

    Edit: I should add that #3 and the suggestion above are easier to add indexes to. I'm not sure how one could write a SQL query like "get me all classes on Thursday" for the #1 or #2 queries that wouldn't result in a table scan. But I may just be dim tonight.

提交回复
热议问题