存储下拉值,什么是好的方法 [英] storing drop-down values, what is a good approach

查看:82
本文介绍了存储下拉值,什么是好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有一个网站,其中包含从后端数据库填充的多个下拉列表。目标是将这些值存储在数据库中,并在呈现表单时检索它们。

Imagine you have a website with several drop-downs that are populated from the back-end database. The objective is to store these values in the database and retrieve them whenever you render the form.

我已经看到使用了两种方法:

I have seen two approaches used:

1)每个列表类型的一个表:

1) One table per list type:

profession_type
|id|value|

hobby_type
|id|value|

2)所有查找值的一个表:

2) One table for all look-up values:

|id|type           |value|
|0 |profession_type|value|
|1 |profession_type|value|
|2 |profession_type|value|
|3 |hobby_type     |value|
|4 |hobby_type     |value|
|5 |hobby_type     |value|

其中任何一个都有客观优势。 #2似乎更为通用(您可以按照类型从表格中选择以填充特定的下拉列表),但是如果您使用#1,则表将趋向于显着更大。另外,如果你使用#2,所有的外键都指向同一个巨型桌子。这似乎不是一件大事,但在我看来,这种方法看起来更加复杂。

Are there objective advantages to either of it. #2 seems to be more generic (you do a select from table by type to populate a specific drop-down), but the table will tend to be significantly larger than if you were to use #1. Also, if you use #2, all of the foreign keys are pointing to the same giant table. It does not seem like a big deal, but in my eyes this approach looks more convoluted.

推荐答案

2。您可以检索并缓存所有信息,即使您添加新的类型,也不需要更改数据库或检索逻辑。

I'd go with #2. You can retrieve and cache all info and even if you add new types you needn't change your database or your retrieval logic.

如果您要添加和更改表,您需要以触​​摸您的数据库模式,以便将来进行所有更改,并更改新表的检索代码。

If you are adding and changing tables you need to touch your database schema for all changes in the future and change retrieval code for the new tables.

此外,您对表大小的疑虑是无可置疑的。除非你有数百万行,否则会很好。我想象了几百或几千行?数据库可以轻松处理。

Furthermore your concerns for the table size are PROBABLY unfounded. Unless you have millions of rows it'll be fine. I'm imagining some hundreds OR thousands of rows? Databases can easily handle that.

这篇关于存储下拉值,什么是好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆