简体   繁体   中英

SQL Scheduling System

I want to create an Exam Scheduling system. now my objective is to avoid the user to insert a conflict time schedule for the same room in SQL Server 2008. for example:

for room 202 there's a schedule of

02/27/2017 4:00pm - 5:00pm

so what if the user input for room 202 is

02/27/2017 3:50pm - 5:10pm

obviously this is conflict.

if I'm going to use between obviously it will not work for the given example input.

Kindly help. Thanks

Like Nick said, between is exactly what you need. In conjunction with exists or not exists in this case, you will be able to avoid schedule conflict. You do the insert only if there is no entry in schedule for the same room, for the same day, that have a starting or ending time that is between the new time slot (which indicated a conflict).

Look at this example :

SQL Fiddle

MS SQL Server 2008 Schema Setup :

CREATE TABLE schedule
    ([room] int, [date] date, [start] time, [finish] time)
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '16:00', '17:00'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND 
                   (start BETWEEN '16:00' AND '17:00'
                    OR finish BETWEEN '16:00' AND '17:00'))
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '15:50', '17:10'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND 
                   (start BETWEEN '15:50' AND '17:10'
                    OR finish BETWEEN '15:50' AND '17:10'))
;

INSERT INTO schedule
    ([room], [date], [start], [finish])
SELECT  202, '2017-02-26', '15:00', '15:59'
WHERE NOT EXISTS (SELECT * 
                  FROM schedule 
                  WHERE room = '202' AND date = '2017-02-26' AND
                   (start BETWEEN '15:00' AND '15:59'
                    OR finish BETWEEN '15:00' AND '15:59'))
;

Query 1 :

SELECT  *
FROM schedule

Results :

| room |       date |            start |           finish |
|------|------------|------------------|------------------|
|  202 | 2017-02-26 | 16:00:00.0000000 | 17:00:00.0000000 |
|  202 | 2017-02-26 | 15:00:00.0000000 | 15:59:00.0000000 |

In an application, make it look like this :

Pseudo :

SELECT count(*)
FROM schedule 
WHERE room = '202' AND date = '2017-02-26' AND
      (start BETWEEN '15:00' AND '15:59'
       OR finish BETWEEN '15:00' AND '15:59')

if count(*) = 0 then
     INSERT INTO schedule
       ([room], [date], [start], [finish])
     SELECT  202, '2017-02-26', '15:00', '15:59'
else
    ERROR "CONFLICT WITH ANOTHER SCHEDULE"
end if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM