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

Homework 10 #1123

Open
wants to merge 2 commits into
base: SMakhov/main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.DS_Store
.AppleDouble
.LSOverride
.env
120 changes: 120 additions & 0 deletions code/data_10000.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Create or replace function random_string(length integer) returns text as
$$
declare
chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
result text := '';
i integer := 0;
begin
if length < 0 then
raise exception 'Given length cannot be less than 0';
end if;
for i in 1..length
loop
result := result || chars[1 + random() * (array_length(chars, 1) - 1)];
end loop;
return result;
end;
$$ language plpgsql;

-- Наполнение таблицы clients (Клиенты)
insert into clients (id, surname, name, email, phone, age)
select gs.id,
concat('surname-', random_string(((random() * (15 - 3 + 1)) + 3)::integer)),
concat('name-', random_string(((random() * (8 - 3 + 1)) + 3)::integer)),
concat('email', ((random() * (1000 - 100 + 1))::integer + 100), '@',
(case (floor(random() * 2))
when 0 then 'mail'
when 1 then 'yandex'
when 2 then 'rambler'
end), '.ru'),
concat('+79', substr((random() * 10000000000)::varchar, 1, 9)),
((random() * (85 - 16 + 1)) + 16)::integer
from generate_series(1, 1500) as gs(id);

-- Наполнение таблицы genres (Жанры)
insert into genres (id, name)
values (1, 'Ужасы'),
(2, 'Боевик'),
(3, 'Мелодрама'),
(4, 'Драма'),
(5, 'Комедия');

-- Наполнение таблицы movies (Фильмы)
insert into movies(id, genre_id, name, description, duration, age_limit, rating)
select gs.id,
(floor(random() * 5 + 1)),
concat('movie-', random_string(((random() * (8 - 3 + 1)) + 3)::integer)),
concat('description-', random_string(((random() * (50 - 10 + 1)) + 10)::integer)),
((random() * (120 - 60 + 1)) + 60)::integer,
((random() * (21 - 6 + 1)) + 6)::integer,
CAST((random() * 4 + 1) AS decimal(2, 1))
from generate_series(1, 10000) as gs(id);

-- Наполнение таблицы halls (Кинозалы)
insert into halls(id, name, seat_count)
select gs.id,
concat('Зал_', random_string(5)),
160
from generate_series(1, 5) as gs(id);

-- Наполнение таблицы seats (Места кинозала)
-- В каждом зале 8 рядов по 20 мест, в сумме 160 мест
do
$$
begin
for hall in 1..5
loop
for row in 1..8
loop
insert into seats(hall_id, number, row)
select hall,
number,
row
from generate_series(1, 20) as number;
end loop;
end loop;
end;
$$ language plpgsql;

-- Наполнение таблицы sessions (Сеансы)
-- В каждом зале 7 показов фильмов в день на протяжении 1 месяца
do
$$
declare
start_time timestamp;
begin
for day in 1..31
loop
for hall in 1..5
loop
start_time = now()::date - (31 - day || ' days')::interval;
insert into sessions(hall_id, movie_id, started_at, ended_at)
select hall,
((random() * (10000 - 1) + 1))::integer,
start_time + (1 || ' days')::interval,
start_time + (1 || ' days')::interval + concat((60 + random() * 180)::integer, ' minutes')::interval
from generate_series(1, 7) as gs(id);
end loop;
end loop;
end;
$$ language plpgsql;

-- Наполнение таблицы prices (Цены)
insert into prices(session_id, seat_id, price)
select sessions.id as session_id,
seats.id as seat_id,
CAST((random() * (500 - 250) + 1) AS decimal(5, 2))
from sessions
left join halls on sessions.hall_id = halls.id
right join seats on seats.hall_id = halls.id;

-- Наполнение таблицы tickets (Билеты)
insert into tickets (client_id, price_id, price, sale_at)
select ((random() * (1500 - 1) + 1))::integer as client_id,
prices.id as price_id,
prices.price,
(sessions.started_at + random() * (sessions.ended_at - sessions.started_at)) as sale_at
from prices
join public.sessions on sessions.id = prices.session_id
order by random()
limit 10000;
120 changes: 120 additions & 0 deletions code/data_10_million.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Create or replace function random_string(length integer) returns text as
$$
declare
chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
result text := '';
i integer := 0;
begin
if length < 0 then
raise exception 'Given length cannot be less than 0';
end if;
for i in 1..length
loop
result := result || chars[1 + random() * (array_length(chars, 1) - 1)];
end loop;
return result;
end;
$$ language plpgsql;

-- Наполнение таблицы clients (Клиенты)
insert into clients (id, surname, name, email, phone, age)
select gs.id,
concat('surname-', random_string(((random() * (15 - 3 + 1)) + 3)::integer)),
concat('name-', random_string(((random() * (8 - 3 + 1)) + 3)::integer)),
concat('email', ((random() * (1000 - 100 + 1))::integer + 100), '@',
(case (floor(random() * 2))
when 0 then 'mail'
when 1 then 'yandex'
when 2 then 'rambler'
end), '.ru'),
concat('+79', substr((random() * 10000000000)::varchar, 1, 9)),
((random() * (85 - 16 + 1)) + 16)::integer
from generate_series(1, 1500) as gs(id);

-- Наполнение таблицы genres (Жанры)
insert into genres (id, name)
values (1, 'Ужасы'),
(2, 'Боевик'),
(3, 'Мелодрама'),
(4, 'Драма'),
(5, 'Комедия');

-- Наполнение таблицы movies (Фильмы)
insert into movies(id, genre_id, name, description, duration, age_limit, rating)
select gs.id,
(floor(random() * 5 + 1)),
concat('movie-', random_string(((random() * (8 - 3 + 1)) + 3)::integer)),
concat('description-', random_string(((random() * (50 - 10 + 1)) + 10)::integer)),
((random() * (120 - 60 + 1)) + 60)::integer,
((random() * (21 - 6 + 1)) + 6)::integer,
CAST((random() * 4 + 1) AS decimal(2, 1))
from generate_series(1, 1000000) as gs(id);

-- Наполнение таблицы halls (Кинозалы)
insert into halls(id, name, seat_count)
select gs.id,
concat('Зал_', random_string(5)),
160
from generate_series(1, 5) as gs(id);

-- Наполнение таблицы seats (Места кинозала)
-- В каждом зале 8 рядов по 20 мест, в сумме 160 мест
do
$$
begin
for hall in 1..5
loop
for row in 1..8
loop
insert into seats(hall_id, number, row)
select hall,
number,
row
from generate_series(1, 20) as number;
end loop;
end loop;
end;
$$ language plpgsql;

-- Наполнение таблицы sessions (Сеансы)
-- В каждом зале 7 показов фильмов в день на протяжении 1095 дней
do
$$
declare
start_time timestamp;
begin
for day in 1..1825
loop
for hall in 1..5
loop
start_time = now()::date - (1825 - day || ' days')::interval;
insert into sessions(hall_id, movie_id, started_at, ended_at)
select hall,
((random() * (10000 - 1) + 1))::integer,
start_time + (1 || ' days')::interval,
start_time + (1 || ' days')::interval + concat((60 + random() * 180)::integer, ' minutes')::interval
from generate_series(1, 7) as gs(id);
end loop;
end loop;
end;
$$ language plpgsql;

-- Наполнение таблицы prices (Цены)
insert into prices(session_id, seat_id, price)
select sessions.id as session_id,
seats.id as seat_id,
CAST((random() * (500 - 250) + 1) AS decimal(5, 2))
from sessions
left join halls on sessions.hall_id = halls.id
right join seats on seats.hall_id = halls.id;

-- Наполнение таблицы tickets (Билеты)
insert into tickets (client_id, price_id, price, sale_at)
select ((random() * (1500 - 1) + 1))::integer as client_id,
prices.id as price_id,
prices.price,
(sessions.started_at + random() * (sessions.ended_at - sessions.started_at)) as sale_at
from prices
join public.sessions on sessions.id = prices.session_id
order by random()
limit 10000000;
104 changes: 104 additions & 0 deletions code/ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- Проверка существует ли таблица halls и её удаление
DROP TABLE IF EXISTS public.halls CASCADE;
-- Создание таблицы halls (Кинозалы)
CREATE TABLE public.halls
(
"id" BIGSERIAL NOT NULL PRIMARY KEY,
"name" VARCHAR(100) NOT NULL,
"seat_count" INT NOT NULL
);

-- Проверка существует ли таблица seats и её удаление
DROP TABLE IF EXISTS public.seats CASCADE;
-- Создание таблицы seats (Места в кинозале) + связь с таблицей (halls)
CREATE TABLE public.seats
(
"id" BIGSERIAL NOT NULL primary key,
"hall_id" BIGINT NOT NULL,
"number" INT NOT NULL,
"row" INT NOT NULL,

CONSTRAINT public_seats_hall_id_foreign FOREIGN KEY (hall_id) REFERENCES halls (id) ON DELETE CASCADE ON UPDATE CASCADE
);

-- Проверка существует ли таблица genres и её удаление
DROP TABLE IF EXISTS public.genres CASCADE;
-- Создание таблицы genres (Жанры фильмов)
CREATE TABLE public.genres
(
"id" BIGSERIAL NOT NULL primary key,
"name" VARCHAR(100) NOT NULL
);

-- Проверка существует ли таблица movies и её удаление
DROP TABLE IF EXISTS public.movies CASCADE;
-- Создание таблицы movies (Фильмы) + связь с таблицей genres
CREATE TABLE public.movies
(
"id" BIGSERIAL NOT NULL primary key,
"genre_id" BIGINT NOT NULL,
"name" VARCHAR(100) NOT NULL,
"description" VARCHAR(255) NOT NULL,
"duration" INT NOT NULL,
"age_limit" INT NOT NULL,
"rating" decimal(2, 1) NOT NULL,

CONSTRAINT public_movies_genre_id_foreign FOREIGN KEY (genre_id) REFERENCES genres (id) ON DELETE CASCADE ON UPDATE CASCADE
);

-- Проверка существует ли таблица sessions и её удаление
DROP TABLE IF EXISTS public.sessions CASCADE;
-- Создание таблицы sessions (Сеансы) + связь с таблицами (halls, movies)
CREATE TABLE public.sessions
(
"id" BIGSERIAL NOT NULL primary key,
"hall_id" BIGINT NOT NULL,
"movie_id" BIGINT NOT NULL,
"started_at" TIMESTAMP NOT NULL,
"ended_at" TIMESTAMP NOT NULL,

CONSTRAINT public_sessions_hall_id_foreign FOREIGN KEY (hall_id) REFERENCES halls (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT public_sessions_movie_id_foreign FOREIGN KEY (movie_id) REFERENCES movies (id) ON DELETE CASCADE ON UPDATE CASCADE
);

-- Проверка существует ли таблица clients и её удаление
DROP TABLE IF EXISTS public.clients CASCADE;
-- Создание таблицы clients (Клиенты)
CREATE TABLE public.clients
(
"id" BIGSERIAL NOT NULL primary key,
"surname" VARCHAR(255) NOT NULL,
"name" VARCHAR(255) NOT NULL,
"email" VARCHAR(255) NOT NULL,
"phone" VARCHAR(50) NOT NULL,
"age" INT NOT NULL
);

-- Проверка существует ли таблица prices и её удаление
DROP TABLE IF EXISTS public.prices CASCADE;
-- Создание таблицы prices (Цены) + связь с таблицами (sessions, seats)
CREATE TABLE public.prices
(
"id" BIGSERIAL NOT NULL primary key,
"session_id" BIGINT NOT NULL,
"seat_id" BIGINT NOT NULL,
"price" DECIMAL(5, 2) NOT NULL,

CONSTRAINT public_prices_session_id_foreign FOREIGN KEY (session_id) REFERENCES sessions (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT public_prices_seat_id_foreign FOREIGN KEY (seat_id) REFERENCES seats (id) ON DELETE CASCADE ON UPDATE CASCADE
);

-- Проверка существует ли таблица tickets и её удаление
DROP TABLE IF EXISTS public.tickets CASCADE;
-- Создание таблицы tickets (Билеты) + связь с таблицами (clients, prices)
CREATE TABLE public.tickets
(
"id" BIGSERIAL NOT NULL primary key,
"client_id" BIGINT NOT NULL,
"price_id" BIGINT NOT NULL,
"price" DECIMAL(5, 2) NOT NULL,
"sale_at" TIMESTAMP NOT NULL,

CONSTRAINT public_tickets_client_id_foreign FOREIGN KEY (client_id) REFERENCES clients (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT public_tickets_price_id_foreign FOREIGN KEY (price_id) REFERENCES prices (id) ON DELETE CASCADE ON UPDATE CASCADE
);
Loading