I have an example where I don't quite understand. What is the optimal way to solve it?
A table called usuarios
.
A table called publicaciones
.
If I plan to save the records of a post with that id
of the user who registered it; is the following structure correct?
But for example considering that an intermediate table called publicaciones-usuarios
with a structure similar to the following could be created:
What is the optimal way?, since for example if I make the first structure with a inner join
like the following:
SELECT titulo, cuerpo, username FROM publicaciones
INNER JOIN usuarios
ON usuarios.id = publicaciones.usuarios_id
WHERE usuarios.id = 2;
I can extract the publications that a user makes taking into account that the link between both tables is that id
of the user as a foreign key in the table publicaciones
; but I don't quite understand under what scenarios each one is used
It is just an example considering that I seek to clarify this doubt
the relationships themselves
The underlying question to answer your question is not whether using this type of relationship I can obtain such data, while using this other type of relationship I cannot obtain it .
Actually, the data can be obtained equally with any type of relationship, if they are well planned.
In short we can say that:
The one-to-many relationship
Applies only when:
Or vice versa:
The many-to-many relationship
Applies when:
Depending on how your application is set up, based on the reality of the data it handles , then you will have to decide which type of relationship to implement.
In other words, what is the reality of the data that your application is going to handle?:
The optimal mode
As for the optimal way to do things, as @gbianchi has said in his comment. The optimal mode is the one that optimizes what you want to do in the database, adhering to business rules.
It is not enough to have a type of relationship in accordance with the reality of the data you handle. There will still be loose ends that you must tie up in the definition of the tables, such as indexes to speed up data retrieval, control duplicate records, add referential integrity constraints that prevent the existence of orphan records, etc.
Get the data
Whatever relationship you use, you can always get the data you want from the tables.
Let's see:
Getting data in a one-to-many relationship
Getting data in a many-to-many relationship
In both examples the same columns are obtained. The only thing that varies is the way of obtaining the data by doing the
JOIN
corresponding, based on the way in which we have created the relations.What I mean is that you can't define a relation thinking that it has some implication on how you get the data . It has no implication. In both examples we get the same columns
nombre
from tableusuarios
andtitulo
from tablepublicaciones
. And so, we can get any column that participates in the relation.Another thing would be the fact of obtaining grouped data, for example in the case of many-to-many relationships. For that there are also grouping functions, such as
GROUP_CONCAT
, in the specific case of MySQL.