Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A fominichev/hw9 #640

Open
wants to merge 3 commits into
base: AFominichev/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions DDL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE TABLE "films" (
"id" integer PRIMARY KEY,
"title" text
);

CREATE TABLE "attribute_types" (
"id" integer PRIMARY KEY,
"name" text,
"data_type" text
);

CREATE TABLE "attributes" (
"id" integer PRIMARY KEY,
"name" text,
"type_id" integer REFERENCES "attribute_types" ("id")
);

CREATE TABLE "attribute_values" (
"id" integer PRIMARY KEY,
"film_id" integer REFERENCES "films" ("id"),
"attribute_id" integer REFERENCES "attributes" ("id"),
"value_text" text,
"value_date" date,
"value_boolean" boolean,
"value_integer" integer,
"value_numeric" numeric(16, 4)
);

CREATE INDEX attribute_values_film_id ON attribute_values (film_id);
CREATE INDEX attribute_values_attribute_id ON attribute_values (attribute_id);
CREATE INDEX attributes_type_id ON attributes (type_id);
35 changes: 35 additions & 0 deletions Data_sample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
INSERT INTO films
VALUES (1, 'Белое солнце пустыни'),
(2, 'Терминатор 2'),
(3, 'Чужие');


INSERT INTO attribute_types
VALUES (1, 'Отзывы', 'text'),
(2, 'Премии', 'boolean'),
(3, 'Важные даты', 'date'),
(4, 'Служебные даты', 'date');


INSERT INTO attributes
VALUES (1, 'Отзыв критика Х', 1),
(2, 'Отзыв критика Y', 1),
(3, 'Оскар', 2),
(4, 'Ника', 2),
(5, 'Мировая премьера', 3),
(6, 'Премьера в РФ ', 3),
(7, 'Старт продаж билетов', 4),
(8, 'Старт рекламы на ТВ', 4);


INSERT INTO attribute_values
VALUES (1, 1, 2, 'Отличный фильм', NULL, NULL, NULL),
(2, 1, 4, '', NULL, TRUE, NULL),
(3, 1, 6, '', '2023-06-03', NULL, NULL),
(4, 1, 7, '', '2023-06-23', NULL, NULL),
(5, 2, 1, 'Веха в истории кино', NULL, NULL, NULL),
(6, 2, 3, '', NULL, TRUE, NULL),
(7, 2, 5, '', '2023-06-23', NULL, NULL),
(8, 2, 8, '', '2023-06-03', NULL, NULL),
(9, 1, 8, '', '2023-06-23', NULL, NULL),
(10, 1, 3, '', NULL, TRUE, NULL);
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# PHP_2022
PHP Developer. Professional

# EAV модель

Спроектировать EAV-хранение для базы данных кинотеатра
Логическая модель
DDL скрипты
View для служебных данных
View для данных для маркетинга
Binary file added UML.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions View_market_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE VIEW market_data AS
SELECT films.title,
attribute_types.name AS attribute_type,
attributes.name,
(CASE
WHEN attribute_types.data_type = 'text' THEN attribute_values.value_text::text
WHEN attribute_types.data_type = 'date' THEN attribute_values.value_date::text
WHEN attribute_types.data_type = 'boolean' THEN attribute_values.value_boolean::text
WHEN attribute_types.data_type = 'integer' THEN attribute_values.value_integer::text
END) AS value
FROM films
LEFT JOIN attribute_values ON attribute_values.film_id = films.id
LEFT JOIN attributes ON attributes.id = attribute_values.attribute_id
LEFT JOIN attribute_types ON attribute_types.id = attributes.type_id
WHERE attribute_types.id in (2,3)
ORDER BY films.id, attribute_types.id;

SELECT *
FROM market_data;
31 changes: 31 additions & 0 deletions View_work_dates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE VIEW work_dates AS
WITH work_dates_today AS
(SELECT films.id,
films.title,
array_agg(CONCAT (attributes.name, ' - ', attribute_values.value_date)) AS works
FROM films
LEFT JOIN attribute_values ON attribute_values.film_id = films.id
LEFT JOIN attributes ON attributes.id = attribute_values.attribute_id
LEFT JOIN attribute_types ON attribute_types.id = attributes.type_id
WHERE attribute_types.id = 4
AND attribute_values.value_date = CURRENT_DATE
GROUP BY films.id),
work_dates_after20days AS
(SELECT films.id,
films.title,
array_agg(CONCAT (attributes.name, ' - ', attribute_values.value_date)) AS works
FROM films
LEFT JOIN attribute_values ON attribute_values.film_id = films.id
LEFT JOIN attributes ON attributes.id = attribute_values.attribute_id
LEFT JOIN attribute_types ON attribute_types.id = attributes.type_id
WHERE attribute_types.id = 4
AND attribute_values.value_date = CURRENT_DATE + 20
GROUP BY films.id)
SELECT coalesce(work_dates_today.title, work_dates_after20days.title) as title,
work_dates_today.works AS today,
work_dates_after20days.works AS after_20
FROM work_dates_today
FULL JOIN work_dates_after20days ON work_dates_today.id = work_dates_after20days.id;

SELECT *
FROM work_dates;