SQL Server CE nvarchar 外键带有尾随空格
在 SQL Server CE 中,仅在删除尾随空格后才会强制执行对 nvarchar 字段的外键约束。这意味着如果 PK 是“foo”,我可以将“foo”插入到 FK 中。
为什么会这样呢?它似乎严重破坏了外键系统应提供的数据完整性。
有没有办法强制执行外键约束,以便在比较中包含空格?我有哪些选项可以解决此问题?
用整数替换 FK 字段是最明显的解决方案,但由于相关应用程序的实现方式,这是最后的手段(在我的例子中)。
In SQL Server CE, foreign key constraints on nvarchar fields are only enforced after dropping the trailing whitespace. This means that if the PK is "foo " I can insert "foo" into the FK.
Why is this the case? It seems to badly undermine the data integrity the foreign key system is supposed to provide.
Is there any way to enforce a foreign key constraint such that whitespace is included in the comparison? What options do I have for working around this behavior?
Replacing the FK fields with ints is the most obvious solution, but is a last resort (in my case) due to the way the related application has been implemented.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的。您唯一的选择是不使用基于字符串的 FK。您可以创建一个查找表,其中包含分配了唯一 ID 的字符串,然后您可以根据需要将其用于 FK。
That is correct. Your only choice is to not use string based FK's. You can create a lookup table with the strings in them that assigned a uniqueID you then use for your FK's if you like.