Advertisement
Guest User

fdgdfg

a guest
Jan 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. drop database if exists ti3_metro;
  2. create database if not exists ti3_metro;
  3. use ti3_metro;
  4.  
  5. create table operator(
  6. id smallint not null primary key,
  7. nazwa varchar(32) not null
  8. );
  9.  
  10. create table linia_metra(
  11. id smallint not null primary key,
  12. nazwa varchar(32) not null,
  13. operatorId smallint not null);
  14.  
  15. create table stacja(
  16. id smallint not null primary key,
  17. nazwa varchar(32) not null,
  18. liniaMetraId smallint not null,
  19. isActive char(1) not null default '1',
  20. passThrough char(1) not null default '0');
  21.  
  22. create table stacje_relacje(
  23. id smallint not null primary key,
  24. aktStacjaId smallint not null,
  25. docelStacjaId smallint not null,
  26. czasPrzejazdu smallint not null,
  27. dystans integer not null);
  28.  
  29. alter table linia_metra add constraint foreign key fk_operatorId(operatorId) references operator(id) on update restrict on delete restrict;
  30. alter table stacja add constraint foreign key fk_liniaMetraId(liniaMetraId) references linia_Metra(id) on update restrict on delete restrict;
  31. alter table stacje_relacje add constraint foreign key fk_docelStacjaId(docelStacjaId) references stacja(id) on update restrict on delete restrict;
  32.  
  33. insert into operator values (1, upper('operator 1'));
  34.  
  35. insert into linia_metra (id, nazwa, operatorId)
  36. values (1, upper('linia a'), 1);
  37. insert into linia_metra (id, nazwa, operatorId)
  38. values (2, upper('linia b'), 1);
  39.  
  40. insert into stacja (id, nazwa, liniaMetraId, isActive, passThrough)
  41. values
  42. (1, upper('a1'), 1, '1', '0'),
  43. (2, upper('a2'), 1, '1', '0'),
  44. (3, upper('a3'), 1, '1', '0'),
  45. (4, upper('a4'), 1, '1', '0'),
  46. (5, upper('b1'), 2, '1', '0'),
  47. (6, upper('b2'), 2, '1', '0'),
  48. (7, upper('b3'), 2, '1', '0');
  49.  
  50. insert into stacje_relacje (id, aktStacjaId, docelStacjaId, czasPrzejazdu, dystans) values
  51. (1,1,2,7,7000),
  52. (2,2,1,7,7000),
  53. (3,2,3,7,7000),
  54. (4,2,5,0,7000),
  55. (5,3,2,7,7000),
  56. (6,3,4,7,7000),
  57. (7,4,3,7,7000),
  58. (8,5,2,0,7000),
  59. (9,5,6,7,7000),
  60. (10,6,5,7,7000),
  61. (11,6,7,7,7000),
  62. (12,7,6,7,7000);
  63.  
  64. select s.*, lm. * from stacje as s
  65. inner join linia_metra as lm on lm.id = s.liniaMetraid
  66. inner join operator as o on o.id = lm.operatorId
  67. order by s.id;
  68.  
  69. select o.nazwa as operatorNazwa, lm.nazwa as liniaMetraNazwa,
  70. s.nazwa as stacjaNazwa, s.isActive, s.passThrough,
  71. lm.operatorId, s.liniaMetraId, s.id as stacjaId
  72. from stacja as s
  73. inner join linia_metra as lm on ls.id = s.liniaMetraId
  74. inner join operator as o on o.id = ls.operatorId
  75. order by o.nazwa asc, lm.nazwa asc, s.id asc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement