SQL Server 2008 中允许重复的唯一标识符吗?
假设我的数据库中只有 2 个表。这两个表是:InputType 和 HardwareType。每个表都使用 GUID 作为主键。从InputType 到HardwareType 有一个外键。
我遇到的问题是,会有多行 InputType 引用 HardwareType 中的同一行(即每种类型的硬件有多个输入)。问题在于,我在 InputType 中引用 HardwareType 中的 GUID 的列不允许重复 - 因此,不允许每个硬件类型有多个输入。
我可以通过将 InputType 中的列的数据类型设置为“varchar”或其他类型而不是“uniqueidentifier”来解决这个问题,但是我必须这样做吗?当 GUID 不是主键而是另一个表的外键时,是否有某种方法允许重复输入 GUID?
感谢帮助!谢谢。
Lets say I have only 2 tables in my DB. The two tables are: InputType and HardwareType. Each table uses GUIDs as their primary key. There is a foreign key from InputType to HardwareType.
The problem I am having is that there will be multiple rows of InputType that refer to the same row in HardwareType (ie. there are several inputs per type of hardware). The issue with this is that the column I have in InputType that refers to the GUID in HardwareType will not allow duplicates -- therefore, not allowing multiple inputs per hardware type.
I could probably get around this by setting the datatype of the column in InputType to a "varchar" or something instead of "uniqueidentifier", but do I have to do it this way? Is there someway to allow duplicate entries of GUIDs when it isn't the primary key, but instead a foreign key to another table?
Help is appreciated! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅仅因为数据类型是
UNIQUEIDENTIFIER
并不意味着您不能在该列中多次使用相同的值!事实上,您可以 - 当然,除非您在该列上显式添加了
UNIQUE CONSTRAINT
或UNIQUE INDEX
- 这是您的选择,但默认情况下不会应用任何内容,除非您自己做一些事情。因此,您应该能够使用
UNIQUEIDENTIFIER
从InputType
引用HardwareType
- 即使InputType
中的多行将引用HardwareType
中的同一行 - 完全没有问题。Just because the datatype is
UNIQUEIDENTIFIER
doesn't imply you cannot have the same value in that column multiple times!You CAN in fact - unless of course, you've explicitly added a
UNIQUE CONSTRAINT
orUNIQUE INDEX
on that column - this is your choice, but there's nothing applied by default, unless you do something about it yourself.So you should be able to reference
HardwareType
fromInputType
using theUNIQUEIDENTIFIER
- even if multiple rows inInputType
will reference the same row inHardwareType
- no problems at all.