SQL 2008:对每种数据类型使用单独的表来返回单行
我想这次我应该灵活一点,让用户决定希望在数据库中存储哪些联系信息。理论上,它看起来像一行,包含以下内容:姓名、地址、邮政编码、类别 X、列表项 A。
示例
FieldType 表定义用户可用的数据类型:
FieldTypeID, FieldTypeName, TableName
1,"Integer","tblContactInt"
2,"String50","tblContactStr50"
...
用户在 FieldDefinition 表中定义其字段:
FieldDefinitionID, FieldTypeID, FieldDefinitionName
11,2,"Name"
12,2,"Address"
13,1,"Age"
最后,我们根据其数据类型将实际联系人数据存储在单独的表中。 主表,仅包含ContactID
tblContact:
ContactID
21
22
tblContactStr50:
ContactStr50ID,ContactID,FieldDefinitionID,ContactStr50Value
31,21,11,"Person A"
32,21,12,"Address of person A"
33,22,11,"Person B"
tblContactInt:
ContactIntID,ContactID,FieldDefinitionID,ContactIntValue
41,22,13,27
问题:是否可以返回内容这些表分成两行,如下所示:
ContactID,Name,Address,Age
21,"Person A","Address of person A",NULL
22,"Person B",NULL,27
我研究了使用 COALESCE 和 Temp 表,想知道这是否可能。即使是这样:也许我只是增加了复杂性,同时牺牲了性能以获得数据存储和用户定义选项的好处。
你怎么认为?
I thought I'd be flexible this time around and let the users decide what contact information the wish to store in their database. In theory it would look as a single row containing, for instance; name, address, zipcode, Category X, Listitems A.
Example
FieldType table defining the datatypes available to a user:
FieldTypeID, FieldTypeName, TableName
1,"Integer","tblContactInt"
2,"String50","tblContactStr50"
...
A user the define his fields in the FieldDefinition table:
FieldDefinitionID, FieldTypeID, FieldDefinitionName
11,2,"Name"
12,2,"Address"
13,1,"Age"
Finally we store the actual contact data in separate tables depending on its datatype.
Master table, only contains the ContactID
tblContact:
ContactID
21
22
tblContactStr50:
ContactStr50ID,ContactID,FieldDefinitionID,ContactStr50Value
31,21,11,"Person A"
32,21,12,"Address of person A"
33,22,11,"Person B"
tblContactInt:
ContactIntID,ContactID,FieldDefinitionID,ContactIntValue
41,22,13,27
Question: Is it possible to return the content of these tables in two rows like this:
ContactID,Name,Address,Age
21,"Person A","Address of person A",NULL
22,"Person B",NULL,27
I have looked into using the COALESCE and Temp tables, wondering if this is at all possible. Even if it is: maybe I'm only adding complexity whilst sacrificing performance for benefit in datastorage and user definition option.
What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是一个好方法,因为:
我个人认为最好采用不太“通用”的方法。保持简单。
更新:
问题是,您真的需要这样灵活的解决方案吗?对于联系人数据,您始终希望能够存储至少一组核心字段(地址行 1-n、名字、姓氏等)。如果您需要一种方法让用户在标准数据集之上存储自定义/用户可定义数据,那么这是一个常见的要求。各种选项包括:
这些问题之前已经讨论过,所以值得深入研究这个问题。快速浏览后似乎找不到我记得的问题!
找到一些讨论键值方法的优缺点的内容,以节省重复:
关系数据库中的键值对
数据库表中的键/值对
I don't think this is a good way to go because:
I personally think it's better to go for a less "generic" approach. Keep it simple.
Update:
The question is, do you really need a flexible solution like this? For contact data, you always expect to be able to store at least a core set of fields (address line 1-n, first name, surname etc). If you need a way for the user to store custom/user definable data on top of that standard data set, that's a common requirement. Various options include:
These have been discussed before here on SO so would be worth digging around for that question. Can't seem to find the question I'm remembering after a quick look though!
Found some that discuss the pros/cons of the key-value approach, to save repeating:
Key value pairs in relational database
Key/Value pairs in a database table