· 7 years ago · Jan 18, 2019, 08:24 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'), 1, '1', '0'),
62(6,upper('b2'), 1, '1', '0'),
63(7,upper('b3'), 1, '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);