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