A
A
Artem2019-04-12 07:54:02
Database design
Artem, 2019-04-12 07:54:02

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

3 answer(s)
R
Rsa97, 2019-04-12
@ArtemITS

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

L
Lander, 2019-04-12
@usdglander

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_idand 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.

I
irishmann, 2019-04-12
@irishmann

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 question

Ask a Question

731 491 924 answers to any question