HomeWork20250206.sql 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. do $$
  2. begin
  3. /*
  4. Скрипт создания информационной базы данных
  5. Согласно технического задания https://git.hostfl.ru/VolovikovAlex/Study2025
  6. Редакция 2025-02-12
  7. Edit by valex
  8. */
  9. /*
  10. 1. Удаляем старые элементы
  11. ======================================
  12. */
  13. raise notice 'Запускаем создание новой структуры базы данных meteo';
  14. begin
  15. -- Связи
  16. alter table if exists public.measurment_input_params
  17. drop constraint if exists measurment_type_id_fk;
  18. alter table if exists public.employees
  19. drop constraint if exists military_rank_id_fk;
  20. alter table if exists public.measurment_baths
  21. drop constraint if exists measurment_input_param_id_fk;
  22. alter table if exists public.measurment_baths
  23. drop constraint if exists emploee_id_fk;
  24. -- Таблицы
  25. drop table if exists public.measurment_input_params;
  26. drop table if exists public.measurment_baths;
  27. drop table if exists public.employees;
  28. drop table if exists public.measurment_types;
  29. drop table if exists public.military_ranks;
  30. -- Нумераторы
  31. drop sequence if exists public.measurment_input_params_seq;
  32. drop sequence if exists public.measurment_baths_seq;
  33. drop sequence if exists public.employees_seq;
  34. drop sequence if exists public.military_ranks_seq;
  35. drop sequence if exists public.measurment_types_seq;
  36. end;
  37. raise notice 'Удаление старых данных выполнено успешно';
  38. /*
  39. 2. Добавляем структуры данных
  40. ================================================
  41. */
  42. -- Справочник должностей
  43. create table military_ranks
  44. (
  45. id integer primary key not null,
  46. description character varying(255)
  47. );
  48. insert into military_ranks(id, description)
  49. values(1,'Рядовой'),(2,'Лейтенант');
  50. create sequence military_ranks_seq start 3;
  51. alter table military_ranks alter column id set default nextval('public.military_ranks_seq');
  52. -- Пользователя
  53. create table employees
  54. (
  55. id integer primary key not null,
  56. name text,
  57. birthday timestamp ,
  58. military_rank_id integer
  59. );
  60. insert into employees(id, name, birthday,military_rank_id )
  61. values(1, 'Воловиков Александр Сергеевич','1978-06-24', 2);
  62. create sequence employees_seq start 2;
  63. alter table employees alter column id set default nextval('public.employees_seq');
  64. -- Устройства для измерения
  65. create table measurment_types
  66. (
  67. id integer primary key not null,
  68. short_name character varying(50),
  69. description text
  70. );
  71. insert into measurment_types(id, short_name, description)
  72. values(1, 'ДМК', 'Десантный метео комплекс'),
  73. (2,'ВР','Ветровое ружье');
  74. create sequence measurment_types_seq start 3;
  75. alter table measurment_types alter column id set default nextval('public.measurment_types_seq');
  76. -- Таблица с параметрами
  77. create table measurment_input_params
  78. (
  79. id integer primary key not null,
  80. measurment_type_id integer not null,
  81. height numeric(8,2) default 0,
  82. temperature numeric(8,2) default 0,
  83. pressure numeric(8,2) default 0,
  84. wind_direction numeric(8,2) default 0,
  85. wind_speed numeric(8,2) default 0
  86. );
  87. insert into measurment_input_params(id, measurment_type_id, height, temperature, pressure, wind_direction,wind_speed )
  88. values(1, 1, 100,12,34,0.2,45);
  89. create sequence measurment_input_params_seq start 2;
  90. alter table measurment_input_params alter column id set default nextval('public.measurment_input_params_seq');
  91. -- Таблица с историей
  92. create table measurment_baths
  93. (
  94. id integer primary key not null,
  95. emploee_id integer not null,
  96. measurment_input_param_id integer not null,
  97. started timestamp default now()
  98. );
  99. insert into measurment_baths(id, emploee_id, measurment_input_param_id)
  100. values(1, 1, 1);
  101. create sequence measurment_baths_seq start 2;
  102. alter table measurment_baths alter column id set default nextval('public.measurment_baths_seq');
  103. raise notice 'Создание общих справочников и наполнение выполнено успешно';
  104. /*
  105. 3. Подготовка расчетных структур
  106. ==========================================
  107. */
  108. drop table if exists calc_temperatures_correction;
  109. create table calc_temperatures_correction
  110. (
  111. temperature numeric(8,2) primary key,
  112. correction numeric(8,2)
  113. );
  114. insert into public.calc_temperatures_correction(temperature, correction)
  115. Values(0, 0.5),(5, 0.5),(10, 1), (20,1), (25, 2), (30, 3.5), (40, 4.5);
  116. drop type if exists interpolation_type;
  117. create type interpolation_type as
  118. (
  119. x0 numeric(8,2),
  120. x1 numeric(8,2),
  121. y0 numeric(8,2),
  122. y1 numeric(8,2)
  123. );
  124. raise notice 'Расчетные структуры сформированы';
  125. /*
  126. 4. Создание связей
  127. ==========================================
  128. */
  129. begin
  130. alter table public.measurment_baths
  131. add constraint emploee_id_fk
  132. foreign key (emploee_id)
  133. references public.employees (id);
  134. alter table public.measurment_baths
  135. add constraint measurment_input_param_id_fk
  136. foreign key(measurment_input_param_id)
  137. references public.measurment_input_params(id);
  138. alter table public.measurment_input_params
  139. add constraint measurment_type_id_fk
  140. foreign key(measurment_type_id)
  141. references public.measurment_types (id);
  142. alter table public.employees
  143. add constraint military_rank_id_fk
  144. foreign key(military_rank_id)
  145. references public.military_ranks (id);
  146. end;
  147. raise notice 'Связи сформированы';
  148. raise notice 'Структура сформирована успешно';
  149. end $$;