-
Notifications
You must be signed in to change notification settings - Fork 0
/
3nf_tables_ddl.sql
348 lines (296 loc) · 10.6 KB
/
3nf_tables_ddl.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
CREATE SEQUENCE tab_3NF.seq_country_id ORDER;
COMMIT;
-- DROP SEQUENCE tab_3NF.seq_country_id;
CREATE TABLE tab_3NF.ce_country (
country_id NUMBER DEFAULT tab_3NF.seq_country_id.NEXTVAL,
country_id_src VARCHAR2(100),
country_name VARCHAR2(100),
country_emerg_id VARCHAR2(100),
country_source VARCHAR2(100),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE tab_3NF.ce_country ADD CONSTRAINT country_pk PRIMARY KEY ( country_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_country_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_country
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE tab_3NF.ce_country;
CREATE SEQUENCE tab_3NF.seq_region_id1 ORDER;
COMMIT;
-- DROP SEQUENCE tab_3NF.seq_tegion_id1;
CREATE TABLE tab_3NF.ce_region (
region_id NUMBER DEFAULT tab_3NF.seq_region_id1.NEXTVAL,
region_abbrev VARCHAR2(255),
region_country_id NUMBER NOT NULL,
region_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_region ADD CONSTRAINT region_pk PRIMARY KEY ( region_id );
ALTER TABLE ce_region
ADD CONSTRAINT region_country_fk FOREIGN KEY ( region_country_id )
REFERENCES ce_country ( country_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_region_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_region
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TRIGGER trg_ce_region_update_dt;
-- DROP TABLE tab_3NF.ce_region;
CREATE SEQUENCE tab_3NF.seq_city_id ORDER;
COMMIT;
CREATE TABLE tab_3NF.ce_city (
city_id NUMBER DEFAULT tab_3NF.seq_city_id.NEXTVAL,
city_name VARCHAR2(255),
city_region_id NUMBER NOT NULL,
city_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_city ADD CONSTRAINT city_pk PRIMARY KEY ( city_id );
ALTER TABLE ce_city
ADD CONSTRAINT city_region_fk FOREIGN KEY ( city_region_id )
REFERENCES ce_region ( region_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_city_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_city
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TRIGGER trg_ce_city_update_dt;
-- DROP TABLE tab_3NF.ce_city;
CREATE SEQUENCE tab_3NF.seq_location_id;
COMMIT;
CREATE TABLE tab_3NF.ce_location (
location_id NUMBER DEFAULT tab_3NF.seq_location_id.NEXTVAL,
location_lat VARCHAR2(100),
location_ltd VARCHAR2(100),
location_postal_code VARCHAR2(100),
location_city_id NUMBER NOT NULL,
location_source VARCHAR2(100),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_location ADD CONSTRAINT address_pk PRIMARY KEY ( location_id );
ALTER TABLE ce_location
ADD CONSTRAINT address_city_fk FOREIGN KEY ( location_city_id )
REFERENCES ce_city ( city_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_location_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_location
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TRIGGER trg_ce_location_update_dt;
-- DROP TABLE tab_3NF.ce_location;
CREATE SEQUENCE tab_3NF.seq_customer_id;
COMMIT;
CREATE TABLE tab_3NF.ce_customer (
customer_id NUMBER DEFAULT tab_3NF.seq_customer_id.NEXTVAL,
customer_id_src VARCHAR2(100),
customer_first_name VARCHAR2(100),
customer_last_name VARCHAR2(100),
customer_telephone_num VARCHAR2(100),
customer_email VARCHAR2(100),
customer_source VARCHAR2(100),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_customer ADD CONSTRAINT customer_pk PRIMARY KEY ( customer_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_customer_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_customer
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE tab_3NF.ce_customer;
-- DROP TRIGGER trg_ce_customer_update_dt;
CREATE SEQUENCE seq_product_id;
COMMIT;
CREATE SEQUENCE tab_3Nf.seq_seller_id;
COMMIT;
CREATE TABLE ce_seller (
seller_id NUMBER DEFAULT tab_3Nf.seq_seller_id.NEXTVAL,
seller_id_src VARCHAR2(100),
seller_telephone_num VARCHAR2(100),
seller_email VARCHAR2(100),
seller_source VARCHAR2(100),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_seller ADD CONSTRAINT warehouse_pk PRIMARY KEY ( seller_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_seller_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_seller
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE ce_seller;
CREATE SEQUENCE tab_3NF.seq_position_id ORDER;
COMMIT;
CREATE TABLE ce_position (
position_id NUMBER DEFAULT tab_3NF.seq_position_id.NEXTVAL,
position_name VARCHAR2(255),
position_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate,
update_dt DATE
);
ALTER TABLE ce_position ADD CONSTRAINT position_pk PRIMARY KEY ( position_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_position_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_position
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE ce_position;
CREATE SEQUENCE tab_3NF.seq_employee_id ORDER;
COMMIT;
CREATE TABLE ce_employee (
employee_id NUMBER DEFAULT tab_3NF.seq_employee_id.NEXTVAL,
employee_id_src VARCHAR2(255),
employee_first_name VARCHAR2(255),
employee_last_name VARCHAR2(255),
employee_telephone_num VARCHAR2(255),
employee_position_id NUMBER NOT NULL,
employee_source VARCHAR2(255),
employee_eff_sop DATE,
employee_eff_eop DATE,
is_active VARCHAR2(255) DEFAULT 'Active',
insert_dt DATE DEFAULT sysdate,
update_dt DATE
);
ALTER TABLE ce_employee ADD CONSTRAINT employee_pk PRIMARY KEY ( employee_id );
ALTER TABLE ce_employee
ADD CONSTRAINT employee_position_fk FOREIGN KEY ( employee_position_id )
REFERENCES ce_position ( position_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_employee_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_employee
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE ce_employee;
CREATE SEQUENCE tab_3NF.seq_store_id ORDER;
COMMIT;
CREATE TABLE ce_store (
store_id NUMBER DEFAULT tab_3NF.seq_store_id.NEXTVAL,
store_id_src VARCHAR2(255),
store_name VARCHAR2(255),
store_telephone_number VARCHAR2(255),
store_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate,
update_dt DATE
);
ALTER TABLE ce_store ADD CONSTRAINT store_pk PRIMARY KEY ( store_id );
CREATE OR REPLACE TRIGGER trg_ce_store_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_store
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE ce_store;
CREATE SEQUENCE tab_3NF.seq_category_id;
COMMIT;
CREATE TABLE tab_3NF.ce_category (
category_id NUMBER DEFAULT tab_3NF.seq_category_id.NEXTVAL,
category_name VARCHAR2(255),
category_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_category ADD CONSTRAINT category_pk PRIMARY KEY ( category_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_category_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_category
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE tab_3NF.ce_category;
-- DROP TRIGGER trg_ce_category_update_dt;
CREATE SEQUENCE tab_3NF.seq_product_id ORDER;
COMMIT;
CREATE TABLE ce_product (
product_id NUMBER DEFAULT tab_3NF.seq_product_id.NEXTVAL,
product_id_src VARCHAR2(255),
product_name VARCHAR2(255),
product_cat_id NUMBER NOT NULL,
product_len VARCHAR2(255),
product_wid VARCHAR2(255),
product_hei VARCHAR2(255),
product_wei VARCHAR2(255),
product_sop DATE,
product_eop DATE,
product_is_active VARCHAR2(255) DEFAULT 'Active',
product_source VARCHAR2(255),
insert_dt DATE DEFAULT sysdate NOT NULL,
update_dt DATE
);
ALTER TABLE ce_product ADD CONSTRAINT product_pk PRIMARY KEY ( product_id );
ALTER TABLE ce_product
ADD CONSTRAINT product_category_fk FOREIGN KEY ( product_cat_id )
REFERENCES ce_category ( category_id );
COMMIT;
CREATE OR REPLACE TRIGGER trg_ce_product_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_product
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;
-- DROP TABLE tab_3NF.ce_product;
CREATE SEQUENCE tab_3NF.seq_sale_id ORDER;
COMMIT;
CREATE SEQUENCE tab_3NF.seq_payment_id ORDER;
COMMIT;
CREATE TABLE ce_sales (
sale_id NUMBER DEFAULT tab_3NF.seq_sale_id.NEXTVAL NOT NULL,
sale_id_src VARCHAR2(255),
product_id NUMBER NOT NULL,
customer_id NUMBER NOT NULL,
store_id NUMBER NOT NULL,
seller_id NUMBER NOT NULL,
location_id NUMBER NOT NULL,
employee_id NUMBER NOT NULL,
unit_price VARCHAR2(255),
sale_timestamp TIMESTAMP,
sale_source VARCHAR2(255),
insert_dt DATE DEFAULT SYSDATE,
update_dt DATE
);
ALTER TABLE ce_sales ADD CONSTRAINT sales_pk PRIMARY KEY ( sale_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_location_fk FOREIGN KEY ( location_id )
REFERENCES ce_location ( location_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_customer_fk FOREIGN KEY ( customer_id )
REFERENCES ce_customer ( customer_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_employee_fk FOREIGN KEY ( employee_id )
REFERENCES ce_employee ( employee_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_product_fk FOREIGN KEY ( product_id )
REFERENCES ce_product ( product_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_store_fk FOREIGN KEY ( store_id )
REFERENCES ce_store ( store_id );
ALTER TABLE ce_sales
ADD CONSTRAINT sales_seller_fk FOREIGN KEY ( seller_id )
REFERENCES ce_seller ( seller_id );
CREATE OR REPLACE TRIGGER trg_ce_sales_update_dt
BEFORE INSERT OR UPDATE ON tab_3NF.ce_sales
FOR EACH ROW
BEGIN
:new.update_dt := sysdate;
END;