Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- drop database if exists ti3_metro;
- create database if not exists ti3_metro;
- use ti3_metro;
- create table operator(
- id smallint not null primary key,
- nazwa varchar(32) not null
- );
- create table linia_metra(
- id smallint not null primary key,
- nazwa varchar(32) not null,
- operatorId smallint not null);
- create table stacja(
- id smallint not null primary key,
- nazwa varchar(32) not null,
- liniaMetraId smallint not null,
- isActive char(1) not null default '1',
- passThrough char(1) not null default '0');
- create table stacje_relacje(
- id smallint not null primary key,
- aktStacjaId smallint not null,
- docelStacjaId smallint not null,
- czasPrzejazdu smallint not null,
- dystans integer not null);
- alter table linia_metra add constraint foreign key fk_operatorId(operatorId) references operator(id) on update restrict on delete restrict;
- alter table stacja add constraint foreign key fk_liniaMetraId(liniaMetraId) references linia_Metra(id) on update restrict on delete restrict;
- alter table stacje_relacje add constraint foreign key fk_docelStacjaId(docelStacjaId) references stacja(id) on update restrict on delete restrict;
- insert into operator values (1, upper('operator 1'));
- insert into linia_metra (id, nazwa, operatorId)
- values (1, upper('linia a'), 1);
- insert into linia_metra (id, nazwa, operatorId)
- values (2, upper('linia b'), 1);
- insert into stacja (id, nazwa, liniaMetraId, isActive, passThrough)
- values
- (1, upper('a1'), 1, '1', '0'),
- (2, upper('a2'), 1, '1', '0'),
- (3, upper('a3'), 1, '1', '0'),
- (4, upper('a4'), 1, '1', '0'),
- (5, upper('b1'), 2, '1', '0'),
- (6, upper('b2'), 2, '1', '0'),
- (7, upper('b3'), 2, '1', '0');
- insert into stacje_relacje (id, aktStacjaId, docelStacjaId, czasPrzejazdu, dystans) values
- (1,1,2,7,7000),
- (2,2,1,7,7000),
- (3,2,3,7,7000),
- (4,2,5,0,7000),
- (5,3,2,7,7000),
- (6,3,4,7,7000),
- (7,4,3,7,7000),
- (8,5,2,0,7000),
- (9,5,6,7,7000),
- (10,6,5,7,7000),
- (11,6,7,7,7000),
- (12,7,6,7,7000);
- select s.*, lm. * from stacje as s
- inner join linia_metra as lm on lm.id = s.liniaMetraid
- inner join operator as o on o.id = lm.operatorId
- order by s.id;
- select o.nazwa as operatorNazwa, lm.nazwa as liniaMetraNazwa,
- s.nazwa as stacjaNazwa, s.isActive, s.passThrough,
- lm.operatorId, s.liniaMetraId, s.id as stacjaId
- from stacja as s
- inner join linia_metra as lm on ls.id = s.liniaMetraId
- inner join operator as o on o.id = ls.operatorId
- order by o.nazwa asc, lm.nazwa asc, s.id asc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement