字典表关系 (MS SQL 2005)
我有一个名为 'Dictionary' 的表,其列如下:
- ID bigint
- TYPE varchar (200)
- ITEM varchar (200)
表被各种表用作简单的字典/查找。
例如,它存储国家、名称、业务类型列表。
TYPE列保存有关字典类型的信息,ITEM是字典字符串值。
一切正常,但我在建立字典和外国表之间的关系时遇到问题。
当我使用“外键关系”时,我无法使其依赖于“类型”列。
(请注意具有相同类型的同一项目 - 例如,当具有其他类型的项目时,“国家/地区”可以链接到多个表可以链接到不同的)
目前我正在使用 USP 来管理它,但我想切换到标准关系机制。
有什么建议如何获得吗?
I have table named 'Dictionary' with columns as follow:
- ID bigint
- TYPE varchar (200)
- ITEM varchar (200)
Table is used by various tables as simple dictionary / lookup.
Eg it stores countries, titles, business type lists.
TYPE column keeps info about type of dictionary , ITEM is dictionary string value.
All works well but I have problem to set up relationship between dictionary and foreigin tables.
When I'm using 'Foreign Key Relationship' I can not make it depended of 'TYPE" column.
(Please note same item with same type - eg 'countries' can be linked to several tables, when item with another type can be linked to different)
Currently I'm using USPs to manage that but I'd like to switch to standard relationship mechanism.
Any advice how to get that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您可以考虑另一种设计
,然后在您当前想要引用 Type 的位置建立指向 DictionaryType 表的 ID 的链接> 来自您原始设计的字段
It looks to me that you could consider an alternative design
and then make links to the ID of DictionaryType table in places where you currently want to reference Type field from your original design
从问题的上下文来看,我猜您需要执行以下两件事之一:
Type
列设为主键ID 字段在这里。
外键需要引用主键,并且看起来您的
Type
列不是 PK。From the context of the question, I'm guessing you'll need to do one of two things:
Type
column the primary keyID
field here.Foreign keys need to refer to a primary key, and it looks like your
Type
column isn't a PK.您这里拥有的是 EAV 数据库设计,该设计很糟糕,原因有很多,其中之一就是您的问题。 这个问题没有真正意义上的解决方案。 您可以尝试使用 sql_variant 作为该项目的列类型,并尝试建立 PK-FK 关系。
还有另一种方法,您可以尝试使用 xml 数据类型和模式来执行此操作,就像我描述的 此处。 但是,您必须对此进行测试,看看它是否适用于您的问题。
what you have here is an EAV db design which is bad for number of reasons one being your problem. there is no solution for this in the real sense. you might try using sql_variant as a column type for the item and try to to a PK-FK relationship on that.
there's another way you could try to do this with the xml datatype and schemas like i describe here. however you'll have to test this to see if it applies to your problem.