我应该如何使用 MySQL 构建我的设置表?

发布于 2024-08-25 19:00:35 字数 1699 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏爱自由 2024-09-01 19:00:35

表名 = 'settings'

name  | varchar <-- primary key
value | varchar

然后你可以像这样查询:

SELECT * FROM settings WHERE name = 'default_printer';

这个选项很好而且很简单,它可以很好地处理 10 或 10,000 个设置。使用另一个选项,您必须添加一个新列,这完全是毫无意义的浪费时间。

编辑

在您发表第一条评论后,您可以选择多个值,如下所示:

SELECT * FROM settings WHERE name IN ('default_printer','default_page_size');

:-)

Table name = 'settings'

name  | varchar <-- primary key
value | varchar

Then you can query like this:

SELECT * FROM settings WHERE name = 'default_printer';

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:

SELECT * FROM settings WHERE name IN ('default_printer','default_page_size');

:-)

深海夜未眠 2024-09-01 19:00:35

将第一个选项(SettingValue)视为列。但也要考虑添加额外的元列,例如 Description(如果您有很多不明确的设置,将会派上用场)、PreviousValueLastUpdated、<代码>更新者等。

Consider the first option (Setting, Value) as columns. But also consider adding additional, meta columns as well, such as Description (would come in handy if you have alot of ambiguous settings), PreviousValue, LastUpdated, UpdatedBy, etc.

初见终念 2024-09-01 19:00:35

您的第一个示例,名称-值对或 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.

在梵高的星空下 2024-09-01 19:00:35

事实上,这一直是我的挑战。每个都有自己的优点和缺点,尽管我有时在项目中使用这两个示例,这取决于它的使用位置。

第一个更多的是当用户要将网站的某些设置保存在数据库中的密钥下时,它是动态的,这意味着可以随时添加某些内容。即使您作为开发人员也想每时每刻添加新的东西。因为通过添加记录作为新键,您不需要重建数据库和模型的结构,并且节省了时间。在此结构中,您无法为每个数据指定特定类型。
示例: 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

鹿港巷口少年归 2024-09-01 19:00:35

像往常一样,这取决于。解决方案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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文