database.sql 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. create table Driver (
  2. driver_id serial primary key,
  3. name varchar(64) not null,
  4. surname varchar(64) not null,
  5. patronimic varchar(64) not null
  6. );
  7. create table Transport (
  8. transport_id serial primary key,
  9. branch varchar(64) not null,
  10. model varchar(64) not null,
  11. license_plate_number varchar(64) not null,
  12. driver_id int
  13. );
  14. create table Transport_Flight (
  15. flight_id serial primary key,
  16. duration time,
  17. transport_id int,
  18. route_id int
  19. );
  20. create table Route (
  21. route_id serial primary key,
  22. route_name text not null
  23. );
  24. create table Route_Point (
  25. route_point_id serial primary key,
  26. route_id int,
  27. point_id int,
  28. order_of_the_route int not null
  29. );
  30. create table Point (
  31. point_id serial primary key,
  32. point_name text not null
  33. );
  34. create table Users(
  35. user_id serial primary key,
  36. login varchar(24) not null unique,
  37. password text not n
  38. role_id int
  39. )
  40. alter table Transport add foreign key (driver_id) references Driver(driver_id);
  41. alter table Transport_Flight add foreign key (transport_id) references Transport(transport_id);
  42. alter table Transport_Flight add foreign key (route_id) references Route(route_id);
  43. alter table Route_Point add foreign key (route_id) references Route(route_id);
  44. alter table Route_Point add foreign key (point_id) references Point(point_id);
  45. --триггер на валидацию ввода длительности поездки
  46. create or replace function check_flight_duration()
  47. returns trigger as $$
  48. begin
  49. if new.duration <= time '00:00:00' then
  50. raise exception 'Длительность рейса должна быть больше 00:00:00';
  51. end if;
  52. return new;
  53. end;
  54. $$ language plpgsql;
  55. create trigger trg_check_flight_duration
  56. before insert or update
  57. on transport_flight
  58. for each row
  59. execute function check_flight_duration();
  60. select * from transport
  61. update transport set driver_id = null where transport_id = 1
  62. select * from driver