在 MySQL 数据库中使用 null。三个外键之一必须不为空。
我的问题是我必须创建具有外键的表,并且三个外键之一必须为 NOT NULL,其余的必须为 NULL。 MySQL 有什么办法可以解决这个问题吗? 迈克尔.
my problem is that i have to make table which will have foreign keys, and one of three foreign keys have to be NOT NULL and rest have to be NULL.
Is there anything in MySQL to solve it?
Michael.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
避免可为空的外键 - 它们有许多问题和缺点。通常,将这些列放在单独的表中会更容易、更好,这样当不存在值时就不必为它们创建空值。这应该是默认方法:针对每种不同情况使用范式,除非您有特殊原因将三列合并到一个表中。
Avoid nullable foreign keys - they have a number of problems and disadvantages. It's generally easier and better to put those columns in separate tables so that you don't have to create nulls for them when no value exists. That ought to be the default approach: Normal Form for each distinct case unless you have some special reason to combine the three columns into one table.
外键约束示例:
FOREIGN KEY Constraints example: