Search code examples
mysqlsqljakarta-eedatabase-designtimetable

Database Design for Time Table Generation


I am doing a project using J2EE(servlet) for Time Table Generation of College. There are Six Slots(6 Hours) in a Day

    4 x 1 HR Lectures
    1 x 2 HR Lab

There Are three batches ( 3IT, 5IT, 7IT)

   2 Classroom 
   1 LAB

Each slot in the time table will have

       (Subject,Faculty)

For Lab I will duplicate the slot. The Tables

    Subject(SubjectID  INT, SubjectName VARCHAR);
    Faculty(FacultyID INT,FacultyName VARCHAR,NumOfSub INT,Subjects  XYZ);

Here I am not able to decide the DATATYPE for subject. What should I do ? Since a faculty can teach multiple subjects ? Also how to link with Subject Table ?

P.S. Using MySQL Database


Solution

  • You don't want to actually store either NumOfSub (number of subjects) OR Subjects in Faculty. Storing subjects that way is a violation of First Normal Form, and dealing with it would cause major headaches.
    Instead, what you want is another table:

    FacultySubject
    ----------------
    FacultyId  -- fk for Faculty.FacultyId
    SubjectId  -- fk for Subject.SubjectId
    

    From this, you can easily get the count of subjects, or a set of rows listing the subjects (I believe MySQL also has functions to return a list of values, but I have no experience with those):
    This query will retrieve the count of Subjects taught by a particular teacher:

    SELECT Faculty.FacultyId, COUNT(*)
    FROM Faculty
    JOIN FacultySubject
      ON FacultySubject.FacultyId = FacultyId.FacultyId
    WHERE Faculty.FacultyName = 'Really Cool Professor'
    GROUP BY Faculty.FacultyId
    

    ... and this query will get all the subjects (named) that they teach:

    SELECT Subject.SubjectId, Subject.SubjectName
    FROM Faculty
    JOIN FacultySubject
      ON FacultySubject.FacultyId = FacultyId.FacultyId
    JOIN Subject
      ON Subject.SubjectId = FacultySubject.SubjectId
    WHERE Faculty.FacultyName = 'Really Cool Professor'
    

    (note that this last returns the subjects as a set of rows ie:

    SubjectId    SubjectName
    =========================
    1            Tree Houses
    2            Annoying Younger Sisters
    3            Swimming Holes
    4            Fishing
    

    )