如何限制两个表之间一对多关系中的许多关系? [英] How to limit number of the many in a one-to-many relationships between two tables?

查看:76
本文介绍了如何限制两个表之间一对多关系中的许多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个MySql表的MySQL数据库.首先是与第一个表(表A)具有一列具有唯一值(从值(从1到n)的列.在第二个表2(表B)中,我有两列:在第一列中,我有一个名称,在第二个是从1到n的值(如果我在中添加一个值.第二个是表B中的已更新列,是对表A中第一个(反之亦然)的引用.

I have a MySQL database with two MySql tables. First is with The first table (table A) has a column with unique values(from values (from 1 to n). In the second table 2 (table B), I have two columns: in the first I have a name and in the second i have values from 1 to n(if i ad an value in the . The second it's updated column in table B is a reference to the first and vice versa. That's done)column in table A.

我的问题:我可以限制表A中第二个值出现在表B中的次数最多最多10次吗? 一个示例是这样的:(列val限制为3)(第三个表将发送错误)

My Question: Can I limit the number of aparation of times the values from the second in table A appear in table B, specifically to a maximum of 10 times? An example is this: (with limitation for column val to 3) (the third table would send an error)

First table:      Second table:        Third table(imaginary)
+---+             +----+-----+          +----+-----+ 
|val|             |name| val |          |name| val |
+---+             +----+-----+          +----+-----+
| 1 |             |  a |  1  |          |  a |  1  |          
| 2 |             |  b |  2  |          |  b |  2  |
+---+             |  c |  1  |          |  c |  1  |
                  |  d |  1  |          |  d |  1  |
                  +----+-----+          |  e |  1  |
                                        +----+-----+

PS:对不起,我的英语.

PS: Sorry for my english.

推荐答案

您需要在第二个表的表定义中添加约束.这样,数据库将在插入和更新时为您检查值.

You need to add a constraint to the table definition of the second table. This way, the database will check the value for you upon insert and update.

这篇关于如何限制两个表之间一对多关系中的许多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆