esta fue mi respuesta muy simplificada pero usando algo de logica simple
-- Tu código aquí 👇SELECT * FROM lineas;SELECT * FROM estaciones;
CREATE TABLE linea_estaciones ( linea_id int not null, estacion_id int not null, foreign key (linea_id) references lineas(id), foreign key (estacion_id) references estaciones(id)
);
insert into linea_estaciones (linea_id, estacion_id)select 1, id from estaciones where inaguracion > '2026-06-00'
union all
select 2, id from estaciones where longitud_km > 3.5
union all
select 3, id from estaciones where id < 6;select * from linea_estaciones;
Seria metro de Medellín, ya que Bogotá no tiene todavía
A que se refiere la longitud de las estaciones? No deberia ser longitud de las lineas?
Estás en lo correcto.
Mi Solucion:
--Tu código aquí 👇
SELECT*FROM lineas;SELECT*FROM estaciones;CREATETABLE`linea_estaciones`( id BIGINT(20)NOTNULL, linea_id BIGINT(20), estacion_id BIGINT(20),CONSTRAINT lineas_estaciones_linea_id_foreing FOREIGNKEY(linea_id)REFERENCESlineas(id),CONSTRAINT lineas_estaciones_estacion_id_foreign FOREIGNKEY(estacion_id)REFERENCESestaciones(id));INSERTINTOlinea_estaciones(id, linea_id, estacion_id)VALUES(1,1,1),(2,1,2),(3,1,8),(4,1,10),(5,1,15),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 6")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 7")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 9")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 10")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 11")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 12")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 13")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 14")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 15")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 2"));SELECT*FROM linea_estaciones
```-- Tu código aquí 👇SELECT \* FROM lineas;SELECT \* FROM estaciones;
CREATE TABLE `linea\_estaciones` ( id BIGINT(20)NOTNULL, linea\_id BIGINT(20), estacion\_id BIGINT(20),CONSTRAINT lineas\_estaciones\_linea\_id\_foreing FOREIGNKEY(linea\_id)REFERENCESlineas(id),CONSTRAINT lineas\_estaciones\_estacion\_id\_foreign FOREIGNKEY(estacion\_id)REFERENCESestaciones(id));INSERTINTO linea\_estaciones(id, linea\_id, estacion\_id)VALUES(1,1,1),(2,1,2),(3,1,8),(4,1,10),(5,1,15),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 6")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 7")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 9")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 10")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 11")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 12")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 13")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 14")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 15")),(6,(SELECT lineas.idFROM lineas WHERE nombre ="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE nombre ="Estacion 2"));SELECT \*FROM linea\_estaciones
--Creando la tabla
CREATE TABLE 'linea_estaciones'(id BIGINT(20) NOT NULL,linea_id BOGINT(20) NOT NULL,estacion_id BIGINT(20) NOT NULL,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,update_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (id) CONSTRAINT linea_estaciones_linea_id_foreign FOREIGN KEY(linea_id) REFERENCES lineas(id),CONSTRAINT lineas_estaciones_estacion_id_foreign FOREIGN KEY(estacion_id)REFERENCES estaciones(id));
--Insertando la información
INSERT INTO linea_estaciones (id, linea_id, estacion_id)VALUES (2, ( SELECT id FROM lineas WHERE color = "Azul" ), ( SELECT id FROM estaciones WHERE inaguracion = "2026-09-08" )), (3, ( SELECT id FROM lineas WHERE color = "Azul" ), ( SELECT id FROM estaciones WHERE inaguracion = "2025-06-12" )), (4, ( SELECT id FROM lineas WHERE color = "Azul" ), ( SELECT id FROM estaciones WHERE inaguracion = "2025-12-01" )), (5, ( SELECT id FROM lineas WHERE color = "Azul" ), ( SELECT id FROM estaciones WHERE inaguracion = "2027-03-19" )), (6, ( SELECT id FROM lineas WHERE color = "Amarillo" ), ( SELECT id FROM estaciones WHERE inaguracion = "2026-01-09" )), (7, ( SELECT id FROM lineas WHERE color = "Rojo" ), ( SELECT id FROM estaciones WHERE inaguracion = "2027-08-11" ));
hay que hacer 5 líneas y cada linea debe tener 5 registros para un total de 25, que no permite autoincrement se coloca el id en los registro. aclaro que así lo hice yo.
INSERT INTO linea_estaciones (`id`, `linea_id`, `estacion_id`) VALUES
(1,1,1),
(2,1,2),
(3,1,3),
(4,1,4),
(5,1,5),
(6,2,1),
(7,2,2),
(8,2,3),
(9,2,4),
(10,2,5);
Esta es mi solución, intente usar los operadores lógicos como lo recomendaban para múltiples ingresos al mismo sin embargo como mencione el resultado fue diferente al esperado.
CREATETABLElinea_estaciones( linea_id INTNOTNULL, estacion_id INTNOTNULL,FOREIGNKEY(`linea_id`)REFERENCES`lineas`(`id`),FOREIGNKEY(`estacion_id`)REFERENCES`estaciones`(`id`));INSERTINTOlinea_estaciones(linea_id, estacion_id)VALUES((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 1")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 2")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 3")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 4")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 5")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 6")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 7")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 8")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 9")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 10")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 11")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 12")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 13")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 14")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estacion 15"));SELECT*FROM linea_estaciones;
-- Asegúrate de que cada línea tenga al menos 5 estaciones.
-- Puedes ajustar estas asignaciones según tus necesidades.
-- Línea 1 - Asignar estaciones 1 a 5
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 1, id
FROM estaciones
WHERE id BETWEEN 1 AND 5;
-- Línea 2 - Asignar estaciones 6 a 10
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 2, id
FROM estaciones
WHERE id BETWEEN 6 AND 10;
-- Línea 3 - Asignar estaciones 11 a 15
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 3, id
FROM estaciones
WHERE id BETWEEN 11 AND 15;
-- Línea 4 - Asignar estaciones 16 a 20
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 4, id
FROM estaciones
WHERE id BETWEEN 16 AND 20;
-- Línea 5 - Asignar estaciones 21 a 25
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 5, id
FROM estaciones
WHERE id BETWEEN 21 AND 25;
-- Línea 6 - Asignar estaciones 26 a 30
INSERT INTO linea_estaciones (linea_id, estacion_id)
SELECT 6, id
FROM estaciones
WHERE id BETWEEN 26 AND 30;
Por alguna razón, no me aceptó los comandos NOT NULL ni los constraint asi que tiene que ser bastante simple la creación de la tabla, esta fue mi solución después de tanto:
<CREATETABLElinea_estaciones( linea_id INT, estacion_id INT,FOREIGNKEY(linea_id)REFERENCESlinea(id),FOREIGNKEY(estacion_id)REFERENCESestaciones(id));INSERTINTOlinea_estaciones(linea_id, estacion_id)VALUES((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 1")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 2")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 3")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 4")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 5")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 6")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 7")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 8")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 9")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 10")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 1")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 2")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 3")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 4")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 5")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 4"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 6")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 4"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 7")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 4"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 8")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 4"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 9")),((SELECT lineas.idFROM lineas
WHERE lineas.nombre="Linea 4"),(SELECT estaciones.idFROM estaciones
WHERE estaciones.nombre="Estación 10"));>
--Tu código aquí 👇
--Crear la tabla pivote linea_estaciones
CREATETABLElinea_estaciones( linea_id INT, estacion_id INT,FOREIGNKEY(linea_id)REFERENCESlineas(id),FOREIGNKEY(estacion_id)REFERENCESestaciones(id));--Agregar relaciones entre estaciones y líneas
INSERTINTOlinea_estaciones(linea_id, estacion_id)VALUES((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 1")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 2")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 3")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 4")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 5")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 6")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 7")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 8")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 9")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 10")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 11")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 12")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 13")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 14")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 15"));
el código está insertando manualmente relaciones entre las líneas y las estaciones en la tabla pivote linea_estaciones utilizando subconsultas para obtener los IDs de las tablas lineas y estaciones según los nombres especificados. Esto asegura que cada línea tenga al menos 5 estaciones conectadas. Sin embargo, este enfoque puede ser poco escalable si hay muchas líneas y estaciones, ya que se requiere una inserción manual para cada combinación.
Si se pudo 🥳🥳🥳🥳
--Tu código aquí 👇
--SELECT*FROM lineas;--SELECT*FROM estaciones;CREATETABLElinea_estaciones( linea_id BIGIBT(20)NOTNULL, estacion_id BIGIBT(20)NOTNULL,CONSTRAINT`linea_estaciones_linea_id_foreign`FOREIGNKEY(`linea_id`)REFERENCES`lineas`(`id`),CONSTRAINT`linea_estaciones_estacion_id_foreign`FOREIGNKEY(`estacion_id`)REFERENCES`estaciones`(`id`));INSERTINTOlinea_estaciones(linea_id, estacion_id)VALUES((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 1")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 2")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 3")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 4")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 1"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 5")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 6")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 7")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 8")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 9")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 2"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 10")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 11")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 12")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 13")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 14")),((SELECT lineas.idFROM lineas WHERE lineas.nombre="Linea 3"),(SELECT estaciones.idFROM estaciones WHERE estaciones.nombre="Estacion 15"));