r/SQL • • Jun 19 '19

PostgreSQL Store student timetables in PostgreSQL

For a project I'm working on, I need to store the course timetables of the students in my college in a PostgreSQL database (this is not my decision).

Some Background:

  • College has around 3000 students and due to the freedom in course selection, we might as well as assume each student might have a unique course list.
  • We have names for certain slots (let's say for example, Weekdays 9AM-10AM is slot A). Most courses follow these slots, but others have custom timings with classes of variable lengths of time.
  • While it is uncommon, some courses have two classes on the same day. By class, I mean a single continuous chunk of time a student has to be present in the classroom.
  • I will also have to store the course's start and end dates.

My ideas:

  • A table for the schedule of all courses and tables listing courses for each student.

    • One hack for storing the course schedule for a week was to split it into half an hour intervals and store a string with binary values of whether a class is there in that period.
    • Another way of storing the course schedule was to have a Day Start Time and Day End Time for each day. To work around have multiple classes on the same day, have Day Start Time 1, etc.

I see both the methods as hacky and was hoping someone could suggest a schema (or give me hints/resources to learn making one my own). It would be really nice if it used the built-in types in the database instead of parsing through strings.

I'm new to working with a SQL database and relatively new to Development, so my apologies if I have missed anything. I'm also not sure if a support request such as this is allowed here, but I couldn't find anything describing a solution I can adapt elsewhere and do not know where else I can ask.

Thank you.

13 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/da_chicken Jun 20 '19

For us each each Semester is broken into 3 Segments and there is an add/drop period for trying out courses. So I'm looking at generating the timetables at least six times a year and easily more than that. The only consolation is that I won't have to do it at the same time for all the students.

The add/drop period would just be an operating rule. The system doesn't need to know everything, necessarily. You would write reports that your staff would have to look at to determine the number of days or weeks they were enrolled in the class. You could certainly do it programmatically, but that would be an additional application feature.

I guess I'm not entirely sure what you mean by a timetable.

I'm not quite sure I understand how this stores the multiple classes per day for a course, possibly due to the different terminology. I would be grateful if you could explain it further.

Okay, so here's roughly what the tables look like. I'm leaving some fields out because there's additional features and complexity that would just confuse things (e.g., the system can support multiple districts, summer school is handled differently, etc.).

(Warning: This is pseudocode based on SQL Server. I don't expect the syntax to work, particularly with foreign keys.)

create table schd_master as (
    school_year smallint not null
    ,building int not null
    ,course nvarchar(10) not null
    ,course_section smallint not null       -- This is just a sequence number to idenitify a section
    ,section_key int identity(1,1) not null -- The artificial key to reduce data duplication
    ,course_name nvarchar(100) not null
    ,maximum_seats smallint not null
    ,department nvarchar(5) null
    ,primary key (school_year, building, course, course_section)
    ,unique key (section_key)
)

create table schd_master_session as (
    section_key int foreign key references schd_master (section_key)
    ,course_session smallint not null
    ,session_name nvarchar(100) not null
    ,start_time time not null                 -- Our system uses periods, but you can use time
    ,end_time time not null
    ,credit decimal(6,4) not null
    ,room_id nvarchar(10) not null            -- Room information like size stored in another table
    ,take_attendance nvarchar(1) not null     -- Yes/No
    ,primary key (section_key, course_session)
)

create table schd_master_staff as (
    section_key int not null
    ,course_session smallint not null
    ,staff_id nvarchar(10) not null           -- Staff information stored elsewhere
    ,primary key (section_key, course_session, staff_id)
    ,foreign key (section_key, course_session) references schd_master_session (section_key, course_session)
)

create table schd_master_mp as (
    section_key int not null
    ,course_session smallint not null
    ,marking_period nvarchar(5) not null -- one row each marking period
)

create table schd_master_days as (
    section_key int not null
    ,course_session smallint not null
    ,day_code nvarchar(1) not null        -- one row each for MTWRF
)

So, let's say we have an COMP101 course called "Database Design". The class meetsin building 22 on MWR 9:00 to 10:30 in room 330, and there's a lab section from 16:00 to 18:30 every F in the same room. It's a first semester class. You say that semester are divided into three. We call the divisions marking periods. So marking periods M1, M2, and M3. Second semester would be M4, M5, and M6. The teacher for the lectures has an ID of 2129, and the TA for the lab has an ID of 5642. Let's say that the lab doesn't get a separate mark on your report card.

So, to represent our course:

insert into schd_master (school_year, building, course, course_section, course_name, maximum_seats, department)
output inserted.section_key
values (2019, 22, 'COMP101', 1, 'Database Design', 24, 'CS')

The artifical section_key is determined automatically. You'll have to fetch it back by querying the primary key or output it as I've done. I'll use @section_key to indicate the placeholder.

Now we create a session for each meeting time for the class:

insert into schd_master_session (section_key, course_session, session_name, start_time, end_time, credit, room_id, take_attendance)
values (@section_key, 1, 'Database Design Lecture', '9:00', '10:00', 4, '330', 'Y')
        ,(@section_key, 2, 'Database Design Lab', '16:00', '18:30', 0, '330', 'N')

Here I put 4 credits on the lecture and none on the lab. There's attendance taken on the lecture, but not the lab.

Now we can add the staff. Let's say we know the professor might go to the lab sometimes so we want him and the TA as teacher on the lab session:

insert into schd_master_staff (section_key, course_session, staff_id)
values (@section_key, 1, '2129')
        ,(@section_key, 2, '2129')
        ,(@section_key, 2, '5642')

And specify the marking periods the class will meet. The start and end dates of the marking periods are defined elsewhere, as are which marking periods belong to which semester, when marks are issued, etc. For a real system, there's a lot of supporting data that determines your configuration.

insert into schd_master_mp (section_key, course_sesion, marking_period)
values (@section_key, 1, 'M1')
        ,(@section_key, 1, 'M2')
        ,(@section_key, 1, 'M3')
        ,(@section_key, 2, 'M1')
        ,(@section_key, 2, 'M2')
        ,(@section_key, 2, 'M3')

And specify the days that the classes will meet. Again, the days that are available to select are defined elsewhere.

insert into schd_master_days (section_key, course_sesion, day_code)
values (@section_key, 1, 'M')
        ,(@section_key, 1, 'W')
        ,(@section_key, 1, 'R')
        ,(@section_key, 2, 'F')

There would be more tables for report card information, but I haven't got any more time today to fully explain that.