[英]Is this database structure correct or is there a better way?
I have got three tables, Student, StudentClass and Class table. 我有三个表,Student,StudentClass和Class表。 The Student Table shows Student Information, The class Table shows the classes that goes on in the university and The StudentClass table links the students to their classrooms.
学生表显示学生信息,班级表显示大学中正在进行的课程,而学生班级表将学生链接到他们的教室。 Problem is though that if I create a query I get duplicate rows in the query if I join all three table.
问题是,如果我创建一个查询,如果我同时连接所有三个表,则会在查询中得到重复的行。 So what my question is that am I right to create a student and classroom database like this with these three tables or is there a more efficient and better way of doing this.
所以我的问题是,我是否应该使用这三个表来创建像这样的学生和教室数据库,还是有一种更有效,更好的方法来做到这一点? There are other tables but I want you to just concentrate on these three tables.
还有其他表格,但我希望您仅关注这三个表格。 Thank You
谢谢
Below are the tables: 下表是:
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
Query Below: (THERE WHERE CLAUSE SIMPLY LOOKS FOR DATA ENTERED IN TEXTBOXES FROM A FORM) 在下面查询:(在哪里从表格中简单查找在文本框中输入的数据)
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)."')
Your schema looks fine, but your query is much more complicated than necessary. 您的架构看起来不错,但是查询比必要的要复杂得多。 Try the following:
请尝试以下操作:
select *
from Student s, Class c, StudentClass sc
where s.StudentId = sc.StudentId
and c.ClassId = sc.ClassId
The inner joins are implicit in this query; 内部联接在此查询中是隐式的; it should give you four rows corresponding to the rows in
StudentClass
. 它应该给你四行对应于
StudentClass
的行。 Note that you'll see Mayur Patel twice: once in CHI2550 on Thursday, and once in CHT2520 on Monday. 请注意,您将两次看到Mayur Patel:一次是在周四的CHI2550中,一次是在星期一的CHT2520中。
If you want other filtering based on form fields, those will just be extra and
clauses. 如果要基于表单字段进行其他过滤,则这些过滤器将是多余的
and
子句。
Your query will look something like this: 您的查询将如下所示:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.