-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tables.sql
260 lines (214 loc) · 7.69 KB
/
create_tables.sql
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
-----************************************************************
-- CATEGORIES
-----************************************************************
DROP TABLE IF EXISTS categories cascade;
CREATE TABLE categories(
categoryID SERIAL PRIMARY KEY,
categoryName VARCHAR(42),
description VARCHAR(255),
picture bytea
);
COPY categories FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/categories.csv'
DELIMITER ','
CSV HEADER;
-----************************************************************
-- CUSTOMERS
-----************************************************************
DROP TABLE IF EXISTS customers cascade;
CREATE TABLE customers(
customerID VARCHAR(20) PRIMARY KEY,
companyName VARCHAR(150),
contactName VARCHAR(200),
contactTitle VARCHAR(100),
address VARCHAR(255),
city VARCHAR(50),
region VARCHAR(50),
postalCode VARCHAR(50),
country VARCHAR(20),
phone VARCHAR(25),
fax VARCHAR(25)
);
COPY customers FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/customers.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM customers;
-----************************************************************
-- EMPLOYEES
-----************************************************************
DROP TABLE IF EXISTS employees cascade;
CREATE TABLE employees(
employeeID INTEGER PRIMARY KEY,
lastName VARCHAR,
firstName VARCHAR,
title VARCHAR,
titleOfCourtesy VARCHAR,
birthDate TIMESTAMP,
hireDate TIMESTAMP,
address VARCHAR,
city VARCHAR,
region VARCHAR,
postalCode VARCHAR,
country VARCHAR,
homePhone VARCHAR,
extension INTEGER,
photo bytea,
notes TEXT,
resportsTo VARCHAR,
photoPath VARCHAR
);
COPY employees FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/employees.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM employees;
-----************************************************************
-- SHIPPERS
-----************************************************************
DROP TABLE IF EXISTS shippers cascade;
CREATE TABLE shippers(
shipperID SERIAL PRIMARY KEY,
companyName CHAR(20),
phone VARCHAR(20)
);
COPY shippers FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/shippers.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM shippers;
-----************************************************************
-- ORDERS
-----************************************************************
DROP TABLE IF EXISTS orders;
CREATE TABLE orders(
orderID INTEGER PRIMARY KEY,
customerID VARCHAR(20),
employeeID SERIAL,
orderDate TIMESTAMP,
requiredDate TIMESTAMP,
shippedDate TIMESTAMP,
shipVia INTEGER,
freight NUMERIC,
shipName VARCHAR(50),
shipAddress VARCHAR(50),
shipCity VARCHAR(50),
shipRegion VARCHAR(50),
shipPostalCode VARCHAR(50),
shipCountry VARCHAR(20),
FOREIGN KEY (customerID) REFERENCES customers(customerID),
FOREIGN KEY (shipVia) REFERENCES shippers(shipperID),
FOREIGN KEY (employeeID) REFERENCES employees(employeeID)
);
COPY orders FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/orders.csv'
DELIMITER ','
CSV HEADER NULL 'NULL';
SELECT * FROM orders;
-----************************************************************
-- SUPPLIERS
-----************************************************************
DROP TABLE IF EXISTS suppliers;
CREATE TABLE suppliers(
supplierID SERIAL PRIMARY KEY,
companyName VARCHAR(255),
contactName VARCHAR(255),
contactTitle VARCHAR(255),
address VARCHAR(255),
city VARCHAR(255),
region VARCHAR(255),
postalCode VARCHAR(255),
country VARCHAR(255),
phone VARCHAR(255),
fax VARCHAR(255),
homePage TEXT
);
COPY suppliers FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/suppliers.csv'
DELIMITER ','
CSV HEADER;
-----************************************************************
-- PRODUCTS
-----************************************************************
DROP TABLE IF EXISTS products cascade;
CREATE TABLE products (
productID SERIAL PRIMARY KEY,
productName VARCHAR(100) NOT NULL,
supplierID INTEGER,
categoryID INTEGER,
quatityPerUnit VARCHAR(100),
unitPrice NUMERIC,
unitsInStock INTEGER,
unitsOnOrder INTEGER,
reorderLevel INTEGER,
discontinued INTEGER,
FOREIGN KEY (supplierID) REFERENCES suppliers (supplierID),
FOREIGN KEY (categoryID) REFERENCES categories (categoryID)
);
\copy products FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/products.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM products;
-----************************************************************
-- ORDER DETAILS
-----************************************************************
DROP TABLE IF EXISTS order_details cascade;
CREATE TABLE order_details(
orderID INTEGER NOT NULL,
productID INTEGER,
unitPrice NUMERIC,
quantity INTEGER,
discount NUMERIC,
FOREIGN KEY (orderID) REFERENCES orders (orderID),
FOREIGN KEY (productID) REFERENCES products (productID)
);
COPY order_details FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/order_details.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM order_details;
-----************************************************************
-- REGIONS
-----************************************************************
DROP TABLE IF EXISTS regions cascade;
CREATE TABLE regions(
regionID SERIAL PRIMARY KEY,
regionDescription VARCHAR(255)
);
COPY regions FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/regions.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM regions;
-----************************************************************
-- TERRITORIES
-----************************************************************
DROP TABLE IF EXISTS territories cascade;
CREATE TABLE territories(
territoryID SERIAL PRIMARY KEY,
territoryDescription VARCHAR(50),
regionID INTEGER,
FOREIGN KEY (regionID) REFERENCES regions (regionID)
);
COPY territories FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/territories.csv'
DELIMITER ','
CSV HEADER;
SELECT * FROM territories;
-----************************************************************
-- EMPLOYEES TERRITORIES
-----************************************************************
DROP TABLE IF EXISTS employee_territories cascade;
CREATE TABLE employee_territories(
employeeID INTEGER,
territoryID INTEGER NOT NULL,
FOREIGN KEY (employeeID) REFERENCES employees (employeeID),
FOREIGN KEY (territoryID) REFERENCES territories (territoryID)
);
COPY employee_territories FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/employee_territories_copy.csv'
DELIMITER ','
CSV HEADER NULL 'NULL';
SELECT * FROM employee_territories;
-----************************************************************
-- Country codes
-----************************************************************
DROP TABLE IF EXISTS country_code cascade;
CREATE TABLE country_code(
country VARCHAR PRIMARY KEY,
code VARCHAR
);
COPY country_code (country,code) FROM '/Users/CristaVillatoro/Desktop/tahini-tensor-student-code/week5/northwind_data_clean-master/data/country_code.csv'
DELIMITER ','
CSV HEADER NULL 'NULL';
SELECT * FROM country_code;
INSERT INTO country_code(country, code) VALUES('UK', 'UK');
SELECT * FROM country_code;