70 lines
1.9 KiB
SQL
70 lines
1.9 KiB
SQL
CREATE EXTENSION "uuid-ossp";
|
|
|
|
CREATE TYPE "Role" AS ENUM (
|
|
'Admin',
|
|
'User'
|
|
);
|
|
|
|
CREATE TYPE "OrderStatus" AS ENUM (
|
|
'Confirmed',
|
|
'Cancelled',
|
|
'Delivered',
|
|
'Payed',
|
|
'RequireRefund',
|
|
'Refunded'
|
|
);
|
|
|
|
CREATE TABLE accounts
|
|
(
|
|
id serial not null primary key,
|
|
email varchar not null unique,
|
|
login varchar not null unique,
|
|
pass_hash varchar not null,
|
|
role "Role" not null default 'User'
|
|
);
|
|
|
|
CREATE TABLE products
|
|
(
|
|
id serial not null primary key,
|
|
name varchar not null unique,
|
|
short_description varchar not null,
|
|
long_description varchar not null,
|
|
category varchar,
|
|
price_major int not null,
|
|
price_minor int not null,
|
|
CONSTRAINT positive_price_minor check ( price_major >= 0 )
|
|
);
|
|
|
|
CREATE TABLE stocks
|
|
(
|
|
id serial not null primary key,
|
|
product_id int references products (id) not null unique,
|
|
quantity int not null default 0,
|
|
CONSTRAINT positive_quantity check ( quantity >= 0 )
|
|
);
|
|
|
|
CREATE TABLE account_orders
|
|
(
|
|
id serial not null primary key,
|
|
buyer_id int references accounts (id) not null,
|
|
status "OrderStatus" not null default 'Confirmed'
|
|
);
|
|
|
|
CREATE TABLE order_items
|
|
(
|
|
id serial not null primary key,
|
|
product_id int references products (id) not null,
|
|
order_id int references account_orders (id),
|
|
quantity int not null default 0,
|
|
CONSTRAINT positive_quantity check ( quantity >= 0 )
|
|
);
|
|
|
|
CREATE TABLE statistics
|
|
(
|
|
id serial not null primary key,
|
|
url varchar not null,
|
|
clicks int not null default 0,
|
|
date DATE not null default now(),
|
|
CONSTRAINT positive_clicks check ( clicks >= 0 )
|
|
);
|