多对多关系(五个表) [英] Many to many relation (five tables)

查看:129
本文介绍了多对多关系(五个表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五张表代表多对多的关系如下:

----------------------- -------------------

1.labels

------------ ------------------------------

id(主键)

label_key

label_value



----------------------- -------------------

2.resource_a

------------ ------------------------------

id(主键)

instance_name



-------------------------------- ----------

3.resource_b

--------------------- ---------------------

id(主键)

instance_name



----------------------------------------- -

4.resource_c

------------------------------ ------------

id(主键)

instance_name



------------------------------------------

5.resource_d

-------------------------------- ----------

id(主键)

instance_name



我试过的:



标签表id分别与以下四个资源表instance_name是多对多的关系,我的想法是建立一个中间表,表中的一个外键字段在表id中的关联标签之间,另一个字段不做链接,只是instance_name存储字段,但这种结构在添加数据时不知道如何加入



请指教我

谢谢

解决方案

你没有发帖代码告诉我们您的问题在哪里使问题非常通用。所以我会给你一般的答案。

多对多关系通常涉及你提到的中间表,但是每个连接表都有外键。很难用所有名为'resource_x'的表来计算你的目标,所以让我们考虑学生和课程(不是C#/ C ++课程,而是在学校提供课程的课程)。一堂课可以有很多学生,学生可以参加很多课程。您的Student_Class表至少包含StudentId和ClassId,分别是Student和Class的外键。这两个值可以组合成一个复合键,也可以有一个额外的字段StudentClassId,即该表的IDENTITY。



HTH,Mike

Hi,I have a five tables representing a many to many relation as follows:
------------------------------------------
1.labels
------------------------------------------
id (primary key)
label_key
label_value

------------------------------------------
2.resource_a
------------------------------------------
id (primary key)
instance_name

------------------------------------------
3.resource_b
------------------------------------------
id (primary key)
instance_name

------------------------------------------
4.resource_c
------------------------------------------
id (primary key)
instance_name

------------------------------------------
5.resource_d
------------------------------------------
id (primary key)
instance_name

What I have tried:

Labels table id respectively with the following four resource table instance_name is a many-to-many relationship, my idea is to establish a middle table, a foreign key field in the table among associated labels in the table id, another field don't do link, just instance_name storage field, but this kind of structure when adding data don't know how to join

please advice me
Thank you

解决方案

You didn't post any code showing us where your issue is which makes the question very generic. So I'll give you the generic answer.
The many-to-many relation typically involves the middle table that you mentioned but with the foreign key for each of the joined tables. It's tough to figure what you're going for with all the tables named 'resource_x' so let's consider Students and Classes (not the C#/C++ class, but the class that offers a course at school). A class can have many students in it, and a student can attend many classes. Your "Student_Class" table would have at least StudentId and ClassId, the foreign key for Student and Class respectively. Those two values can either be combined to make a composite key or it can have an additional field StudentClassId that is the IDENTITY for that table.

HTH, Mike


这篇关于多对多关系(五个表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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