Comparison Datetime in scheduling in the database

Asked

Viewed 48 times

0

I need a light. I am creating a car travel scheduling system and I need that before including a new trip, it be checked whether the date and time of the trip and the date and time of the trip were no longer taken, using that car that’s like FK and that driver that’s like FK.

Or it is necessary that before the trip is included, know if that vehicle And the driver And the date and time are available.

1 answer

1


I will assume that :data_inicio and :data_fim be the timestamps of the trip you want to mark, with the id driver :idm and vehicle :idv.

I will also assume that your tables are viagem, motorista and viatura, whereas motorista and viatura have a field id each being a primary key and viagem have the fields id_motorista and id_veiculo. On the table viagem we also have the columns inicio and fim, which are timestamps. All these fields are NOT NULL.

SELECT g.*
FROM viagem g
INNER JOIN motorista m ON g.id_motorista = m.id
INNER JOIN viatura v ON g.id_viatura = v.id
WHERE (m.id = :idm OR v.id = :idv)
AND NOT (g.inicio > :data_fim)
AND NOT (g.fim < :data_inicio)

This query will bring as a result the trips that will be colliding with the one you want to register. It works looking for trips with:

  • The same driver OR the same car.

  • That do not start after the date/ time that ends the trip you want to register

  • That do not end before the date/ time that begins the trip you want to register.

These last two conditions are the most important part, because if the trip you want to register ends before starting already registered or begins after finishing already registered, then it does not collide. Otherwise, there must have been a collision of schedules.

  • Victor, your help, I’m already testing her, I’m in the 3 semester of college, loving it, but it’s hard to take this logic, thanks for the help. Since I’m doing this comic book-only routine, would you recommend a Trigger to treat this Insert? P.s. guy you gave a light at the end of the tunnel and it’s not a kkkkkk train

  • @Mauriciokalfelz No. I wouldn’t recommend using Rigger for that. It would probably work and would even be reasonably easy to do, but in general it is better to put the business rules in the application and leave the database only to store the data. Although you are doing the seminar only with the BD, there is no escape from the fact that a BD without an application does not have much use.

  • Victor, good morning, you guys are right even this should be done in the application, but as I said besides being a beginner, practically a baby in this area, I’m having a lot of trouble, and I’m going to need to create these rules within DB, Next semester’s seminar that we’re putting BD with the front, so I’m going to lay out the rules for the application. Thank you so much for your help, I’ll try to make the rest of Trigger

  • Victor took what you explained to me and tried to hit something, but it didn’t even quite right almost giving up

Browser other questions tagged

You are not signed in. Login or sign up in order to post.