How to store opening weekdays in a database

天大地大妈咪最大 提交于 2021-02-07 07:19:17

问题


I have a group of checkboxes where the user selects some of the weekdays (the opening days of a store). How can I save the selected days? Should I save something like 0111111 (zero means closed on sunday) on the same field and split the result when reading the data? Or create a field for each day and store 0 or 1 on each (weird)?


回答1:


The first approach saves space, the second improves performance while retrieving data, especially if you need to filter by individual weekday. It also makes the queries that manipulate weekdays a way clearer. You have to decide what is more important to you based on the application's logic.

In general, I'd use field-per-day solution.




回答2:


It totally depends on what you are going to do with that data once you have it. By and large, if these seven factoids (weekdays that the store is opn) are one fact, will be treated as one fact, and you'll never need to parse out and individually process one of the "factoids" within the string, then it can and possibly should be stored as one piece of data.

But, if you'll have to consider this infomation as seven distinct pieces of data, you'll be better off recording it as seven different pieces of data (i.e. seven different columns).

Again, there's a whole lot of "it depends" behind this. Another way to think of it is, look ahead to how you will be using the data, and try and structure your storage to simplify your processes (i.e. do you want to always be parsing string "1010101" for the element(s) you want?)



来源:https://stackoverflow.com/questions/2730645/how-to-store-opening-weekdays-in-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!