This commit is contained in:
2026-03-10 14:35:04 +03:00
parent 8fa7d13072
commit ab1d4c72fa
6 changed files with 263 additions and 26 deletions

1
4/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
data.sql

0
4/data.js Normal file
View File

13
4/deploy.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
set -exu
SERVER=root@185.103.252.32
PSQL="sudo -u postgres psql"
DB=db2026
scp schema.sql data.sql "$SERVER:/tmp/$DB/"
ssh $SERVER \
"chmod -R 777 /tmp/$DB;" \
"$PSQL -d $DB -e -q -f /tmp/$DB/schema.sql;" \
"$PSQL -d $DB -e -q -f /tmp/$DB/data.sql;"

25
4/report.md Normal file
View File

@ -0,0 +1,25 @@
## Лабораторная работа 4
**Физическая реализация базы данных**
**Срок выполнения: 2 недели**
### **Цель**
Перевести логическую модель в реальную базу данных.
### **Содержание работы**
Студент должен:
* разработать физическую схему БД (DDL);
* определить ограничения целостности;
* подготовить тестовые данные;
* автоматизировать пересоздание базы данных.
### **Отчетные материалы**
* SQL-скрипт создания схемы;
* SQL-скрипт начального наполнения;
* скрипт генерации тестовых данных (Python или другой удобный вам язык);
* инструкция по запуску.

224
4/schema.sql Normal file
View File

@ -0,0 +1,224 @@
DROP TABLE IF EXISTS "Person" CASCADE;
CREATE TABLE "Person" (
id int PRIMARY KEY,
full_name text NOT NULL,
passwd_hash char(64) NOT NULL
);
CREATE TABLE "Client" (
phone text NOT NULL,
email text NOT NULL,
billing_account text NOT NULL,
PRIMARY KEY(id)
) INHERITS ("Person");
CREATE TABLE "Master" (
passport text UNIQUE NOT NULL,
readme text NOT NULL,
PRIMARY KEY(id)
) INHERITS ("Person");
DROP TABLE IF EXISTS "Studio", "Course", "Class", "Class_Master", "Registration", "FoodProductEnum", "FoodProduct", "Equipment", "Class_Equipment", "DrugEnum", "Drug", "DrugIntolerance", "ActivityLog";
CREATE TABLE "Studio" (
id int PRIMARY KEY,
address text UNIQUE NOT NULL,
capacity int NOT NULL CHECK (capacity > 0),
begin_date timestamptz NOT NULL,
duration interval NOT NULL
);
CREATE TABLE "Course" (
id int PRIMARY KEY,
Master_id int,
name text NOT NULL,
duration interval NOT NULL,
FOREIGN KEY (Master_id) REFERENCES "Master" (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Class" (
begin_date timestamptz,
Course_id int,
Studio_id int,
name text NOT NULL,
type int NOT NULL,
duration interval NOT NULL,
PRIMARY KEY (begin_date, Course_id),
FOREIGN KEY (Course_id) REFERENCES "Course" (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Studio_id) REFERENCES "Studio" (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Class_Master" (
Class_begin_date timestamptz,
Class_Course_id int,
Master_id int,
PRIMARY KEY (Class_begin_date, Class_Course_id, Master_id),
FOREIGN KEY (Master_id) REFERENCES "Master" (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Class_begin_date, Class_Course_id) REFERENCES "Class" (begin_date, Course_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "Registration" (
Client_id int,
Course_id int,
date timestamptz NOT NULL,
status int NOT NULL,
PRIMARY KEY (Client_id, Course_id),
FOREIGN KEY (Client_id) REFERENCES "Client" (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Course_id) REFERENCES "Course" (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "FoodProductEnum" (
id int PRIMARY KEY,
name text NOT NULL,
avg_price real NOT NULL
);
CREATE TABLE "FoodProduct" (
id int,
FoodProductEnum_id int,
Class_begin_date timestamptz,
Class_Course_id int,
buy_price real NOT NULL,
buy_date timestamptz NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
expires_date timestamptz NOT NULL,
PRIMARY KEY (id, FoodProductEnum_id),
FOREIGN KEY (FoodProductEnum_id) REFERENCES "FoodProductEnum" (id)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
FOREIGN KEY (Class_begin_date, Class_Course_id) REFERENCES "Class" (begin_date, Course_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Equipment" (
id int PRIMARY KEY,
Studio_id int,
name text NOT NULL,
usage_price real NOT NULL,
usage_type int NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
FOREIGN KEY (Studio_id) REFERENCES "Studio" (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "Class_Equipment" (
Class_begin_date timestamptz,
Class_Course_id int,
Equipment_id int,
PRIMARY KEY (Class_begin_date, Class_Course_id, Equipment_id),
FOREIGN KEY (Class_begin_date, Class_course_id) REFERENCES "Class" (begin_date, Course_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Equipment_id) REFERENCES "Equipment" (id)
ON DELETE RESTRICT
ON UPDATE CASCADE
);
CREATE TABLE "DrugEnum" (
id int PRIMARY KEY,
name text NOT NULL,
avg_price real NOT NULL
);
CREATE TABLE "Drug" (
id int,
DrugEnum_id int,
Course_id int,
buy_price real NOT NULL,
buy_date timestamptz NOT NULL,
delivery_price real NOT NULL,
delivery_date timestamptz NOT NULL,
expires_date timestamptz NOT NULL,
PRIMARY KEY (id, DrugEnum_id),
FOREIGN KEY (DrugEnum_id) REFERENCES "DrugEnum" (id)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
FOREIGN KEY (Course_id) REFERENCES "Course" (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
CREATE TABLE "DrugIntolerance" (
Person_id int,
DrugEnum_id int,
PRIMARY KEY (Person_id, DrugEnum_id),
FOREIGN KEY (Person_id) REFERENCES "Person" (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (DrugEnum_id) REFERENCES "DrugEnum" (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE "ActivityLog" (
id int,
Person_id int,
action text NOT NULL,
PRIMARY KEY (id, Person_id),
FOREIGN KEY (Person_id) REFERENCES "Person" (id)
ON DELETE NO ACTION
ON UPDATE CASCADE
);