简体   繁体   English

在 MySQL 表关系设计方面需要帮助

[英]Need help with MySQL table relation design

I'm creating web application for my friend, a tutor school system.我正在为我的朋友,一个辅导学校系统创建 web 应用程序。 Now, I'm having a problem about designing table to store teacher record.现在,我在设计表格来存储教师记录时遇到了问题。 Here is the scenario...这是场景...

1. We have to create course first 1.我们必须先创建课程
2. Then we add teacher to course (Let's say that we already have teacher record stored in teacher table), each course can have more than one teacher and each teacher can teach more than one course. 2. 然后我们将教师添加到课程中(假设我们已经有教师记录存储在教师表中),每门课程可以有多个教师,每个教师可以教多个课程。
3. Everytime teacher come to teach, we keep record. 3. 每次老师来教,我们都有记录。 Each teacher teaching at a course more than once.每位教师不止一次教授一门课程。
4. By the end of the month, we pull all teaching record out and calculate money we have to pay 4. 到月底,我们把所有的教学记录都拿出来,计算我们要付的钱

This is my current tables这是我目前的表

// Course Table
course_id (PK)
...
...

// Teacher Table
teacher_id (PK)
...
...

// Course_teacher
course_id (FK: course.course_id)
teacher_id (FK: teacher.teacher_id)

The problem is I can't add fee to the course_teacher table because at the first moment I add teacher to this course, there is no teaching happened yet.问题是我无法在 course_teacher 表中添加费用,因为在我向这门课程添加教师的第一刻,还没有发生教学。 If I add column "fee" and "date" to this table, data will be like...如果我在此表中添加“费用”和“日期”列,数据将类似于...

1,1,null,null (This record add before the course is starting, which I don't like having null field(s)) 1,1,null,null (此记录在课程开始前添加,我不喜欢 null 字段)
1,1,50,2011-06-12 (This record add at the first time teacher is teaching) 1,1,50,2011-06-12(此记录在老师第一次教学时添加)
1,1,50,2011-06-19 (This record add at the second time teacher is teaching) 1,1,50,2011-06-19(此记录在老师第二次授课时添加)
... ...

I was thinking about adding one more table like我正在考虑再添加一张桌子,例如

//Teaching_fee
course_id (FK: course.course_id)
teacher_id (FK: teacher.teacher_id)
fee
paid_status
date

If I do so,如果我这样做,
1. course_teacher will keep information of teacher related to course 1. course_teacher 会保存与课程相关的老师信息
2. teaching_fee will keep information of the teaching fee 2. Teaching_fee 将保留教学费用信息

It should works.它应该有效。 But, something in my mind is telling me that this is not right.但是,我脑海中的某件事告诉我,这是不对的。 Those two tables are similar to each other even their creation purpose are different.这两个表彼此相似,即使它们的创建目的不同。

Any suggestion?有什么建议吗?

Edit 1 : Just came up with one idea.编辑1 :刚想出一个主意。 What if do this way如果这样做怎么办

//Course_teacher Table
id (PK)
course_id (FK: course.course_id)
teacher_id (FK: teacher.teacher_id)

//Teaching_fee Table
id (FK: course_teacher.id)
fee
paid_status
date

Final solution : I decided to do as btreat suggested最终解决方案:我决定按照btreat 的建议做

// Course Table
course_id (PK)
...
...

// Teacher Table
teacher_id (PK)
...
...

// Course Teacher Table
course_teacher_id (PK) // synthetic
teacher_id (FK)
course_id (FK)
...
...

// Teacher Fee Table
course_teacher_id (PK)
date_paid (PK)
fee
paid_status
...
...

This would probably be more normal.这可能会更正常。

TABLE RateCards uid fee paid_status date TABLE RateCards uid feepaid_status date

TABLE Teacher_RateCards uid RateCardId TeacherId TABLE Teacher_RateCards uid RateCardId TeacherId

If you use a 'teaching_fee' table rather than 'course_teacher', I wouldn't be concerned about having a null value for fee as long as the business logic in the application knows how to interpret null (ie the fee has not yet been paid).如果您使用“teaching_fee”表而不是“course_teacher”,只要应用程序中的业务逻辑知道如何解释 null(即尚未支付费用),我就不会担心有一个 null 值作为费用)。

However, based on your description, it sounds like a record of each time a teacher is paid for a course and the date that payment was made needs to be kept around?但是,根据您的描述,这听起来像是每次为教师支付课程费用的记录,并且需要保留付款日期? If that's the case, you will definitely need a 'teaching_fee' table where the combination of teacher_id, course_id and date_paid forms a unique key.如果是这种情况,您肯定需要一个“teaching_fee”表,其中teaching_fee 的组合是teacher_id、course_id 和date_paid forms 的唯一键。 If separately, you need to know all of the unique teacher / course combinations, you can query the 'teaching_fee' table.如果分开,你需要知道所有唯一的教师/课程组合,你可以查询'teaching_fee'表。 However, if the teacher / course combination doesn't get created in 'teaching_fee' until the teacher is paid, having a separate 'course_teacher' table would be fine.但是,如果教师/课程组合在教师获得报酬之前没有在“teaching_fee”中创建,那么有一个单独的“course_teacher”表就可以了。

To avoid your dissatisfaction with the denormalizing (repeating) course / teacher combinations, another alternative would be to structure your tables like this:为了避免您对非规范化(重复)课程/教师组合的不满,另一种选择是像这样构建您的表格:

// Course Table
course_id (PK)
...
...

// Teacher Table
teacher_id (PK)
...
...

// Course Teacher Table
course_teacher_id (PK) // synthetic
teacher_id (FK)
course_id (FK)
...
...

// Teacher Fee Table
course_teacher_id (PK)
date_paid (PK)
fee
paid_status
...
...

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

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