· 7 years ago · Jan 18, 2019, 08: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 linia_metra add constraint foreign key fk_operatorId(operatorId) references operator(id) on update restrict on delete restrict;
30alter table stacja add constraint foreign key fk_liniaMetraId(liniaMetraId) references linia_Metra(id) on update restrict on delete restrict;
31alter table stacje_relacje add constraint foreign key fk_docelStacjaId(docelStacjaId) references stacja(id) on update restrict on delete restrict;
32
33insert into operator values (1, upper('operator 1'));
34
35insert into linia_metra (id, nazwa, operatorId) values (1, upper('linia a'), 1);
36insert into linia_metra (id, nazwa, operatorId) values (2, upper('linia b'), 1);
37
38insert into stacja (id, nazwa, liniaMetraId, isActive, passThrough) values
39(1, upper('a1'), '1' , '1', '0'),
40(2, upper('a2'), '1' , '1', '0'),
41(3, upper('a3'), '1' , '1', '0'),
42(4, upper('a4'), '1' , '1', '0'),
43(5, upper('b1'), '2' , '1', '0'),
44(6, upper('b2'), '2' , '1', '0'),
45(7, upper('b3'), '2' , '1', '0');
46
47insert into stacje_relacje (id, aktStacjaId, docelStacjaId, czasPrzejazdu, dystans) values
48(1, 1, 2, 7, 7000),
49(2, 2, 1, 7, 7000),
50(3, 2, 3, 7, 7000),
51(4, 2, 5, 0, 0000),
52(5, 3, 2, 7, 7000),
53(6, 3, 4, 7, 7000),
54(7, 4, 3, 7, 7000),
55(8, 5, 2, 0, 0000),
56(9, 5, 6, 7, 7000),
57(10, 6, 5, 7, 7000),
58(11, 6, 7, 7, 7000),
59(12, 7, 6, 7, 7000);
60
61select s.*, lm.* from stacja as s
62inner join linia_metra as lm on lm.id = s.liniaMetraId
63inner join operator as o on o.id = lm.operatorId
64order by s.id;
65
66select o.nazwa as operatorNazwa, lm.nazwa as liniaMetraNazwa,
67s.nazwa as stacjaNazwa, s.isActive, s.passThrough,
68lm.operatorId, s.liniaMetraId, s.id as stacjaId
69from stacja as s
70inner join linia_metra as lm on lm.id = s.liniaMetraId
71inner join operator as o on o.id = lm.operatorId
72order by o.nazwa asc, lm.nazwa asc, s.id asc;