使用外键 - 只有 2 条记录?
我是否应该使用只有“贷方”或“借方”两个选项的“balanceimpact”外键?
这是否有意义,这里只有两个选择?
Should I use a foreign key for "balanceimpact" that only has two options either "CREDIT" or "DEBIT"?
Does it even make sense, there are only two options here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重要的不是有多少个选项,而是这是什么类型的 rel(1:1;1:n;等等)。
在这种特殊情况下,我不会使用外键,只需将其保留为列即可。
另外,正如 Marc_S 在他的评论中建议的那样,添加 CHECK 约束以确保仅输入这两个值之一。
It's not so much as how many options there are, but what kind of rel this is (1:1; 1:n; etc).
On this particular case, I wouldn't use a foreign key, just leave it as a column.
Also, as Marc_S suggested on his comment, add a CHECK constraint to ensure that only one of those 2 values are entered.
名言“只有两种选择”,
一毛不拔二十种,但是,当我看到这句话时,我的做法略有不同。我说用 fk 创建一个查找表。只是因为当他们想要添加第三个、第四个和..选项时,您将拥有所有结构。显然,在这种情况下,你可以采取任何一种方式,都可以,我只是不明白为什么一开始就将其标准化并避免将来发生任何返工是那么糟糕,即使有很小的机会。只是我深夜头顶的2美分。
Famous words "There will only be two options"
Not to split a hair 20 ways but, I have a slightly different approach when seeing this. I say create a lookup table with a fk. Only because when they want to add the 3rd, 4th, and .. options you will have all the structure in place. Obviously in this case you can go either way and be fine, I just don't see why it's so bad to normalize it up front to begin with and avoid any rework in the future even if there is a slight chance of it. Just my 2cents of the top of my head late at night.
如果您只有两个选项,那么似乎更好的解决方案就是有一列是真/假。除非它映射到其他表。
If you're only going to have two options it seems that the better solution would be just to have a column that is true/false. Unless it's mapping to other tables.