数据库表中的行限制
我有三个表:
BallId Color
ball_1 red
ball_2 red
ball_3 blue
ball_4 green
ball_5 green
.......
.
BoxId Color
box_1 green
box_2 green
box_3 red
.......
。
BoxId BallId
box_1 ball4
box_1 ball5
box_3 ball2
我想在 BoxId、BallId 表上强制颜色关系,是否可以示意性地实现?
I have three tables:
BallId Color
ball_1 red
ball_2 red
ball_3 blue
ball_4 green
ball_5 green
.......
.
BoxId Color
box_1 green
box_2 green
box_3 red
.......
.
BoxId BallId
box_1 ball4
box_1 ball5
box_3 ball2
I want to force color relationship on BoxId,BallId table, is it possible schematically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不太确定你的最终目标是什么。
如果您只是想确保底部表中的
BoxId
和BallId
必须存在于顶部两个表中,那么您可以使用 FOREIGN KEY(又名“引用完整性”) )。--- 编辑 ---
根据其他评论/响应,我发现您实际上希望确保通过第三个表连接的 2 行始终具有相同的颜色,但断开连接的行可以还是有自己的颜色的。
如果是这样,那么您可以像这样“滥用”键:
这是实际的 DDL SQL:
顺便说一句,这允许在“基”表和“连接”表中使用 NULL 颜色。如果这不是您想要的,那么添加 NOT NULL 约束很容易。
I'm not quite sure what is your end goal here.
If you are simply trying to ensure that a
BoxId
andBallId
in the bottom table must exist in the top two tables, then you can use FOREIGN KEYs (a.k.a "referential integrity").--- EDIT ---
Based on other comments/responses, I see that you actually want to ensure that 2 rows that are connected through the third table always have the same color, yet the disconnected rows can still have their own color.
If so, then you can "abuse" keys like this:
Here is the actual DDL SQL:
BTW, this allows for NULL colors both in "base" tables and in the "connection" table. It is easy to add NOT NULL constraints if that's not what you want.
一种方法(尽管它不是严格的“示意图”)是在第三个表上有一个插入触发器,它检查正在输入的球/框的颜色,如果它们不相同,则抛出异常
One way to do it (although its not strictly 'schematic') is to have an insert trigger on the third table which checks the colours of the ball/box being entered and throws an exception if they're not the same
可以使用复合外键通过数据库来完成此操作:
然后您可以将哪些球允许放入
boxBallRule
表中的哪个盒子中。任何不符合“允许”框与球关系的boxBall
表插入都会失败。因此:最后一条语句应该失败,因为它违反了
boxBallRule
表上的复合外键。It is possible to do this via the database using a composite foreign key:
You can then store which balls are allowed in which box in the
boxBallRule
table. Any insert into theboxBall
table that does not conform to the 'allowed' box to ball relationship will fail. Hence:The last statement should fail since it violates the composite foreign key onto the
boxBallRule
table.我认为,从关系理论的角度来看,这个问题的答案如下:你在这里真正想说的是,你有一组盒子和一组球,每个球都在一个盒子里。盒子和球都有一种颜色,球只能位于颜色相匹配的盒子中。但将球的颜色存储在球表中是一个设计错误。相反,您应该只存储每个球所在的盒子,然后您知道球是什么颜色,因为您可以检查它存储在其中的盒子的颜色(使用连接)。
所以,不,您没有可以指定的约束来强制执行您想要的关系,但那是因为您以错误的方式处理此问题。 balls 表中不应有
Color
列。编辑:上面假设每个球都必须在一个盒子里。 OP 澄清并非每个球都需要放在盒子里。这似乎是一个更难的问题,因为在这种情况下,您不能依靠盒子表来跟踪球的颜色。我可以看到一些不同的解决方案,但没有一个是完美的。
I think the answer to this, in terms of relational theory, is the following: what you're really saying here is that you have a set of boxes and a set of balls, and each ball is in a box. The boxes and balls each have a colour, and a ball can only be in a box of the matching colour. But it's a design error to store the colour of the ball in the balls table. Rather, you should just store which box each ball is in, and then you know what colour the ball is because you can check the colour of the box that it is stored inside (using a join).
So, no, there isn't a constraint you can specify that enforces the relationship you want, but that's because you're going about this the wrong way. You shouldn't have the
Color
column in the balls table.EDIT : The above assumes that every ball must be in a box. The OP clarifies that not every ball need be in a box. This seems to be a harder problem, because in that case you can't rely on the boxes table to keep track of what colour a ball is. I can see a few different solutions, none of them perfect.