0

I'm working on a small program for my school and I have a question about the following DB design.

The admin will assign subjects to teachers via an online form.

An example of a subject would be something like:

  • Subject Field 1: (Grade 5 Math: Teaching 5/1, 5/2 and 5/3) [Text Field]
  • Subject Field 2: (Grade 6 Math: Teaching 6/2 and 6/6) [Text Field]

I have added 7 subjects fields per their request as there won't be anyone that will exceed 7 subjects.

Each Subject Field will have required things to complete such as lesson plans, course syllabus etc...

Each requirement will have the following requirements:

  • Language (Language 1, Language 2 or Both) [Drop Down]
  • Type (Printed, File or Both) [Drop Down]
  • Time (Semester 1, Semester 2 or both) [Drop Down]

Here is a Visual

So far I have come up with this DB design:

  • ID (Primary, Auto Incremented)
  • TID (Teacher ID)
  • Year

  • Subject1

  • Subject1Requirement1
  • Subject1Requirement2
  • Subject1Requirement3
  • TimeSent
  • TimeReviewed

  • Subject2

  • Subject2Requirement1
  • Subject2Requirement2
  • Subject2Requirement3
  • TimeSent
  • TimeReviewed

  • Subject3

  • Subject3Requirement1
  • Subject3Requirement2
  • Subject3Requirement3
  • TimeSent
  • TimeReviewed

Continued to Subject 7.

I feel that there is a more efficient way of doing it but just can't think of a better way.

Thanks.

2 Answers 2

2

If there's no relationship between teacher's subjects, you can design 3 tables like the following

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

In such design

  • Each teacher can have multiple subjects (not limited to 7 subjects)
  • Each teacher's subject have multiple requirements (not limited to 5 requrirements)

Sample data

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');
--...
Sign up to request clarification or add additional context in comments.

Comments

0

On of the key aspects of database is having relationships (links) between tables - so that if a table (row) needs to refer to something else that is different then use another table and link together with foreign keys - this table, in our case TeacherSubject is a junction table

To give you a brief look of how I'd implement your requirements I think we need three tables, and the SubjectsTeacher below is a linking table to join the two.

You define each subject and teacher and then add entries to the TeacherSubject table to associate teachers with subjects.

enter image description here

To create the database:

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`)
);

Then add the foreign keys that tells the database how your fields are linked - this step is important as it will ensure that you maintain data integrity and cannot insert bad data into this data.

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

Then setup a few rows for test.

 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);

And finally to find out which subject teacher#1 has:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.