From 2907c0379ae43e208c4caf45fe16256318375cd2 Mon Sep 17 00:00:00 2001 From: "av.kalyanov" Date: Sun, 26 Apr 2026 08:52:50 +0300 Subject: [PATCH] Add dml.sql and fix ddl.sql --- SQL/ddl.sql | 12 +++++++----- SQL/dml.sql | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 SQL/dml.sql diff --git a/SQL/ddl.sql b/SQL/ddl.sql index a3047ed..0b54bec 100644 --- a/SQL/ddl.sql +++ b/SQL/ddl.sql @@ -8,19 +8,19 @@ create table akalyanov.users ( is_active boolean, created_at timestamp not null ); --- Таблица товаров +-- Таблица продуктов create table akalyanov.products ( id int primary key, name varchar(100) not null, category varchar(50), - price decimal(10, 2) not null, + price decimal(10,2) not null, in_stock boolean ); -- Таблица заказов create table akalyanov.orders ( id int primary key, user_id int, - order_date date not null, + order_date timestamptz not null, status varchar(20), foreign key (user_id) references akalyanov.users(id) ); @@ -29,6 +29,8 @@ create table akalyanov.order_items ( order_id int, product_id int, quantity smallint not null, - price_at_purchase decimal(10, 2) not null, - primary key (order_id, product_id) + price_at_purchase decimal(10,2) not null, + primary key (order_id, product_id), + foreign key (order_id) references akalyanov.orders(id), + foreign key (product_id) references akalyanov.products(id) ); \ No newline at end of file diff --git a/SQL/dml.sql b/SQL/dml.sql new file mode 100644 index 0000000..768e76b --- /dev/null +++ b/SQL/dml.sql @@ -0,0 +1,46 @@ +-- Добавление пользователей в таблицу users +insert into akalyanov.users (id, name, age, email, country, is_active, created_at) values +(1, 'Alice', 25, 'alice@example.com', 'US', true, '2024-01-10 10:00:00'), +(2, 'Bob', 30, 'bob@example.com', 'UK', true, '2024-02-05 09:30:00'), +(3, 'Carol', 27, 'carol@example.com', 'US', false, '2024-03-01 15:20:00'), +(4, 'Diana', 22, 'diana@GMAIL.com', 'US', true, '2024-01-12 08:15:00'), +(5, 'Evan', 35, 'EVAN@example.com', 'UK', true, '2024-01-18 19:40:00'), +(6, 'Farida', 29, 'farida@example.com', 'KZ', true, '2024-02-20 11:05:00'), +(7, 'George', 31, 'george+test@example.com', 'US', true, '2024-03-05 16:45:00'), +(8, 'Helen', 24, NULL, 'UK', false, '2024-03-06 08:20:00'), +(9, 'Ivan', 26, 'ivan@example.com', 'RU', true, '2024-01-11 14:30:00'), +(10, 'Julia', 28, 'julia@example.com', 'US', true, '2024-03-07 18:05:00'); + +-- Добавление товаров в таблицу products +insert into akalyanov.products (id, name, category, price, in_stock) values +(1, 'iPhone 15', 'Электроника', 80000, true), +(2, 'Kindle', 'Электроника', 15000, true), +(3, 'Кроссовки Nike', 'Одежда', 9000, true), +(4, 'Кроссовки NoName', 'Одежда', 3000, false), +(5, 'Наушники Pro', 'Электроника', 12000, true), +(6, 'Фитнес‑браслет', 'Гаджеты', 6000, true), +(7, 'Подарочная карта', 'Сервисы', 1000, true); + +-- Добавление заказов в таблицу orders +insert into akalyanov.orders (id, user_id, order_date, status) values +(1, 1, '2025-01-10 12:00:00', 'paid'), +(2, 3, '2025-01-12 09:30:00', 'cancelled'), +(3, 1, '2025-01-15 18:20:00', 'paid'), +(4, 2, '2025-01-20 11:05:00', 'created'), +(5, 4, '2025-02-01 17:45:00', 'paid'), +(6, 5, '2025-02-10 10:10:00', 'refunded'), +(7, 7, '2025-03-01 09:00:00', 'paid'), +(8, 9, '2025-03-05 19:30:00', 'paid'); + +-- Добавление товаров в таблицу order_items +insert into akalyanov.order_items (order_id, product_id, quantity, price_at_purchase) values +(1, 1, 1, 78000), +(1, 3, 2, 8500), +(2, 2, 1, 14000), +(3, 5, 1, 11000), +(3, 7, 1, 1000), +(4, 2, 1, 15000), +(5, 6, 1, 6000), +(6, 1, 1, 82000), +(7, 3, 1, 9000), +(8, 4, 2, 2800); \ No newline at end of file