A
A
Ayan Bai2016-08-13 07:00:43
MySQL
Ayan Bai, 2016-08-13 07:00:43

How to bind a car to a driver on a SQL server database?

Good day to all!
I rewrote my project from mean.js with the mongodb database to golang with the mySQL database, I asked the project questions earlier
I have been working on the project for a year, there is no budget for freelance, so I write it myself as best I can.
Indeed, with mySQL it is much easier, and most importantly, familiar.
But I have a problem here again, I once worked SQL for 2-3 years, pulled out simple reports, did simple update, delete and add operations directly in SQL. But I never tried to develop on it from scratch. Now again here I can not correctly implement the function of binding a car to a driver.
Essence of the question:
There is a taxi service with its own fleet, drivers in the state. You need to tie the car to the driver. This function must meet the following requirements:
- The driver cannot drive two cars at the same time;
- Auto cannot be linked to 2 drivers.
I created a carsDrivers table from the cars and drivers table, it contains the field trace:
- id;
- driver_id;
- car_id;
- link_date (link date).
- description (driver.mark +' '+driver.number+').
I'm going to pick up the current bundle on the following request:

SELECT * FROM car_driver 
Where driver_id = 2
Order by link_date desc
LIMIT 1

But here I have such a problem, I do not know how to ensure integrity (if I may say so), so that the table meets my requirements.
I can't even formulate the question correctly. Friends help me understand

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2016-08-13
@DmitriyEntelis

1) If the request is frequent, and the change of drivers is rare - store who is tied to which car right now explicitly. Additionally, for history, store your plate.
2) If the change of drivers is frequent (and it happens in a taxi) - it is logical to store not only the start time, but also the end time of the binding, at the application logic level, make sure that the intervals do not intersect.

A
Alexander Zolotykh, 2016-08-13
@zolotykh

An auxiliary table is not needed if the binding is 1 to 1 for all time. Store the car id separately as written or in the drivers table.
But it’s more correct to allow the possibility of transferring the car to other hands (transition of the driver to another car), then your created table is suitable. And store information about the car in a "broken" form.

V
Vladimir, 2016-08-13
@rostel

the driver and one of his cars at any time make up the crew,
respectively, store this relationship in a separate table
when changing the car, update the crew composition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question