-
Notifications
You must be signed in to change notification settings - Fork 18
/
import.sql
275 lines (248 loc) · 6.41 KB
/
import.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
\set nw_categories :cwd categories.csv
\set nw_customers :cwd customers.csv
\set nw_employees :cwd employees.csv
\set nw_employee_territories :cwd employee-territories.csv
\set nw_order_details :cwd order-details.csv
\set nw_orders :cwd orders.csv
\set nw_products :cwd products.csv
\set nw_regions :cwd regions.csv
\set nw_shippers :cwd shippers.csv
\set nw_suppliers :cwd suppliers.csv
\set nw_territories :cwd territories.csv
START TRANSACTION;
CREATE EXTENSION IF NOT EXISTS file_fdw;
CREATE SERVER northwind FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE nw_categories (
categoryid int,
categoryname varchar(15),
description text,
picture bytea
)
SERVER northwind
OPTIONS (
filename :'nw_categories',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_customers (
customerid char(5),
companyname varchar(40),
contactname varchar(30),
contacttitle varchar(30),
address varchar(60),
city varchar(15),
region varchar(15),
postalcode varchar(10),
country varchar(15),
phone varchar(24),
fax varchar(24)
)
SERVER northwind
OPTIONS (
filename :'nw_customers',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_employees (
employeeid int,
lastname varchar(20),
firstname varchar(10),
title varchar(30),
titleofcourtesy varchar(25),
birthdate date,
hiredate date,
address varchar(60),
city varchar(15),
region varchar(15),
postalcode varchar(10),
country varchar(15),
homephone varchar(24),
extension varchar(4),
photo bytea,
notes text,
reportto int,
photopath varchar(255)
)
SERVER northwind
OPTIONS (
filename :'nw_employees',
format 'csv',
header 'true',
delimiter '|',
null 'NULL'
);
CREATE FOREIGN TABLE nw_employee_territories (
employeeid int,
territoryid varchar(20)
)
SERVER northwind
OPTIONS (
filename :'nw_employee_territories',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_order_details (
orderid int,
productid int,
unitprice money,
quantity smallint,
discount real
)
SERVER northwind
OPTIONS (
filename :'nw_order_details',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_orders (
orderid int,
customerid char(5),
employeeid int,
orderdate date,
requireddate date,
shippeddate date,
shipvia int,
freight money,
shipname varchar(40),
shipaddress varchar(60),
shipcity varchar(15),
shipregion varchar(15),
shippostalcode varchar(10),
shipcountry varchar(15)
)
SERVER northwind
OPTIONS (
filename :'nw_orders',
format 'csv',
header 'true',
delimiter '|',
null 'NULL'
);
CREATE FOREIGN TABLE nw_products (
productid int,
productname varchar(40),
supplierid int,
categoryid int,
quantityperunit varchar(20),
unitprice money,
unitsinstock smallint,
unitsonorder smallint,
reorderlevel smallint,
discontinued bit
)
SERVER northwind
OPTIONS (
filename :'nw_products',
format 'csv',
header 'true',
delimiter '|',
null 'NULL'
);
CREATE FOREIGN TABLE nw_regions (
regionid int,
regiondescription char(50)
)
SERVER northwind
OPTIONS (
filename :'nw_regions',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_shippers (
shipperid int,
companyname varchar(40),
phone varchar(24)
)
SERVER northwind
OPTIONS (
filename :'nw_shippers',
format 'csv',
header 'true',
delimiter '|'
);
CREATE FOREIGN TABLE nw_suppliers (
supplierid int,
companyname varchar(40),
contactname varchar(30),
contacttitle varchar(30),
address varchar(60),
city varchar(15),
region varchar(15),
postalcode varchar(10),
country varchar(15),
phone varchar(24),
fax varchar(24),
homepage text
)
SERVER northwind
OPTIONS (
filename :'nw_suppliers',
format 'csv',
header 'true',
delimiter '|',
null 'NULL'
);
CREATE FOREIGN TABLE nw_territories (
territoryid varchar(20),
territorydescription char(50),
regionid int
)
SERVER northwind
OPTIONS (
filename :'nw_territories',
format 'csv',
header 'true',
delimiter '|'
);
CREATE GRAPH northwind_graph;
SET graph_path = northwind_graph;
LOAD FROM nw_categories AS r CREATE (:categories =to_jsonb(r));
LOAD FROM nw_customers AS r CREATE (:customers =to_jsonb(r));
LOAD FROM nw_employees AS r CREATE (:employees =to_jsonb(r));
LOAD FROM nw_orders AS r CREATE (:orders =to_jsonb(r));
LOAD FROM nw_products AS r CREATE (:products =to_jsonb(r));
LOAD FROM nw_regions AS r CREATE (:regions =to_jsonb(r));
LOAD FROM nw_shippers AS r CREATE (:shippers =to_jsonb(r));
LOAD FROM nw_suppliers AS r CREATE (:suppliers =to_jsonb(r));
LOAD FROM nw_territories AS r CREATE (:territories =to_jsonb(r));
CREATE PROPERTY INDEX ON categories (categoryid);
CREATE PROPERTY INDEX ON customers (customerid);
CREATE PROPERTY INDEX ON employees (employeeid);
CREATE PROPERTY INDEX ON orders (orderid);
CREATE PROPERTY INDEX ON products (productid);
CREATE PROPERTY INDEX ON regions (regionid);
CREATE PROPERTY INDEX ON shippers (shipperid);
CREATE PROPERTY INDEX ON suppliers (supplierid);
CREATE PROPERTY INDEX ON territories (territoryid);
LOAD FROM nw_employee_territories AS r
MATCH (e:employees), (t:territories)
WHERE e.employeeid = to_jsonb(r.employeeid) AND
t.territoryid = to_jsonb(r.territoryid)
CREATE (e)-[:belongs_to]->(t);
LOAD FROM nw_order_details AS r
MATCH (o:orders), (p:products)
WHERE o.orderid = to_jsonb(r.orderid) AND
p.productid = to_jsonb(r.productid)
CREATE (o)-[:contains {unitprice: r.unitprice,
quantity: r.quantity,
discount: r.discount}]->(p);
MATCH (e:employees), (b:employees) WHERE e.reportto = b.employeeid
CREATE (e)-[:reports_to]->(b);
MATCH (s:suppliers), (p:products) WHERE s.supplierid = p.supplierid
CREATE (s)-[:supplies]->(p);
MATCH (p:products), (c:categories) WHERE p.categoryid = c.categoryid
CREATE (p)-[:is_part_of]->(c);
MATCH (t:territories), (r:regions) WHERE t.regionid = r.regionid
CREATE (t)-[:is_in]->(r);
MATCH (c:customers), (o:orders) WHERE c.customerid = o.customerid
CREATE (c)-[:makes]->(o);
MATCH (e:employees), (o:orders) WHERE e.employeeid = o.employeeid
CREATE (e)-[:manages]->(o);
MATCH (o:orders), (s:shippers) WHERE o.shipvia = s.shipperid
CREATE (o)-[:is_delivered_by]->(s);
COMMIT;