do $$ begin /* Скрипт создания информационной базы данных Согласно технического задания https://git.hostfl.ru/VolovikovAlex/Study2025 Редакция 2025-02-12 Edit by valex */ /* 1. Удаляем старые элементы ====================================== */ raise notice 'Запускаем создание новой структуры базы данных meteo'; begin -- Связи alter table if exists public.measurment_input_params drop constraint if exists measurment_type_id_fk; alter table if exists public.employees drop constraint if exists military_rank_id_fk; alter table if exists public.measurment_baths drop constraint if exists measurment_input_param_id_fk; alter table if exists public.measurment_baths drop constraint if exists emploee_id_fk; -- Таблицы drop table if exists public.measurment_input_params; drop table if exists public.measurment_baths; drop table if exists public.employees; drop table if exists public.measurment_types; drop table if exists public.military_ranks; -- Нумераторы drop sequence if exists public.measurment_input_params_seq; drop sequence if exists public.measurment_baths_seq; drop sequence if exists public.employees_seq; drop sequence if exists public.military_ranks_seq; drop sequence if exists public.measurment_types_seq; end; raise notice 'Удаление старых данных выполнено успешно'; /* 2. Добавляем структуры данных ================================================ */ -- Справочник должностей create table military_ranks ( id integer primary key not null, description character varying(255) ); insert into military_ranks(id, description) values(1,'Рядовой'),(2,'Лейтенант'); create sequence military_ranks_seq start 3; alter table military_ranks alter column id set default nextval('public.military_ranks_seq'); -- Пользователя create table employees ( id integer primary key not null, name text, birthday timestamp , military_rank_id integer ); insert into employees(id, name, birthday,military_rank_id ) values(1, 'Воловиков Александр Сергеевич','1978-06-24', 2); create sequence employees_seq start 2; alter table employees alter column id set default nextval('public.employees_seq'); -- Устройства для измерения create table measurment_types ( id integer primary key not null, short_name character varying(50), description text ); insert into measurment_types(id, short_name, description) values(1, 'ДМК', 'Десантный метео комплекс'), (2,'ВР','Ветровое ружье'); create sequence measurment_types_seq start 3; alter table measurment_types alter column id set default nextval('public.measurment_types_seq'); -- Таблица с параметрами create table measurment_input_params ( id integer primary key not null, measurment_type_id integer not null, height numeric(8,2) default 0, temperature numeric(8,2) default 0, pressure numeric(8,2) default 0, wind_direction numeric(8,2) default 0, wind_speed numeric(8,2) default 0 ); insert into measurment_input_params(id, measurment_type_id, height, temperature, pressure, wind_direction,wind_speed ) values(1, 1, 100,12,34,0.2,45); create sequence measurment_input_params_seq start 2; alter table measurment_input_params alter column id set default nextval('public.measurment_input_params_seq'); -- Таблица с историей create table measurment_baths ( id integer primary key not null, emploee_id integer not null, measurment_input_param_id integer not null, started timestamp default now() ); insert into measurment_baths(id, emploee_id, measurment_input_param_id) values(1, 1, 1); create sequence measurment_baths_seq start 2; alter table measurment_baths alter column id set default nextval('public.measurment_baths_seq'); raise notice 'Создание общих справочников и наполнение выполнено успешно'; /* 3. Подготовка расчетных структур ========================================== */ drop table if exists calc_temperatures_correction; create table calc_temperatures_correction ( temperature numeric(8,2) primary key, correction numeric(8,2) ); insert into public.calc_temperatures_correction(temperature, correction) Values(0, 0.5),(5, 0.5),(10, 1), (20,1), (25, 2), (30, 3.5), (40, 4.5); drop type if exists interpolation_type; create type interpolation_type as ( x0 numeric(8,2), x1 numeric(8,2), y0 numeric(8,2), y1 numeric(8,2) ); raise notice 'Расчетные структуры сформированы'; /* 4. Создание связей ========================================== */ begin alter table public.measurment_baths add constraint emploee_id_fk foreign key (emploee_id) references public.employees (id); alter table public.measurment_baths add constraint measurment_input_param_id_fk foreign key(measurment_input_param_id) references public.measurment_input_params(id); alter table public.measurment_input_params add constraint measurment_type_id_fk foreign key(measurment_type_id) references public.measurment_types (id); alter table public.employees add constraint military_rank_id_fk foreign key(military_rank_id) references public.military_ranks (id); end; raise notice 'Связи сформированы'; raise notice 'Структура сформирована успешно'; end $$;