1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| create table IT_itineraire
( IT_ident integer auto_increment primary key
, IT_code char(4) not null unique
, IT_nom varchar(128) not null
)
;
create table VI_ville
( VI_ident integer auto_increment primary key
, VI_insee char(5) not null
, VI_nom varchar(50) not null
)
;
create table ET_etape
( IT_ident integer not null
, ET_num smallint not null
, VI_ident integer not null
, constraint ETPK
primary key (IT_ident, ET_num)
, constraint ETFK1
foreign key (IT_ident)
references IT_itineraire(IT_ident)
, constraint ETFK2
foreign key (VI_ident)
references VI_ville(VI_ident)
)
;
insert into VI_ville
(VI_insee, VI_nom)
values ('33063', 'Bordeaux')
, ('29019', 'Brest')
, ('17300', 'La Rochelle')
, ('64122', 'Biarritz')
, ('64260', 'Hendaye')
, ('64485', 'Saint-Jean-Pied-de-Port')
;
select * from VI_ville
;
insert into IT_itineraire
(IT_code, IT_nom)
values ('ATL1', 'La côte atlantique du nord au sud')
, ('ATL2', 'La côte atlantique du sud au nord')
, ('TBPA', 'Le tour du Pays Basque')
;
select * from IT_itineraire
;
insert into ET_etape
(IT_ident, ET_num, VI_ident)
values (1, 1, 2)
, (1, 2, 3)
, (1, 3, 1)
, (1, 4, 4)
, (1, 5, 5)
, (2, 1, 5)
, (2, 2, 4)
, (2, 3, 3)
, (2, 4, 1)
, (2, 5, 2)
, (3, 1, 5)
, (3, 2, 4)
, (3, 3, 6)
, (3, 4, 5)
;
select IT.IT_code as "Ref"
, IT.IT_nom as "Itinéraire"
, ET.ET_num as "n° etape"
, VI.VI_nom as "Ville"
from IT_itineraire as IT
inner join ET_etape as ET
on ET.IT_ident = IT.IT_ident
inner join VI_ville as VI
on VI.VI_ident = ET.VI_ident
order by IT.IT_code
, ET.ET_num
;
-- recherche des itinéraires passant à la fois par Biarritz et La Rochelle
select IT.IT_code as "Ref"
, IT.IT_nom as "Itinéraire"
, ET.ET_num as "n° etape"
, VI.VI_nom as "Ville"
from IT_itineraire as IT
inner join ET_etape as ET
on ET.IT_ident = IT.IT_ident
inner join VI_ville as VI
on VI.VI_ident = ET.VI_ident
where exists
(select 1
from IT_itineraire I1
inner join ET_etape E1
on E1.IT_ident = I1.IT_ident
inner join VI_ville V1
on V1.VI_ident = E1.VI_ident
where I1.IT_ident=IT.IT_ident
and V1.VI_nom='Biarritz'
)
and exists
(select 1
from IT_itineraire I1
inner join ET_etape E1
on E1.IT_ident = I1.IT_ident
inner join VI_ville V1
on V1.VI_ident = E1.VI_ident
where I1.IT_ident=IT.IT_ident
and V1.VI_nom='La Rochelle'
)
order by IT.IT_code
, ET.ET_num
; |
Partager