Answer the question
In order to leave comments, you need to log in
Relationship between tables (Master-Slave). How to connect correctly?
When designing a database, on what basis is the main-table and the subordinate-table selected?
For example, we establish a connection between tables: Car <-> Driver. Who will have the primary key and who will have the secondary?
Answer the question
In order to leave comments, you need to log in
The "driver" and "car" tables most likely have a many-to-many relationship defined through an intermediate table.
In general, there are no "master" and "subordinate" tables in the database, there are links between tables.
For example, tables "apartments" and "people".
apartments <= owner =>
apartment people == responsible tenant =>
apartment people <= registration == people
If the driver can have several cars, then Автомобили
add the field to the table dirver_id
.
If the driver can only have one car. Водители
then we add a field to the table car_id
.
If a driver can have many cars, and a car can have many drivers, then we create a table Автомобили_Водители
in which there will be two fields car_id
and driver_id
.
This is how I tried to tell you about the types of connections , using an example . It all depends on the specific task.
See how you want.
You can create a one-to-many or one-to-one relationship by designating a table as the main table Водитель
, in which you specify the table's primary key as the secondary key Автомобиль
. Or vice versa, Автомобиль
the main table, a one-to-many or one-to-one relationship , the secondary key is the table's primary key Водитель
.
You can создать третью таблицу
, with your primary key, a many-to-many relationship , store primary keys of tables Водитель
and as secondary keys Автомобиль
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question