简体   繁体   English

如何将具有两个不同主键的两个表联接到另一个表中?

[英]How can I join two tables with two different primary keys into another table?

I have two tables: students and courses , assuming that each student can be in more than one course and that each course can have more than one student. 我有两个表格:“ students和“ courses ,假设每个学生可以参加一个以上的课程,并且每个课程可以有一个以上的学生。

[Table Students]             [Table Courses]
id(PK)                       id(PK)
name                         name
age                          duration
etc...                       etc...

and what I want to do it is to relate both tables into another table, for example, studying , in which I will store the course or courses that is doing each student. 我要做的是将两个表关联到另一个表(例如studying ,其中将存储每个学生正在学习的课程。 Like this: 像这样:

[Table studying]
idStudent
idCourse

What I have deduced 我的推论

I think that idStudent and idCourse should be foreign keys because the information it is stored in students and courses respectively with an unique primary key and to respect the consistency of the database. 我认为idStudentidCourse应该是外键,因为它的信息分别以唯一的主键存储在studentscourses ,并尊重数据库的一致性。 It cannot exist a relation without information neither of the student nor the course or just without the information of one of them. 没有学生或课程的信息,或者没有他们之一的信息,就不可能存在关系。

I also know that some tables has two primary keys to allow that in the table could exist more than one repeated value of a primary key, but not of both primary keys at the same time. 我还知道有些表具有两个主键,以允许表中的主键可以存在多个重复值,但不能同时存在两个主键。

My questions 我的问题

  • These ids ( idStudent , idCourse ). 这些ID( idStudentidCourse )。 Have to be primary keys or foreign keys? 必须是主键还是外键?
  • Should the table studying has another column with an ID? studying的表格是否应该具有ID的另一列?
  • Is my deduction in the good way? 我的推论是好的吗?

PS: I do not need sql statements, I just need help to clarify my confusion. PS:我不需要sql语句,我只需要帮助来澄清我的困惑。

Thanks in advance! 提前致谢!

These ids (idStudent, idCourse). 这些ID(idStudent,idCourse)。 Have to be primary keys or foreign keys? 必须是主键还是外键?

You want them to be foreign keys, because the existence of each record on your third table depends on the availability of the first, that is, there cannot be a "Student Course" or a "Course with Students" without either the course or the student. 您希望它们是外键,因为第三个表上每个记录的存在都取决于第一个表的可用性,也就是说,没有课程或课程表,就不可能有“学生课程”或“学生课程”学生。 It could (if you don't make those keys) but you would break referential integrity 可以(如果不做这些键的话)但是会破坏参照完整性

On the other hand, having FK's is usually a good thing because you make sure that you don't remove dependable records by mistake (which is what the constraint is for on the first place) unless you did something like cascade deleting 另一方面,拥有FK通常是一件好事,因为您可以确保不要错误地删除可靠的记录(这首先是约束的目的),除非您执行了类似层叠删除的操作

Should the table studying has another column with an ID? 所研究的表格是否应该具有ID的另一列?

No, it does not have to but again, sometimes it is a good practice because some software like Object Relational Mappers, Diagram Software, etc. may rely on the fact that they always needs a by-convention primary key. 不,不是必须的,但有时又是一个好习惯,因为某些软件(例如,对象关系映射器,图表软件等)可能依赖于这样的事实,即它们始终需要使用约定俗成的主键。 Some others don't even support composite keys so while it is not mandatory it can help in the future and it does not hurt. 其他一些甚至不支持复合键,因此虽然不是强制性的,但将来可以提供帮助,并且不会造成伤害。 Of course this all depends on what you are using the database for and how (pure SQL, which engine you use, if you use it with a framework etc.) 当然,这一切都取决于您使用数据库的目的和方式(纯SQL,使用哪种引擎,如果与框架一起使用等)。

Is my deduction in the good way? 我的推论是好的吗?

All is relative. 一切都是相对的。 But I think your logic is good. 但我认为您的逻辑很好。 My advice is that you always design your data schemas as flexible as you can because if a project grows its harder (and more costly) to do those changes down the road. 我的建议是,您始终应尽可能灵活地设计数据模式,因为如果项目变得越来越困难(且成本更高),则很难在将来进行这些更改。 Invest time on thinking how you may expand your application functionality and think if the schema will adapt to it. 花一些时间思考如何扩展应用程序功能,并考虑架构是否适合它。

If you like you can make them primary keys on studying table. 如果愿意,可以使它们成为学习桌的主键。 But this is unnecesary, because relation (role of studying table) is many to many and this kind of table dont need primary keys. 但这不是必需的,因为关系(学习表的角色)是多对多的,并且这种表不需要主键。 You need to know that also when you make them pk (pair of student id and course id) , thats mean that theee could be only one pair of each, thats equivalent to constrain unique - student can take a course only ones. 您需要知道,当您将它们设置为pk(学生ID和课程ID对)时,这意味着theee只能是一对,这等同于限制唯一性-学生只能学习一门课程。 In the future you maybe would like to add to this table start_date and this kind of pk could be a problem, you will need to modify them. 将来您可能想将start_date添加到该表中,而这种pk可能会成为问题,您需要对其进行修改。

Your deduction is correct. 您的推论是正确的。 In fact, you should have a composite primary key consisting of both (idStudent, idCourse) columns, because this tuple is the identifier of row in the table, you do not need additional ID column (of course, you can also take that approach to add additional ID column that would be your primary key, but you do not need it if one student can have one course assigned only once) 实际上,您应该有一个由两个(idStudent,idCourse)列组成的复合主键,因为此元组是表中行的标识符,因此不需要其他ID列(当然,您也可以采用这种方法添加其他ID列作为您的主键,但是如果一个学生只能分配一门课程一次,则不需要此列)

To respect the integrity, both columns (separately) should be foreign keys - idStudent should be referencing id column of Students table and idCourse should reference id column of Courses table. 为了尊重完整性,这两列(分别)应该是外键-idStudent应该引用“学生”表的id列,而idCourse应该引用“课程”表的id列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM