E
E
Eugene2019-04-26 15:55:21
Laravel
Eugene, 2019-04-26 15:55:21

How to properly organize a one-to-many relationship?

Simple relationships are used throughout the mySQL project.
Example:
1. A table where "client types" are stored
2. A table with the clients themselves, there is a field - "Client type", (this field has a connection in mysql with another table by ID) referring to table No. 1. Everything is standard. One client can have one type.
And if I need, for example, that one client would have a connection with two types, with two records in table No. 1? How to do it right.?
I assume that you can make a field and store an array of IDs of these types there. But then there will be no more connections in mysql, and it is not clear how to then select clients with a certain type.
How is this generally implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-04-26
@atachrus

And if I need, for example, that one client would have a connection with two types, with two records in table No. 1?
This is a many-to-many relationship. Organized by an intermediate table, each record of which links a pair of the first and second main tables.
`clients` (`id`, ...)
`clientTypes` (`id`, ...)
`clientToClientType` (`client_id`, `clientType_id`)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question