Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
表名 = 'settings'
然后你可以像这样查询:
这个选项很好而且很简单,它可以很好地处理 10 或 10,000 个设置。使用另一个选项,您必须添加一个新列,这完全是毫无意义的浪费时间。
编辑
在您发表第一条评论后,您可以选择多个值,如下所示:
:-)
Table name = 'settings'
Then you can query like this:
This option is nice and easy and it will work well with 10, or 10,000 settings. With the other option you'll have to add a new column, which would be a completely pointless waste of time.
Edit
After your 1st comment you could choose multiple values like this:
:-)
将第一个选项(
Setting
、Value
)视为列。但也要考虑添加额外的元列,例如Description
(如果您有很多不明确的设置,将会派上用场)、PreviousValue
、LastUpdated、<代码>更新者等。
Consider the first option (
Setting
,Value
) as columns. But also consider adding additional, meta columns as well, such asDescription
(would come in handy if you have alot of ambiguous settings),PreviousValue
,LastUpdated
,UpdatedBy
, etc.您的第一个示例,名称-值对或 EAV,提供了更大的灵活性。
查看有关数据库中 EAV 建模的 wiki 页面。
Your first example, name-value pairs or EAV, allows for a good deal more flexibility.
Check out the wiki page about EAV modelling in databases.
事实上,这一直是我的挑战。每个都有自己的优点和缺点,尽管我有时在项目中使用这两个示例,这取决于它的使用位置。
第一个更多的是当用户要将网站的某些设置保存在数据库中的密钥下时,它是动态的,这意味着可以随时添加某些内容。即使您作为开发人员也想每时每刻添加新的东西。因为通过添加记录作为新键,您不需要重建数据库和模型的结构,并且节省了时间。在此结构中,您无法为每个数据指定特定类型。
示例: http://sqlfiddle.com/#!9/1ab8aa/1/1< /a>
但在第二种情况下,添加新键取决于更改数据库结构和更改模型。因此,当您拥有不会每次都改变的固定且特定的键时,更多的是这样的情况。然而,例如,如果要针对特定语言或特定用户组对设置进行分类,则此方法仍然使任务变得更加容易,因为每条记录都可以针对特定用户或特定语言。在这种情况下,查询此结构比以前的方法更容易。在此结构中,您可以按字符串、数字、日期等设置和验证数据。
示例: http://sqlfiddle.com/#!9/b99d52/1/1< /a>
Actually, this has always been my challenge. Each has its own advantages and disadvantages, although I have sometimes used both examples in projects and it depends on where it is used.
The first is more for when the user is going to save certain settings of the site under the key in the database and it is dynamic, meaning that something may be added at any time. Even you as a developer want to add something new every moment. Because by adding a record as a new key, you do not need to rebuild the structure of the database and models, and it saves time. In this structure you can not specify a specific type for each data.
Sample: http://sqlfiddle.com/#!9/1ab8aa/1/1
But in the second, adding a new key depends on changing the structure of the database and changing the models. So more for when you have fixed and specific keys that are not going to change every time. However, if, for example, the settings are to be categorized for a specific language or a specific user group, this method still makes the task much easier because each record can be for a specific user or a specific language. In such cases, querying on this structure is easier than the previous method. In this structure, you can set and validate data by string, number, date, etc.
Sample: http://sqlfiddle.com/#!9/b99d52/1/1
像往常一样,这取决于。解决方案1更简单。 Sol #2 可以轻松与 ORM 集成,但可能会遇到数据库行大小限制。
Google for OTLT(如“一个真实查找表问题”)
您有多少设置(几个?几十个?几百个?)您多久需要一次?
As usual, it depends. Solution 1 is simpler. Sol #2 easily integrates with ORMs, but may hit DB row size limitations.
Google for OTLT (as in One True Lookup Table problem)
How much settings do you have(few? dozens? hundreds?) How often will you need them?