简体   繁体   English

SQL设计问题-是否有很多表?

[英]Sql design question - many tables or not?

15 ECTS credits worth of database design down the bin.. I really can't come up with the best design solution for my problem. 15个ECTS积分值得进行数据库设计。.我真的无法为我的问题提供最佳的设计解决方案。

Which is this: Basically I'm making a tool that gathers a lot of information concerning the user. 就是这样:基本上,我正在制作一个工具,该工具收集了大量有关用户的信息。 At the most the user would fill in 50 fields of data, ranging from simple checkboxes to text input. 用户最多只能填写50个数据字段,范围从简单的复选框到文本输入。 I'm designing the db right now (with mySql) and can't decide whether or not to use a single User table with all of those fields, or to have a table for each category of input. 我现在正在设计数据库(使用mySql),并且无法决定是否将单个User表与所有这些字段一起使用,或为每个输入类别使用一个表。

One example would be "type of payment". 一个例子是“付款类型”。 This one has three options and if I went with the "table" way I would add a table paymentType and give it binary fields for each payment type. 这有三个选项,如果我采用“表格”方式,我将添加一个表paymentType,并为每种付款类型提供二进制字段。 Then I would need and id table to identify which paymentType the user has chosen whereas if I use a single user table, the data would already be there. 然后,我将需要一个id表来标识用户选择了哪个paymentType,而如果使用单个用户表,则数据已经存在。

The site will probably see a lot of users (tv, internet and radio marketing) so I'm concerned which alternative would be the best. 该网站可能会看到很多用户(电视,互联网和广播营销),因此我担心哪种选择最好。

I'll be happy to provide more details if you need more to base a decision. 如果您需要更多决策依据,我们将乐意提供更多详细信息。

Thanks for reading. 谢谢阅读。

Read this article " Database Normalization Basics ", and come back here if you still have questions. 阅读本文“ 数据库规范化基础知识 ”,如果仍有疑问,请返回此处。 It should help a lot. 它应该有很大帮助。

The most fundamental idea behind these decisions, as you will see in this article, is that each table should represent one and only one "thing", and each field should relate directly and only to that thing. 正如您将在本文中看到的那样,这些决定背后的最基本思想是,每个表应仅代表一个“事物”,并且每个字段都应直接且仅与该事物相关。

In your payment types example, it probably makes sense to break it out into a separate table if you anticipate the need to store additional information about each payment type. 在您的“付款方式”示例中,如果您预计需要存储有关每种付款方式的其他信息,则可以将其分解为一个单独的表。

If you are building a form with variable inputs, I wouldn't recommend building it as one table. 如果您要构建具有可变输入的表单,我不建议将其构建为一个表。 This is inflexible and dirty. 这是不灵活和肮脏的。

Normalization is the key, though if you end up with a key/value setup, or effectively a scalar type implementation across many tables and can't cache: 归一化是关键,但是如果最终要进行键/值设置,或者实际上是跨多个表的标量类型实现,并且不能缓存:

a) the form definition from table data and a)来自表数据的表单定义,以及
b) the joined result of storage (either a caching view or otherwise) b)存储的合并结果(缓存视图或其他)
c) or don't build in proper sharding c)或不建立适当的分片

Then you may hit a performance boundary. 然后,您可能会遇到性能边界。

In this KVP setup, you might want to look at something like CouchDB or a less table-driven storage format. 在此KVP设置中,您可能需要查看类似CouchDB或表驱动较少的存储格式。

You may also want to look at trickier setups such as serialized object storage and cache-tables if your internal data is heavily relative to other data already in the database 如果您的内部数据相对于数据库中已经存在的其他数据而言,您可能还希望查看更棘手的设置,例如序列化对象存储和缓存表

Create your "Type of Payment" table; 创建您的“付款类型”表; there's no real question there. 那里没有真正的问题。 That's proper normalization and the power behind using relational databases. 那就是适当的规范化,以及使用关系数据库的强大功能。 One of the many reasons to do so is the ability to update a Type of Payment record and not have to touch the related data in your users table. 这样做的众多原因之一是能够更新“付款类型”记录,而不必触摸用户表中的相关数据。 Your join between the two tables will allow your app to see the updated type of payment info by changing it in just the 1 place. 您可以通过在两个表之间进行联接来使您的应用通过仅在1个位置进行更改来查看更新的付款信息类型。

Regarding your other fields, they may not be as clear cut. 关于您的其他领域,可能没有那么明确。 The question to ask yourself about each field is "does this field relate only to a user or does it have meaning and possible use in its own right?". 关于每个字段要问自己的问题是“该字段仅与用户有关,还是它本身具有含义和可能的用途?”。 If you can never imagine a field having meaning outside of the context of a user you're safe leaving it as a field on the user table, otherwise do the primary key-foreign key relationship and put the information in its own table. 如果您永远无法想象某个字段在用户上下文之外具有意义,那么您可以放心地将其保留为用户表中的字段,否则可以进行主键-外键关系并将信息放在其自己的表中。

50 columns is a lot. 50列很多。 Have you considered a table that stores values like a property sheet? 您是否考虑过像属性表那样存储值的表? This would only be useful if you didn't need to regularly query the values it contains. 仅在不需要定期查询其中包含的值时,这才有用。

INSERT INTO UserProperty(UserID, Name, Value)
     VALUES(1, 'PaymentType', 'Visa')

INSERT INTO UserProperty(UserID, Name, Value)
     VALUES(1, 'TrafficSource', 'TV')

I think I figured out a great way of solving this. 我想我找到了解决此问题的好方法。 Thanks to a friend of mine for suggesting this! 感谢我的一个朋友的建议!

I have three tables, Field {IdField, FieldName, FieldType}, FieldInput {IdInput, IdField, IdUser} and User { IdUser, UserName... etc } 我有三个表,字段{IdField,FieldName,FieldType},FieldInput {IdInput,IdField,IdUser}和用户{IdUser,UserName ...等}

This way it becomes very easy to see what a user has answered, the solution is somewhat scalable and it provides a good overview. 这样,很容易看到用户的回答,该解决方案具有一定的可扩展性,并且提供了很好的概述。 I will constrain the alternatives in another layer, farther away from the db. 我将替代方案约束在距离数据库更远的另一层。 I believe it's a tradeoff worth doing. 我认为这是一个值得权衡的事情。

Any suggestions or critics to this solution? 对这个解决方案有什么建议或批评吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM