SQL 2008:对每种数据类型使用单独的表来返回单行

发布于 2024-08-28 19:39:08 字数 1151 浏览 11 评论 0原文

我想这次我应该灵活一点,让用户决定希望在数据库中存储哪些联系信息。理论上,它看起来像一行,包含以下内容:姓名、地址、邮政编码、类别 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 技术交流群。

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

发布评论

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

评论(1

断肠人 2024-09-04 19:39:08

我认为这不是一个好方法,因为:

  • 一个联系人的 1 条记录的简单插入突然变成了 n 条记录。例如,如果您存储联系人的 varchar、nvarchar、int、bit、datetime、smallint 和tinyint 数据,则在数据类型特定表中插入 7 次,主标头记录 +1
  • 同样,查询将自动引用 7 个表,其中有 6 个 JOIN参与只是为了获得完整的细节,

我个人认为最好采用不太“通用”的方法。保持简单。

更新:
问题是,您真的需要这样灵活的解决方案吗?对于联系人数据,您始终希望能够存储至少一组核心字段(地址行 1-n、名字、姓氏等)。如果您需要一种方法让用户在标准数据集之上存储自定义/用户可定​​义数据,那么这是一个常见的要求。各种选项包括:

  • 主联系人表中的 XML 列,用于存储所有用户定义的数据
  • 1 个包含键值对数据的额外表,有点像您最初讨论的那样,但程度要小得多!这将包含联系人的键、自定义数据项名称和值。

这些问题之前已经讨论过,所以值得深入研究这个问题。快速浏览后似乎找不到我记得的问题!

找到一些讨论键值方法的优缺点的内容,以节省重复:
关系数据库中的键值对
数据库表中的键/值对

I don't think this is a good way to go because:

  • A simple insert of 1 record for a contact suddenly becomes n inserts. e.g. if you store varchar, nvarchar, int, bit, datetime, smallint and tinyint data for a contact, that's 7 inserts in datatype specific tables, +1 for the main header record
  • Likewise, a query will automatically reference 7 tables, with 6 JOINs involved just to get the full details

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:

  • XML column in your main Contacts table to store all the user defined data
  • 1 extra table containing key-value pair data a bit like you originally talked about but to much lesser degree! This would contain the key of the contact, the custom data item name and the value.

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

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