225 lines
4.6 KiB
SQL
225 lines
4.6 KiB
SQL
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
|
|
);
|