1

I have table with tv shows, in this table there are columns, one for start time and one for finishing time. I need to make that if you want to insert tv show that you cannot put 2 tv shows in same time, like in real television.

Table tvshows have columns: tvshow_id, name, description, startime, finishingtime. I want to insert info via form, but if there is already tvshow which overlaps another tv show, you cannot insert this in database, you need to change time.

My try of solving this problem:

$sql = "SELECT ...";
$result = mysqli_query ($link, $sql);

if(mysqli_num_rows($result) > 0) {
        echo "Change datetime";
    } else {
    
        $sql = "INSERT ...";
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
iposave
  • 11
  • 4

1 Answers1

0

First: it is better to have Priority in your table, it helps you so much in ordering TV shows. You can control overlaps easily with this field.

Assume that you have:

tvshows table

tvshow_id, name, description, startime, finishingtime

and all your tvshows records inserted without overlap.

Your new record (with newStarttime and newFinishingtime) should not have overlap with any existed records.

Solution 1: Your new record should be external range of existing records range.

Select * from tvshows t
Where (newStarttime>=t.finishingtime AND newFinishingtime>t.finishingtime) OR
      (newFinishingtime<=t.starttime AND newStarttime<t.starttime )

Solution 2: So you should check these clauses:

  1. State 1: newStarttime should not be in the range of any records.

NOT(newStarttime>=t.strattime AND newStarttime<t.finihingtime)

  1. Satate 2: newFinishing time should not be in the range of any records.

NOT(newFinishing >t.strattime AND newFinishing <=t.finihingtime)

  1. State 3: your new range should not overlap the existing range

NOT(newStarttime<t.strattime AND newFinishing >t.finihingtime)

  • The problem isn't gaps, necessarily (although depending on what's happening here, it's certainly possible for there to be "no TV on right now"). Rather, what the OP is concerned about is having a 2-hour show that starts at 3... and a half-hour show that starts at 3:30. Uniqueness of starting time is unimportant, really. – Clockwork-Muse Dec 30 '17 at 17:11
  • @Clockwork-Muse i dont have a problem with that i need to get all time in day used etc. i just need to get it working when you want to insert info about tv show in database, if there is no tv show in this certain time, insert it in database, if there is show in that time, tell user to change date and time and thats all. – iposave Dec 30 '17 at 17:26
  • @Clockwork-Muse, you are right, I assume that is DateTime, Sorry –  Dec 30 '17 at 21:16
  • @DB-UML-Modeler thank you very much, can you please help me how to use this code with insert statement? I tried but it is not successfull. – iposave Dec 30 '17 at 23:19
  • @iposave, first select it, if there is not any results, you can insert it. If there is a overlaps, you can show them to user. –  Dec 30 '17 at 23:21
  • DB-UML-Modeler - false: if this database is shared, somebody else could insert into the database between the `SELECT` running and the `INSERT` being submitted - this must be part of a trigger. @iposave - the condition for the check [is actually much simpler than this](https://stackoverflow.com/a/325964/812837), although that's not the trigger, obviously – Clockwork-Muse Dec 31 '17 at 01:54
  • @DB-UML-Modeler I edited my question, i tried some code but there is no success, can you please help me solve problem?, or if there is better way to do this please help. – iposave Dec 31 '17 at 14:44
  • @Clockwork-Muse, do you know Transactions? Our databases are always SHARED. we should handle it with transaction techniques. Like ticketing problems. –  Dec 31 '17 at 20:09
  • @iposave, Now, your problem is not SQL, it's PHP. My profession is Java. –  Dec 31 '17 at 20:10
  • 1
    Okay, you didn't mention that in your answer, though, which would be important information. Note that you'd need at least `REPEATABLE READ` for write access to this table, for all statements that update the date/time. And you have to check before you update that row, too, which is easy to forget (which is why the trigger - the database is enforcing the constraint, not requiring the application to remember. And it also protects against adhoc updates). This would be much easier if MySQL actually executed check constraints... – Clockwork-Muse Jan 01 '18 at 07:57