-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
121 lines (101 loc) · 2.8 KB
/
schema.sql
File metadata and controls
121 lines (101 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Use this command line to create the database first in terminal
CREATE DATABASE atelier WITH OWNER = hrvy ENCODING = 'UTF8';
then run this file with the command:
psql -d atelier -a -f schema.sql
to create the tables within that database
*/
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
slogan VARCHAR(200) NOT NULL,
description TEXT NOT NULL,
category VARCHAR(200) NOT NULL,
default_price INT NOT NULL
);
CREATE TABLE IF NOT EXISTS features (
id SERIAL PRIMARY KEY,
product_id INT NOT NULL,
feature VARCHAR(200) NOT NULL,
value VARCHAR(200),
CONSTRAINT fk_product
FOREIGN KEY(product_id)
REFERENCES products(id)
);
CREATE TABLE IF NOT EXISTS related (
id SERIAL PRIMARY KEY,
product_id INT NOT NULL,
related_id INT NOT NULL,
CONSTRAINT fk_product
FOREIGN KEY(product_id)
REFERENCES products(id)
);
CREATE TABLE IF NOT EXISTS styles (
style_id SERIAL PRIMARY KEY,
product_id INT NOT NULL,
name VARCHAR(200) NOT NULL,
sale_price VARCHAR(20),
original_price INT NOT NULL,
"default?" BOOLEAN,
CONSTRAINT fk_product
FOREIGN KEY(product_id)
REFERENCES products(id)
);
CREATE TABLE IF NOT EXISTS photos (
id SERIAL PRIMARY KEY,
photos_id INT,
style_id INT NOT NULL,
url TEXT NOT NULL,
thumbnail_url TEXT NOT NULL,
CONSTRAINT fk_style
FOREIGN KEY(style_id)
REFERENCES styles(style_id)
);
CREATE TABLE IF NOT EXISTS skus (
id SERIAL PRIMARY KEY,
style_id INT NOT NULL,
size VARCHAR(10),
quantity SMALLINT NOT NULL,
CONSTRAINT fk_style
FOREIGN KEY(style_id)
REFERENCES styles(style_id)
);
-- import CSV files statements
COPY products
FROM '/Users/vytran/Downloads/product.csv'
DELIMITER ','
CSV HEADER;
COPY features
FROM '/Users/vytran/Downloads/features.csv'
DELIMITER ','
CSV HEADER;
COPY related
FROM '/Users/vytran/Downloads/related.csv'
DELIMITER ','
CSV HEADER;
COPY styles
FROM '/Users/vytran/Downloads/styles.csv'
DELIMITER ','
CSV HEADER;
COPY photos(photos_id, style_id, url, thumbnail_url)
FROM '/Users/vytran/Downloads/photos.csv'
DELIMITER ','
CSV HEADER;
-- the id column of the csv hit a duplicate id
-- since the PK needs to be unique and we don't use this id column in our data
-- i just dropped it..
ALTER TABLE photos
DROP COLUMN photos_id;
-- due to some formatting issues within the photos id
-- I also deleted the records where the thumbnail_url was incorrect
DELETE FROM photos WHERE thumbnail_url LIKE '%,http%';
COPY skus
FROM '/Users/vytran/Downloads/skus.csv'
DELIMITER ','
CSV HEADER;
-- create indexes
CREATE INDEX style_prod_index ON styles(product_id);
CREATE INDEX photo_style_index ON photos(style_id);
CREATE INDEX sku_style_index ON skus(style_id);
CREATE INDEX features_prod_index ON features(product_id);
CREATE INDEX related_prod_index ON related(product_id);