· 7 years ago · Feb 15, 2019, 11:38 AM
1drop database if exists ti3_metro;
2create database if not exists ti3_metro;
3use ti3_metro;
4
5create table operator(
6id smallint not null primary key,
7nazwa varchar(32) not null
8);
9
10create table linia_metra(
11id smallint not null primary key,
12nazwa varchar(32) not null,
13operatorId smallint not null);
14
15create table stacja(
16id smallint not null primary key,
17nazwa varchar(32) not null,
18liniaMetraId smallint not null,
19isActive char(1) not null default '1',
20passThrough char(1) not null default '0');
21
22create table stacje_relacje(
23id smallint not null primary key,
24aktStacjaId smallint not null,
25docelStacjaId smallint not null,
26czasPrzejazdu smallint not null,
27dystans integer not null);
28
29alter table stacje_relacje add column kosztPrzejazdu
30smallint not null default 0;
31
32alter table linia_metra add constraint foreign key fk_operatorId(operatorId) references operator(id) on update restrict on delete restrict;
33alter table stacja add constraint foreign key fk_liniaMetraId(liniaMetraId) references linia_Metra(id) on update restrict on delete restrict;
34alter table stacje_relacje add constraint foreign key fk_docelStacjaId(docelStacjaId) references stacja(id) on update restrict on delete restrict;
35
36insert into operator values (1, upper('operator 1'));
37
38insert into linia_metra (id, nazwa, operatorId)
39values (1, upper('linia a'), 1);
40insert into linia_metra (id, nazwa, operatorId)
41values (2, upper('linia b'), 1);
42
43insert into stacja (id, nazwa, liniaMetraId, isActive, passThrough)
44values
45(1, upper('a1'), 1, '1', '0'),
46(2, upper('a2'), 1, '1', '0'),
47(3, upper('a3'), 1, '1', '0'),
48(4, upper('a4'), 1, '1', '0'),
49(5, upper('b1'), 2, '1', '0'),
50(6, upper('b2'), 2, '1', '0'),
51(7, upper('b3'), 2, '1', '0');
52
53insert into stacje_relacje (id, aktStacjaId, docelStacjaId, czasPrzejazdu, dystans) values
54(1,1,2,7,7000),
55(2,2,1,7,7000),
56(3,2,3,7,7000),
57(4,2,5,0,7000),
58(5,3,2,7,7000),
59(6,3,4,7,7000),
60(7,4,3,7,7000),
61(8,5,2,0,7000),
62(9,5,6,7,7000),
63(10,6,5,7,7000),
64(11,6,7,7,7000),
65(12,7,6,7,7000);
66
67select s.*, lm. * from stacja as s
68inner join linia_metra as lm on lm.id = s.liniaMetraid
69inner join operator as o on o.id = lm.operatorId
70order by s.id;
71
72select o.nazwa as operatorNazwa, lm.nazwa as liniaMetraNazwa,
73s.nazwa as stacjaNazwa, s.isActive, s.passThrough,
74lm.operatorId, s.liniaMetraId, s.id as stacjaId
75from stacja as s
76inner join linia_metra as lm on lm.id = s.liniaMetraId
77inner join operator as o on o.id = lm.operatorId
78order by o.nazwa asc, lm.nazwa asc, s.id asc;