MySQL:两个 n:1 关系,但不能同时存在
对不起,这个标题很难解释。
我需要一个与此类似的数据模型:
如您所见,集合可以同时属于用户或学校。我的问题:它应该只允许属于用户或学校。但永远不会同时两者。
我该如何解决这个问题?
Sorry for the title, it's difficult to explain.
I need a data model similar to this:
As you can see, a set can belong to both a user or a school. My problem: It should only be allowed to belong either to a user OR a school. But never both at the same time.
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您当前的设计称为“独占弧”,其中“sets”表有两个外键,并且需要其中一个为非空。这是实现多态关联的一种方法,因为给定的外键只能引用一个目标表。
另一个解决方案是创建一个
用户
和学校
都引用的通用“超级表”,然后将其用作集
的父级。您可以将其视为类似于 OO 建模中的接口:
回复您的评论:
让 SetOwners 表生成 id 值。您必须先插入 SetOwners,然后才能插入 Users 或 Schools。因此,请使“用户”和“学校”中的 id不自动递增;只需使用 SetOwners 生成的值:
这样,学校和用户都不会使用给定的 id 值。
你当然可以做到这一点。事实上,可能还有用户和学校共有的其他列,您可以将这些列放在超表 SetOwners 中。这涉及到 Martin Fowler 的类表继承模式。
您需要加入。如果您从给定的 Set 进行查询并且知道它属于用户(而不是学校),则可以跳过加入 SetOwners 并直接加入 Users。连接不一定必须通过外键。
如果您不知道给定的集合是否属于用户或学校,则必须对两者进行外连接:
您知道 SetOwner_id 必须匹配一个或另一个表(用户或学校),但不能同时匹配两者。
Your current design is called exclusive arcs where the
sets
table has two foreign keys, and needs exactly one of them to be non-null. This is one way to implement polymorphic associations, since a given foreign key can reference only one target table.Another solution is to make a common "supertable" that both
users
andschools
references, and then use that as the parent ofsets
.You can think of this as analogous to an interface in OO modeling:
Re your comments:
Let the SetOwners table generate id values. You have to insert into SetOwners before you can insert into either Users or Schools. So make the id's in Users and Schools not auto-incrementing; just use the value that was generated by SetOwners:
This way no given id value will be used for both a school and a user.
You can certainly do this. In fact, there may be other columns that are common to both Users and Schools, and you could put these columns in the supertable SetOwners. This gets into Martin Fowler's Class Table Inheritance pattern.
You need to do a join. If you're querying from a given Set and you know it belongs to a user (not a school) you can skip joining to SetOwners and join directly to Users. Joins don't necessarily have to go by foreign keys.
If you don't know whether a given set belongs to a User or a School, you'd have to do an outer join to both:
You know that the SetOwner_id must match one or the other table, Users or Schools, but not both.
需要注意的是:尚不完全清楚数据模型中的集合是什么。
我在质疑你的数据模型。
为什么用户->布景和学校 ->集合必须指向同一个表。如果这些确实是独立的数据集,那么它就不会更加标准化。仅仅因为它们在跟踪的列中有一些相似之处并不意味着它们应该存储在同一个表中。它们在逻辑上是同一件事吗?
只需为学校集和用户集创建单独的表即可。这也将使反向查询变得更容易,因为您不必检查 Sets 表中的空关系来知道它是否真的是 UserSet 还是 SchoolSet。
With the caveat: It's not entirely clear what a Set is in your data model.
I'm questioning your data model.
Why do User -> Sets and School -> Sets have to point to the same table. It's not more Normalized if those are really independent sets of data. Just because they share some similarities in the columns that they track doesn't mean they should be stored in the same table. Are they logically the same thing?
Just create separate tables for School Sets and for User Sets. This will make inverse queries easier as well because you won't have to check for null relationships in the Sets table to know if it's really a UserSet or a SchoolSet.
您必须使用触发器来强制执行此业务规则,因为 MySQL 有但不强制执行 CHECK 约束(在任何引擎上)。
You'll have to use a trigger to enforce this business rule, because MySQL has but don't enforce CHECK constraints (on any engine).