将第三个表链接到多对多关联中的桥表 [英] Linking a third table to a bridge table in a many to many association
问题描述
设计此数据库的正确方法是什么?
这是我如何设置表格:
我有一个表之间的多对多关系称为老师和一个名为仪器的表。然后我有一个桥梁表连接两者。我想将另一个表与BRIDGE表相关联。表示仪器/教师组合。该表将具有3行,指定教师可以教授什么级别的教学(即初级,中级,高级)。看起来像我应该设置bridge表有teacher_id,instrument_id和level_id,但我不知道这是否是常规的方式做。
我是使用mysql和cakephp,我还没有找到任何关于HABTM关联关于在桥表中有一个额外的字段的文档。只是想确保我做的正确。
如果这是你的数据要求,听起来像它,然后去。 b
$ b
基本上,正如你所说,你将有一个三部分的关键字:
- teacher_id
- instrument_id
- level_id
老师可以教初级和高级的吉他,中级和初级的钢琴,另一位老师可以在所有三个级别上教钢琴,初学者可以双簧管...听起来不错。
顺便说一句,桥表也被称为 intersect
或交集
表。
What is the correct way to design this database?
Here is how I have my tables set up:
I have a many to many relationship between a table called teachers and a table called instruments. I then have a bridge table connecting the two. I would like to associate another table with the BRIDGE table. Meaning the instrument/teacher combination. That table would have 3 rows specifying what level of playing a teacher can teach (ie. Beginner, intermediate, advanced). Seems like I should set up the bridge table to have teacher_id, instrument_id AND level_id, but I don't know if this is the conventional way to do it.
I am using mysql and cakephp, and I haven't found anything in the documentation about the HABTM associations about having an extra field in the bridge table. Just want to make sure I'm doing it correctly.
If that's what your data calls for, and it sounds like it does, then go for it.
Basically, as you noted, you'd have a three-part key:
- teacher_id
- instrument_id
- level_id
Which means that a teacher could teach guitars at beginner and advanced, piano at intermediate and beginner, and another teacher could teach piano at all three levels and oboe at beginner ... sounds good.
Incidentally, the bridge table is also known as an intersect
or intersection
table.
这篇关于将第三个表链接到多对多关联中的桥表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!