高效的MySQL数据库设计

时间:2012-07-02 08:06:31

标签: mysql database-design

我正在为我的学校开设一个小程序,我对以下数据库设计有疑问。

管理员将通过在线表格将主题分配给教师。

主题的一个例子是:

  • 主题领域1 :( 5年级数学:教学5 / 1,5 / 2和5/3)[文本字段]
  • 主题领域2 :( 6年级数学:教学6/2和6/6)[文本字段]

我已根据他们的要求添加了7个主题字段,因为不会有任何人超过7个主题。

每个学科领域都需要完成所需的课程,如课程计划,课程大纲等......

每项要求都有以下要求:

  • 语言(语言1,语言2或两者)[下拉]
  • 类型(打印,文件或两者)[下拉]
  • 时间(第一学期,第二学期或两者)[下拉]

Here is a Visual

到目前为止,我已经提出了这个数据库设计:

  • ID(主要,自动增加)
  • TID(教师ID)
  • Subject1

  • Subject1Requirement1
  • Subject1Requirement2
  • Subject1Requirement3
  • TimeSent
  • TimeReviewed

  • Subject2

  • Subject2Requirement1
  • Subject2Requirement2
  • Subject2Requirement3
  • TimeSent
  • TimeReviewed

  • Subject3

  • Subject3Requirement1
  • Subject3Requirement2
  • Subject3Requirement3
  • TimeSent
  • TimeReviewed

继续主题7。

我觉得有一种更有效的方式,但却想不出更好的方法。

感谢。

2 个答案:

答案 0 :(得分:2)

如果教师的科目之间没有任何关系,您可以设计3个表格,如下所示

Teachers    TeacherSubjects SubjectRequirements
----------  --------------- --------------------
ID          SubjectID ----> SubjectID
TID --\     SubjectName     SubjectRequirement
Year   \--> TID             Language
            TimeSent        Type
            TimeReviewed    Time

在这样的设计中

  • 每位教师可以有多个科目(不限于7科目)
  • 每位教师的科目都有多项要求(不限于5项要求)

示例数据

INSERT INTO Teachers(ID, TID, Year) VALUES (1,'LiuYan', 2012);
INSERT INTO Teachers(ID, TID, Year) VALUES (2,'Emily',  2012);

INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_1', 'SubjectName1', 'LiuYan');
INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_2', 'SubjectName2', 'LiuYan');
-- ...
INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_N', 'SubjectNameN', 'LiuYan');

INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_N+1', 'SubjectName N+1', 'Emily');
INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_N+2', 'SubjectName N+2', 'Emily');
-- ...
INSERT INTO TeacherSubjects (SubjectID, SubjectName, TID) VALUES ('SubjectID_M', 'SubjectName M', 'Emily');

INSERT INTO SubjectRequirements (SubjectID, SubjectRequirement, Language, Type, Time) VALUES ('SubjectID_1', 'Curriculum', 'Language 1', 'Printed', 'Semester 1');
INSERT INTO SubjectRequirements (SubjectID, SubjectRequirement, Language, Type, Time) VALUES ('SubjectID_1', 'Course Syllabus', 'Language 2', 'File', 'Semester 2');
INSERT INTO SubjectRequirements (SubjectID, SubjectRequirement, Language, Type, Time) VALUES ('SubjectID_1', 'Learning Management', 'Both Language', 'Both Type', 'Both Semester');
--...

INSERT INTO SubjectRequirements (SubjectID, SubjectRequirement, Language, Type, Time) VALUES ('SubjectID_N+1', 'Curriculum', 'Language 2', 'Both Type', 'Semester 2');
--...

答案 1 :(得分:0)

关于数据库的关键方面是在表之间有relationships(链接) - 因此如果表(行)需要引用其他不同的东西,那么使用另一个表并与{{{ 3}} - 这个表,在我们的例子中,TeacherSubject是foreign keys

为了简要介绍一下我如何实现您的要求,我认为我们需要三个表,下面的SubjectsTeacher是一个连接两个表的链接表。

您定义每个科目和教师,然后将条目添加到TeacherSubject表,以将教师与科目相关联。

enter image description here

创建数据库:

CREATE TABLE `Subject` (
`SID` INTEGER NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(100) NOT NULL ,
`Curriculum` INTEGER NOT NULL ,
`Syllabus` INTEGER NOT NULL ,
`LearnManagement` INTEGER NOT NULL ,
`Individual Analysis` INTEGER NOT NULL ,
PRIMARY KEY (`SID`)
);

CREATE TABLE `Teachers` (
`TID` INTEGER NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`TID`)
);

CREATE TABLE `TeacherSubject` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`SID_Subject` INTEGER NOT NULL ,
`TID_Teachers` INTEGER NOT NULL ,
PRIMARY KEY (`id`)
);

然后添加告诉数据库如何链接字段的外键 - 这一步很重要,因为它将确保您保持数据完整性并且不能将错误数据插入到此数据中。

ALTER TABLE `TeacherSubject` ADD FOREIGN KEY (SID_Subject) REFERENCES `Subject` (`SID`);
ALTER TABLE `TeacherSubject` ADD FOREIGN KEY (TID_Teachers) REFERENCES `Teachers` (`TID`);

然后设置几行进行测试。

 INSERT INTO `Subject` (`SID`, `Name`, `Curriculum`, `Syllabus`, `LearnManagement`, `Individual Analysis`) VALUES
    (1, 'Subject1', 1, 2, 3, 4),
    (2, 'Subject1', 1, 2, 3, 4),
    (3, 'Subject2', 1, 2, 3, 4),
    (4, 'Subject3', 1, 2, 3, 4),
    (5, 'Subject4', 1, 2, 3, 4);
INSERT INTO `Teachers` (`Name`) VALUES ('Teacher 1');
INSERT INTO `Teachers` (`Name`) VALUES ('Teacher 2');

INSERT INTO `TeacherSubject` (`id`, `SID_Subject`, `TID_Teachers`) VALUES
    (1, 1, 1),
    (2, 2, 1),
    (3, 3, 2),
    (4, 4, 2),
    (5, 1, 2);

最后找出老师#1的主题:

 select * from TeacherSubject
INNER JOIN Teachers on TID=TID_Teachers
WHERE TID_Teachers=1