这个数据库结构是正确的还是有更好的方法?

时间:2011-10-25 23:52:09

标签: php mysql database

我有三个表,Student,StudentClass和Class表。学生表显示学生信息,课程表显示大学中正在进行的课程,学生课程表将学生链接到他们的课堂。问题是,如果我创建一个查询,如果我加入所有三个表,我会在查询中得到重复的行。所以我的问题是,我是否有权利用这三个表创建这样的学生和课堂数据库,或者是否有更有效和更好的方法来实现这一目标。还有其他表,但我希望你只关注这三个表。谢谢

以下是表格:

Student Table:


StudentId(PK) StudentForename     StudentSurname    Year   StudentUsername    CourseId(FK) 
S1            Mayur                  Patel            3       u0867587          INFO101
S2            Jim                    Carlton          3       u1231231          INFO101
S3            Ahmed                  Seedat           3       u0660663          INFO101
S4            Amar                   Barot            3       u0954857          INFO101
S5            Richard                Davies           3       u0877223          INFO101

StudentClass Table:

ClassId(PK)  StudentId(PK)
101        S1
102        S3
103        S1
104        S2


    Class Table:

ClassId(PK)    Room      ClassDay      ClassTime      ModuleId(FK)      CourseId(FK)      TeacherId(FK) 
101          CW4/10     Thursday        10:00:00      CHI2550          INFO101             T1
102          CW5/01     Wednesday       12:00:00      CHI2565          INFO101             T5   
103          CW2/04     Monday          15:00:00      CHT2520          INFO101             T2
104          CW4/10     Thursday        11:00:00      CHI2550          INFO101             T1

下面的查询:(在哪里条款简单查看从表格中输入的文本框中的数据)

SELECT * FROM Module m
            INNER JOIN Class cl ON m.ModuleId = cl.ModuleId 
            JOIN Teacher t ON cl.TeacherId = t.TeacherId 
            JOIN Session s ON m.ModuleId = s.ModuleId
            JOIN Grade_Report gr ON s.SessionId = gr.SessionId
            JOIN Student st ON gr.StudentId = st.StudentId
            JOIN Course c ON st.CourseId = c.CourseId
          WHERE
            ('".mysql_real_escape_string($sessionid)."' = '' OR gr.SessionId = '".mysql_real_escape_string($sessionid)."')
          AND
            ('".mysql_real_escape_string($moduleid)."' = '' OR s.ModuleId = '".mysql_real_escape_string($moduleid)."')
          AND
            ('".mysql_real_escape_string($courseid)."' = '' OR c.CourseId = '".mysql_real_escape_string($courseid)."')
          AND
            ('".mysql_real_escape_string($classid)."' = '' OR cl.ClassId = '".mysql_real_escape_string($classid)."')
          AND
            ('".mysql_real_escape_string($teacherid)."' = '' OR t.TeacherUsername = '".mysql_real_escape_string($teacherid)."')
          AND
            ('".mysql_real_escape_string($studentid)."' = '' OR st.StudentUsername = '".mysql_real_escape_string($studentid)."')
          AND
            ('".mysql_real_escape_string($year)."' = '' OR st.Year = '".mysql_real_escape_string($year)."')
          AND
            ('".mysql_real_escape_string($grade)."' = '' OR gr.Grade = '".mysql_real_escape_string($grade)."')

2 个答案:

答案 0 :(得分:1)

您的架构看起来很好,但您的查询比必要的复杂得多。请尝试以下方法:

select *
from Student s, Class c, StudentClass sc
where s.StudentId = sc.StudentId
and c.ClassId = sc.ClassId

内部联接隐含在此查询中;它应该为您提供与StudentClass中的行对应的四行。请注意,你会看到Mayur Patel两次:周四在CHI2550一次,周一在CHT2520一次。

如果您想要基于表单字段进行其他过滤,那么这些只是额外的and子句。

答案 1 :(得分:0)

您的查询将如下所示:

select *
from Student as s
left join 
StudentClass as sc
on 
s.StudentId = sc.StudentId
right join 
Class as c
on
sc.ClassId = c.ClassId