|
| 1 | +set session my.number_of_sales = '2000000'; |
| 2 | +set session my.number_of_products = '300'; |
| 3 | +set session my.number_of_coutries = '100'; |
| 4 | +set session my.number_of_cities = '30'; |
| 5 | +set session my.start_date = '2019-01-01 00:00:00'; |
| 6 | +set session my.end_date = '2020-02-01 00:00:00'; |
| 7 | + |
| 8 | +--delete tables |
| 9 | +DROP TABLE IF EXISTS city CASCADE; |
| 10 | +DROP TABLE IF EXISTS country CASCADE; |
| 11 | +DROP TABLE IF EXISTS product CASCADE; |
| 12 | +DROP TABLE IF EXISTS sale CASCADE; |
| 13 | + |
| 14 | +-- Creation of product |
| 15 | +CREATE TABLE IF NOT EXISTS product ( |
| 16 | + product_id INT NOT NULL, |
| 17 | + name varchar(250) NOT NULL, |
| 18 | + PRIMARY KEY (product_id) |
| 19 | + ); |
| 20 | + |
| 21 | + -- Creation of country |
| 22 | +CREATE TABLE IF NOT EXISTS country ( |
| 23 | + country_id INT NOT NULL, |
| 24 | + country_name varchar(450) NOT NULL, |
| 25 | + PRIMARY KEY (country_id) |
| 26 | + ); |
| 27 | + |
| 28 | + -- Creation of city |
| 29 | +CREATE TABLE IF NOT EXISTS city ( |
| 30 | + city_id INT NOT NULL, |
| 31 | + city_name varchar(450) NOT NULL, |
| 32 | + country_id INT NOT NULL, |
| 33 | + PRIMARY KEY (city_id), |
| 34 | + CONSTRAINT fk_country |
| 35 | + FOREIGN KEY(country_id) |
| 36 | + REFERENCES country(country_id) |
| 37 | + ); |
| 38 | + |
| 39 | + -- Creation of sale |
| 40 | +CREATE TABLE IF NOT EXISTS sale ( |
| 41 | + sale_id INT NOT NULL, |
| 42 | + amount DECIMAL(20,3) NOT NULL, |
| 43 | + date_sale TIMESTAMP, |
| 44 | + product_id INT NOT NULL, |
| 45 | + city_id INT NOT NULL, |
| 46 | + PRIMARY KEY (sale_id), |
| 47 | + CONSTRAINT fk_product |
| 48 | + FOREIGN KEY(product_id) |
| 49 | + REFERENCES product(product_id), |
| 50 | + CONSTRAINT fk_city |
| 51 | + FOREIGN KEY(city_id) |
| 52 | + REFERENCES city(city_id) |
| 53 | + |
| 54 | + ); |
| 55 | + |
| 56 | + -- Filling of products |
| 57 | +INSERT INTO product |
| 58 | +select id, concat('Product ', id) |
| 59 | +FROM GENERATE_SERIES(1, current_setting('my.number_of_products')::int) as id; |
| 60 | + |
| 61 | +-- Filling of countries |
| 62 | +INSERT INTO country |
| 63 | +select id, concat('Country ', id) |
| 64 | +FROM GENERATE_SERIES(1, current_setting('my.number_of_coutries')::int) as id; |
| 65 | + |
| 66 | +-- Filling of cities |
| 67 | +INSERT INTO city |
| 68 | +select id |
| 69 | + , concat('City ', id) |
| 70 | + , floor(random() * (current_setting('my.number_of_coutries')::int) + 1)::int |
| 71 | +FROM GENERATE_SERIES(1, current_setting('my.number_of_cities')::int) as id; |
| 72 | + |
| 73 | +-- Filling of sales |
| 74 | + |
| 75 | +INSERT INTO sale |
| 76 | +select id |
| 77 | + , round(CAST(float8 (random() * 10000) as numeric), 3) |
| 78 | + , TO_TIMESTAMP(start_date, 'YYYY-MM-DD HH24:MI:SS') + |
| 79 | + random()* (TO_TIMESTAMP(end_date, 'YYYY-MM-DD HH24:MI:SS') |
| 80 | + - TO_TIMESTAMP(start_date, 'YYYY-MM-DD HH24:MI:SS')) |
| 81 | + , floor(random() * (current_setting('my.number_of_products')::int) + 1)::int |
| 82 | + , floor(random() * (current_setting('my.number_of_cities')::int) + 1)::int |
| 83 | +FROM GENERATE_SERIES(1, current_setting('my.number_of_sales')::int) as id |
| 84 | + , current_setting('my.start_date') as start_date |
| 85 | + , current_setting('my.end_date') as end_date; |
0 commit comments