设计表时如何实现一对一、一对多、多对多关系?
谁能用一些例子解释如何在设计表格时实现一对一、一对多和多对多关系?
Can anyone explain how to implement one-to-one, one-to-many and many-to-many relationships while designing tables with some examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一对一:使用引用表的外键:
您还必须在外键列 (
addess.student_id
) 上设置唯一约束以防止出现多行子表 (address
) 中的内容与引用表 (student
) 中的同一行相关。一对多:在关系的多方使用外键链接回“一”方:
多对多:使用联结表(示例):
示例查询:
One-to-one: Use a foreign key to the referenced table:
You must also put a unique constraint on the foreign key column (
addess.student_id
) to prevent multiple rows in the child table (address
) from relating to the same row in the referenced table (student
).One-to-many: Use a foreign key on the many side of the relationship linking back to the "one" side:
Many-to-many: Use a junction table (example):
Example queries:
以下是关系类型的一些实际示例:
一对一 (1:1)
当且仅当表 A 中的一条记录与表中最多一条记录相关时,关系才是一对一的B.
要建立一对一关系,B 表(无孤立记录)的主键必须是 A 表(有孤立记录)的辅助键。
例如:
一对多 (1:M)
当且仅当表 A 中的一条记录为
与表 B 中的一条或多条记录相关。但是,表 B 中的一条记录不能与表 A 中的多条记录相关。
要建立一对多关系,表 A 的主键(“一”)表)必须是表 B(“多”表)的辅助键。
例如:
多对多 (M:M)
当且仅当表 A 中的一条记录与表 B 中的一条或多条记录相关时,关系才是多对多,反之亦然。
要建立多对多关系,请创建第三个名为“ClassStudentRelation”的表,该表将具有表 A 和表 B 的主键。
Here are some real-world examples of the types of relationships:
One-to-one (1:1)
A relationship is one-to-one if and only if one record from table A is related to a maximum of one record in table B.
To establish a one-to-one relationship, the primary key of table B (with no orphan record) must be the secondary key of table A (with orphan records).
For example:
One-to-many (1:M)
A relationship is one-to-many if and only if one record from table A is
related to one or more records in table B. However, one record in table B cannot be related to more than one record in table A.
To establish a one-to-many relationship, the primary key of table A (the "one" table) must be the secondary key of table B (the "many" table).
For example:
Many-to-many (M:M)
A relationship is many-to-many if and only if one record from table A is related to one or more records in table B and vice-versa.
To establish a many-to-many relationship, create a third table called "ClassStudentRelation" which will have the primary keys of both table A and table B.
一对多
一对多表关系如下所示:
在关系数据库系统中,一对多表关系基于
外键链接两个表
列中引用父表行的主键
的子表。在上表中,
post_comment
表中的post_id
列与post
表具有Foreign Key
关系id主键
列:一对一
一对一的表关系如下所示:
在关系数据库系统中,一对一的表关系基于以下关系链接两个表:子表中的
主键
列,它也是引用父表行的主键
的外键
。因此,我们可以说子表与父表共享主键。
在上表中,
post_details
表中的id
列与post
也具有Foreign Key
关系tableid
Primary Key
列:多对多
多对多表关系如下所示:
在关系数据库系统中,多对多许多表关系通过子表链接两个父表,子表包含两个引用两个父表的
主键
列的外键
列。在上表中,
post_tag
表中的post_id
列与post
也具有Foreign Key
关系表 idPrimary Key
列:并且,
post_tag
表中的tag_id
列与标签
表 ID主键
列:One-to-many
The one-to-many table relationship looks as follows:
In a relational database system, a one-to-many table relationship links two tables based on a
Foreign Key
column in the child which references thePrimary Key
of the parent table row.In the table diagram above, the
post_id
column in thepost_comment
table has aForeign Key
relationship with thepost
table idPrimary Key
column:One-to-one
The one-to-one table relationship looks as follows:
In a relational database system, a one-to-one table relationship links two tables based on a
Primary Key
column in the child which is also aForeign Key
referencing thePrimary Key
of the parent table row.Therefore, we can say that the child table shares the
Primary Key
with the parent table.In the table diagram above, the
id
column in thepost_details
table has also aForeign Key
relationship with thepost
tableid
Primary Key
column:Many-to-many
The many-to-many table relationship looks as follows:
In a relational database system, a many-to-many table relationship links two parent tables via a child table which contains two
Foreign Key
columns referencing thePrimary Key
columns of the two parent tables.In the table diagram above, the
post_id
column in thepost_tag
table has also aForeign Key
relationship with thepost
table idPrimary Key
column:And, the
tag_id
column in thepost_tag
table has aForeign Key
relationship with thetag
table idPrimary Key
column:一对一 (1-1) 关系:
这是主要和次要之间的关系。外键(与外键相关的主键只有一条记录)。这是一对一的关系。
一对多 (1-M) 关系:
这也是primary 和 primary 之间的关系。外键关系,但这里的主键与多个记录相关(即表A有书籍信息,表B有一本书的多个出版商)。
多对多(MM):多对多包括两个维度,下面通过示例详细解释。
One to one (1-1) relationship:
This is relationship between primary & foreign key (primary key relating to foreign key only one record). this is one to one relationship.
One to Many (1-M) relationship:
This is also relationship between primary & foreign keys relationships but here primary key relating to multiple records (i.e. Table A have book info and Table B have multiple publishers of one book).
Many to Many (M-M): Many to many includes two dimensions, explained fully as below with sample.