-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_setting.sql
268 lines (200 loc) · 5.29 KB
/
schema_setting.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
-- Alter the data types of the orders table
ALTER TABLE orders_table
ALTER COLUMN card_number
TYPE VARCHAR,
ALTER COLUMN store_code
TYPE VARCHAR,
ALTER COLUMN product_code
TYPE VARCHAR,
ALTER COLUMN date_uuid
TYPE UUID
USING date_uuid::uuid,
ALTER COLUMN user_uuid
TYPE UUID
USING user_uuid::uuid,
ALTER COLUMN product_quantity
TYPE SMALLINT;
-- Alter the data types of the dim_users table
ALTER TABLE dim_users
ALTER COLUMN first_name
TYPE VARCHAR(255),
ALTER COLUMN last_name
TYPE VARCHAR(255),
ALTER COLUMN country_code
TYPE VARCHAR,
ALTER COLUMN date_of_birth
TYPE DATE
USING date_of_birth::DATE,
ALTER COLUMN join_date
TYPE DATE
USING join_date::DATE,
ALTER COLUMN user_uuid
TYPE UUID
USING user_uuid::uuid;
/*
Combine latitude columns. Yo achieve this new column
is added and then combined lat and latituue columnsms are Updated t
latitude_m colmn, then the original colms are droped
and latitude_m renamed to latitude.
*/
ALTER TABLE dim_store_details
ADD COLUMN IF NOT EXISTS Latitude_m TEXT;
UPDATE dim_store_details
SET Latitude_m = CONCAT(lat, latitude);
UPDATE dim_store_details
SET latitude_m = ''
WHERE latitude_m = 'NULLNULL';
-- DROP cloumns
ALTER TABLE dim_store_details
DROP COLUMN lat,
DROP COLUMN latitude;
-- RENMS ERCOLUMNS
ALTER TABLE dim_store_details
RENAME COLUMN Latitude_m TO latitude;
DELETE FROM dim_store_details
WHERE longitude = 'NULL';
-- Convert N/A's to null so the columns can be converted
-- in Longitude and latitude.
UPDATE dim_store_details
SET longitude = NULL
WHERE longitude = 'N/A';
UPDATE dim_store_details
SET latitude = NULL
WHERE latitude = 'N/A';
-- ALter the data types of the dim_store_details table
ALTER TABLE dim_store_details
ALTER COLUMN longitude
TYPE FLOAT
USING longitude::double precision,
ALTER COLUMN locality
TYPE VARCHAR(255),
ALTER COLUMN store_code
TYPE VARCHAR,
ALTER COLUMN staff_numbers
TYPE SMALLINT
USING staff_numbers::smallint,
ALTER COLUMN opening_date
TYPE DATE
USING opening_date::DATE,
ALTER COLUMN store_type
TYPE character varying(255),
ALTER COLUMN latitude
TYPE FLOAT
USING latitude::double precision,
ALTER COLUMN country_code
TYPE VARCHAR,
ALTER COLUMN continent
TYPE VARCHAR(255);
-- Make store_type NULLABLE
ALTER TABLE dim_store_details
ALTER COLUMN store_type
DROP NOT NULL;
-- Alter dim_products table
ALTER TABLE dim_products
--REMOVE £ symbol from prices.
ALTER COLUMN product_price
TYPE FLOAT
USING REPLACE(product_price, '£', '')::float;
/*Add human readable specification to dim_products
table*/
ALTER TABLE dim_products
ADD COLUMN weight_class VARCHAR,
ALTER COLUMN weight
TYPE FLOAT
USING weight::float;
UPDATE dim_products
SET weight_class = 'light'
WHERE weight < 2;
UPDATE dim_products
SET weight_class = 'Mid_Sized'
WHERE weight >= 2 AND weight < 40;
UPDATE dim_products
SET weight_class = 'Heavy'
WHERE weight >= 40 AND weight < 140;
UPDATE dim_products
SET weight_class = 'Truck_Required'
WHERE weight >= 140;
-- Rename removed column
ALTER TABLE dim_products
RENAME COLUMN removed TO still_available;
/*
Convert Still_availble column valies
to allow for BOOLEAN type conversion
below
*/
UPDATE dim_products
SET still_available = 0
WHERE still_available = 'Removed';
UPDATE dim_products
SET still_available = 1
WHERE still_available = 'Still_avaliable';
/*
Alter the dim_products data types.
*/
ALTER TABLE dim_products
ALTER COLUMN ean
TYPE VARCHAR,
ALTER COLUMN still_available
TYPE BOOLEAN
USING still_available::boolean,
ALTER COLUMN date_added
TYPE DATE
USING date_added::DATE,
ALTER COLUMN uuid
TYPE UUID
USING uuid::uuid,
ALTER COLUMN product_code
TYPE VARCHAR;
-- ALter dim_date_times data types.
ALTER TABLE dim_date_times
ALTER COLUMN month
TYPE VARCHAR,
ALTER COLUMN year
TYPE VARCHAR,
ALTER COLUMN day
TYPE VARCHAR,
ALTER COLUMN time_period
TYPE VARCHAR,
ALTER COLUMN date_uuid
TYPE UUID
USING date_uuid::uuid;
-- ALter dim_card_details_times data types.
ALTER TABLE dim_card_details
ALTER COLUMN card_number
TYPE VARCHAR,
ALTER COLUMN expiry_date
TYPE VARCHAR,
ALTER COLUMN date_payment_confirmed
TYPE DATE
USING date_payment_confirmed::DATE;
-- Set the Primary Key for each dim_card_details
ALTER TABLE dim_card_details
ADD PRIMARY KEY (card_number);
-- Set the Primary Key for dim_date_times
ALTER TABLE dim_date_times
ADD PRIMARY KEY (date_uuid);
-- Set the Primary Key for dim_products
ALTER TABLE dim_products
ADD PRIMARY KEY (product_code);
-- Set the Primary Key for dim_store_details
ALTER TABLE dim_store_details
ADD PRIMARY KEY (store_code);
-- Set the Primary Key for dim_users
ALTER TABLE dim_users
ADD PRIMARY KEY (user_uuid);
-- Set the foreign keys for data tables
ALTER TABLE orders_table
ADD CONSTRAINT fk_date_uuid FOREIGN KEY (date_uuid)
REFERENCES dim_date_times (date_uuid);
ALTER TABLE orders_table
ADD CONSTRAINT fk_user_uuid FOREIGN KEY (user_uuid)
REFERENCES dim_users (user_uuid);
ALTER TABLE orders_table
ADD CONSTRAINT fk_product_code FOREIGN KEY (product_code)
REFERENCES dim_products (product_code);
ALTER TABLE orders_table
ADD CONSTRAINT fk_card_details FOREIGN KEY (card_number)
REFERENCES dim_card_details (card_number);
ALTER TABLE orders_table
ADD CONSTRAINT fk_store_code FOREIGN KEY (store_code)
REFERENCES dim_store_details (store_code);