As mentioned by our colleague Alonso Carrasco to join two or more tables you need to create foreign keys, but if you say you're new, maybe with your comment it's not enough and you need an explanation.
HOW TO JOIN TWO TABLES
First, to relate tables, you have to be clear about how many types of relationships exist:
- One-to-one ratio (1: 1): this relationship occurs, for example, between two tables called
Usuario
and DNI
. A DNI
can only belong to a Usuario
and a Usuario
can only have a DNI
. So, in general, a foreign key of Usuario
to DNI
would be created, resulting in a new field in the DNI
table that would reference the user's id or vice versa, since in this case it would not matter where to put the outside key
- One to many relationship (1: n): in this relation the same thing happens as in 1: 1, since for example, between two tables called
Usuario
and Vehiculo
, it would happen that a Vehiculo
may have as owner at a Usuario
, but a Usuario
can have more than one Vehiculo
, it is related in the following way:
- Many to many relationship (n: m): this relationship is a bit more laborious, but it is just as simple to understand. We have two tables called
Asignatura
and Alumno
, so we think and say the magic formula, a Alumno
can have many Asignaturas
and a Asignatura
can have many Alumnos
, so this would be a case of relationship many to many. When you come across this relationship, remember that always you have to create an auxiliary table between the two, which collects the id of each table, just like in the following image:
The function of this auxiliary table is that it will collect the ids of each table and link them, for example, a record of this table is:
Student 1 and Subject 1.
Student 2 and Subject 1.
Student 1 and Subject 3.
And the two would be primary keys.
RELATING TABLES BY JUDGMENT
Once you have this clear, you just need to know how you can join these tables when you want to build a sentence. To join tables you can use the INNER JOIN, in the following way:
SELECT u.nombre, u.direccion, d.numero
FROM Usuario u
INNER JOIN DNI d
ON u.id = d.Usuario_id
WHERE u.id = 1
After adding FROM 'tabla1' 'acronimo1'
, you add INNER JOIN 'tabla a relacionar' 'acronimo' ON acronimo1.'campo enlazado' = acronimo2.'campo enlazado'. Siempre se tienen que enlazar los campos que sean el "mismo", es decir el
id de
User , siempre sera igual al campo
User_id de
DNI '.
In case you need to relate more than one table, just keep adding INNER JON ... ON ... = ...
, for example, assuming that the Usuario
table has a Vehiculo_id
field in relation to the id
of the Vehiculo
table:
SELECT u.nombre, u.direccion, d.titulo, v.marca
FROM Usuario u
INNER JOIN DNI d
ON u.id = d.Usuario_id
INNER JOIN Vehiculo v
ON u.Vehiculo_id = v.id
WHERE u.id = 1
You can also use different JOIN
, in addition to the INNER JOIN there is also the LEFT JOIN , RIGHT JOIN , FULL JOIN , etc ..
JOIN TWO SENTENCES
Anyway, in case you want to join two sentences that do not have any relation, I recommend you use UNION that allows you just this. For example, in your case, it could be:
SELECT region, etapa, linea FROM line
UNION
SELECT enero, febrero, marzo FROM tr;
I hope I have solved and clarified your doubts even more. If you need anything or do not understand something that I explain, please do not hesitate to share it with me. In addition, I encourage anyone who wants to add another case and thus have a more complete entry.
Good luck with your BD!