-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.sql
544 lines (491 loc) · 11.9 KB
/
mysql.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/************************************
* DROP CONSTRAINTS
************************************/
ALTER TABLE CONTACT DROP FOREIGN KEY fk1_contact;
ALTER TABLE CONTACT DROP FOREIGN KEY fk2_contact;
ALTER TABLE SYSTEM_USER DROP FOREIGN KEY fk1_system_user;
ALTER TABLE SYSTEM_USER DROP FOREIGN KEY fk2_system_user;
ALTER TABLE PROJECT DROP FOREIGN KEY fk1_project;
ALTER TABLE PROJECT DROP FOREIGN KEY fk2_project;
ALTER TABLE PROJECT DROP FOREIGN KEY fk3_project;
ALTER TABLE INTERACTION DROP FOREIGN KEY fk1_interaction;
ALTER TABLE INTERACTION DROP FOREIGN KEY fk2_interaction;
ALTER TABLE INTERACTION DROP FOREIGN KEY fk3_interaction;
ALTER TABLE INTER_PRO_JUNC DROP FOREIGN KEY fk1_inter_pro_junc;
ALTER TABLE INTER_PRO_JUNC DROP FOREIGN KEY fk2_inter_pro_junc;
ALTER TABLE MESSAGE DROP FOREIGN KEY fk1_message;
/************************************
* DROP TABLES
************************************/
DROP TABLE CONTACT;
DROP TABLE COMMON_LOOKUP;
DROP TABLE SYSTEM_USER;
DROP TABLE MESSAGE;
DROP TABLE INTERACTION;
DROP TABLE PROJECT;
DROP TABLE INTER_PRO_JUNC;
/************************************
* CREATE TABLES/CONSTRAINTS
************************************/
CREATE TABLE COMMON_LOOKUP(
COMMON_LOOKUP_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, COMMON_LOOKUP_TABLE VARCHAR(30) NOT NULL
, COMMON_LOOKUP_COLUMN VARCHAR(30) NOT NULL
, COMMON_LOOKUP_TYPE VARCHAR(30)
, COMMON_LOOKUP_CODE VARCHAR(8)
, COMMON_LOOKUP_MEANING VARCHAR(255)
, CREATED_BY INT NOT NULL
, CREATION_DATE DATE NOT NULL
, LAST_UPDATED_BY INT NOT NULL
, LAST_UPDATED_DATE DATE NOT NULL
);
CREATE TABLE CONTACT(
CONTACT_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, FK_CONTACT_TYPE INT NOT NULL
, LAST_NAME VARCHAR(40) NOT NULL
, FIRST_NAME VARCHAR(40) NOT NULL
, PRIMARY_PHONE INT NOT NULL
, SECONDARY_PHONE INT
, ADDRESS VARCHAR(120)
, EMAIL VARCHAR(60) NOT NULL
, FK_DEFAULT_CONTACT_METHOD INT NOT NULL
, CREATED_BY INT NOT NULL
, CREATION_DATE DATE NOT NULL
, LAST_UPDATED_BY INT NOT NULL
, LAST_UPDATED_DATE DATE NOT NULL
);
ALTER TABLE CONTACT ADD CONSTRAINT fk1_contact
FOREIGN KEY (FK_CONTACT_TYPE) REFERENCES COMMON_LOOKUP(COMMON_LOOKUP_ID);
ALTER TABLE CONTACT ADD CONSTRAINT fk2_contact
FOREIGN KEY (FK_DEFAULT_CONTACT_METHOD) REFERENCES COMMON_LOOKUP(COMMON_LOOKUP_ID);
CREATE TABLE SYSTEM_USER(
SYSTEM_USER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, SYSTEM_USER_NAME VARCHAR(20) NOT NULL
, FK_CONTACT_ID INT NOT NULL
, FK_SYSTEM_USER_TYPE INT
, CREATED_BY INT NOT NULL
, CREATION_DATE DATE NOT NULL
, LAST_UPDATED_BY INT NOT NULL
, LAST_UPDATED_DATE DATE NOT NULL
);
ALTER TABLE SYSTEM_USER ADD CONSTRAINT fk1_system_user
FOREIGN KEY (FK_SYSTEM_USER_TYPE) REFERENCES COMMON_LOOKUP(COMMON_LOOKUP_ID);
CREATE TABLE PROJECT(
PROJECT_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, FK_CONTACT_ID INT
, FK_SYSTEM_USER_ID INT
, FK_SEVERITY INT
, DUE_DATE DATE
, NEXT_RESPONSE_DUE DATE
, CREATED_BY INT
, CREATION_DATE DATE
, LAST_UPDATED_BY INT
, LAST_UPDATED_DATE DATE
);
ALTER TABLE PROJECT ADD CONSTRAINT fk1_project
FOREIGN KEY (FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID);
ALTER TABLE PROJECT ADD CONSTRAINT fk2_project
FOREIGN KEY (FK_SYSTEM_USER_ID) REFERENCES SYSTEM_USER(SYSTEM_USER_ID);
ALTER TABLE PROJECT ADD CONSTRAINT fk3_project
FOREIGN KEY (FK_SEVERITY) REFERENCES COMMON_LOOKUP(COMMON_LOOKUP_ID);
CREATE TABLE INTERACTION(
INTERACTION_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, FK_SYSTEM_USER_ID INT NOT NULL
, FK_CONTACT_ID INT NOT NULL
, FK_INTERACTION_TYPE INT NOT NULL
, INTERACTION_SOURCE_OBJECT TEXT NOT NULL
, CREATED_BY INT
, CREATION_DATE DATE
, LAST_UPDATED_BY INT
, LAST_UPDATED_DATE DATE
);
ALTER TABLE INTERACTION ADD CONSTRAINT fk1_interaction
FOREIGN KEY (FK_SYSTEM_USER_ID) REFERENCES SYSTEM_USER(SYSTEM_USER_ID);
ALTER TABLE INTERACTION ADD CONSTRAINT fk2_interaction
FOREIGN KEY (FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID);
ALTER TABLE INTERACTION ADD CONSTRAINT fk3_interaction
FOREIGN KEY (FK_INTERACTION_TYPE) REFERENCES COMMON_LOOKUP(COMMON_LOOKUP_ID);
CREATE TABLE INTER_PRO_JUNC(
INTER_PRO_JUNC_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, FK_INTERACTION_ID INT
, FK_PROJECT_ID INT
);
ALTER TABLE INTER_PRO_JUNC
ADD CONSTRAINT fk1_inter_pro_junc
FOREIGN KEY (FK_INTERACTION_ID) REFERENCES INTERACTION(INTERACTION_ID) ON DELETE CASCADE;
ALTER TABLE INTER_PRO_JUNC
ADD CONSTRAINT fk2_inter_pro_junc
FOREIGN KEY (FK_PROJECT_ID) REFERENCES PROJECT(PROJECT_ID) ON DELETE CASCADE;
CREATE TABLE MESSAGE(
MESSAGE_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT
, FK_INTERACTION_ID INT
, MESSAGE VARCHAR(1000) NOT NULL
, CREATED_BY INT NOT NULL
, CREATION_DATE DATE NOT NULL
, LAST_UPDATED_BY INT NOT NULL
, LAST_UPDATED_DATE DATE NOT NULL
);
ALTER TABLE MESSAGE ADD CONSTRAINT fk1_message
FOREIGN KEY (FK_INTERACTION_ID) REFERENCES INTERACTION(INTERACTION_ID);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'PROJECT'
, 'FK_SEVERITY'
, 'Severity'
, 1
, 'One'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
/************************************
* INERST DATA
************************************/
/*
COMMON_LOOKUP INSERTS
*/
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'SYSTEM_USER'
, 'FK_SYSTEM_USER_TYPE'
, 'System User'
, 1
, 'System User'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'CONTACT'
, 'FK_CONTACT_TYPE'
, 'Contact Type'
, 'CT'
, 'Customer'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'CONTACT'
, 'FK_DEFAULT_CONTACT_METHOD'
, 'Contact Method'
, 'CM'
, 'Phone'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'CONTACT'
, 'FK_DEFAULT_CONTACT_METHOD'
, 'Contact Method'
, 'CM'
, 'Email'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'CONTACT'
, 'FK_DEFAULT_CONTACT_METHOD'
, 'Contact Method'
, 'CM'
, 'Mail'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'PROJECT'
, 'FK_SEVERITY'
, 'Severity'
, 1
, 'One'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'PROJECT'
, 'FK_SEVERITY'
, 'Severity'
, 2
, 'Two'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
INSERT INTO COMMON_LOOKUP(
COMMON_LOOKUP_ID
, COMMON_LOOKUP_TABLE
, COMMON_LOOKUP_COLUMN
, COMMON_LOOKUP_TYPE
, COMMON_LOOKUP_CODE
, COMMON_LOOKUP_MEANING
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE
)
VALUES(
NULL
, 'PROJECT'
, 'FK_SEVERITY'
, 'Severity'
, 3
, 'Three'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
/*
SYSTEM_USER INSERTS
*/
INSERT INTO SYSTEM_USER(
SYSTEM_USER_ID
, SYSTEM_USER_NAME
, FK_CONTACT_ID
, FK_SYSTEM_USER_TYPE
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE)
VALUES(
NULL
, 'SYSTEM'
, 1
, 1
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
/*
CONTACT INSERTS
*/
INSERT INTO CONTACT(
CONTACT_ID
, FK_CONTACT_TYPE
, LAST_NAME
, FIRST_NAME
, PRIMARY_PHONE
, SECONDARY_PHONE
, ADDRESS
, EMAIL
, FK_DEFAULT_CONTACT_METHOD
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE)
VALUES(
NULL
, (SELECT COMMON_LOOKUP_ID FROM COMMON_LOOKUP WHERE COMMON_LOOKUP_MEANING = 'Customer')
, 'Rob'
, 'Page'
, 12
, NULL
, '3727 West Mountain View Drive Rexburg, ID 83440'
, 'pager925@gmail.com'
, (SELECT COMMON_LOOKUP_ID FROM COMMON_LOOKUP WHERE COMMON_LOOKUP_MEANING = 'Phone')
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
ALTER TABLE SYSTEM_USER ADD CONSTRAINT fk2_system_user
FOREIGN KEY (FK_CONTACT_ID) REFERENCES CONTACT(CONTACT_ID);
/*
PROJECT INSERTS
*/
INSERT INTO PROJECT(
PROJECT_ID
, FK_CONTACT_ID
, FK_SYSTEM_USER_ID
, FK_SEVERITY
, DUE_DATE
, NEXT_RESPONSE_DUE
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE)
VALUES(
NULL
, (SELECT CONTACT_ID FROM CONTACT WHERE PRIMARY_PHONE = '12082065802')
, (SELECT SYSTEM_USER_ID FROM SYSTEM_USER WHERE SYSTEM_USER_ID = 1)
, (SELECT COMMON_LOOKUP_ID FROM COMMON_LOOKUP WHERE COMMON_LOOKUP_ID = 4)
, '2001-09-10'
, '2001-09-10'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
/*
INTERACTION INSERTS
*/
INSERT INTO INTERACTION(
INTERACTION_ID
, FK_SYSTEM_USER_ID
, FK_CONTACT_ID
, FK_INTERACTION_TYPE
, INTERACTION_SOURCE_OBJECT
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE)
VALUES(
NULL
, (SELECT SYSTEM_USER_ID FROM SYSTEM_USER WHERE SYSTEM_USER_ID = 1)
, (SELECT CONTACT_ID FROM CONTACT WHERE CONTACT_ID = 1)
, (SELECT COMMON_LOOKUP_ID FROM COMMON_LOOKUP WHERE COMMON_LOOKUP_MEANING = 'Phone')
, 'njkjn'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
/*
INTER_PRO_JUNC INSERTS
*/
INSERT INTO INTER_PRO_JUNC(
INTER_PRO_JUNC_ID
, FK_INTERACTION_ID
, FK_PROJECT_ID)
VALUES(
NULL
, (SELECT INTERACTION_ID FROM INTERACTION WHERE INTERACTION_ID = 1)
, (SELECT PROJECT_ID FROM PROJECT WHERE PROJECT_ID = 1)
);
/*
MESSAGE INSERTS
*/
INSERT INTO MESSAGE(
MESSAGE_ID
, FK_INTERACTION_ID
, MESSAGE
, CREATED_BY
, CREATION_DATE
, LAST_UPDATED_BY
, LAST_UPDATED_DATE)
VALUES(
NULL
, (SELECT INTERACTION_ID FROM INTERACTION WHERE INTERACTION_ID = 1)
, 'TEST MESSAGE ON FIRST INTERACTION'
, 1
, '2001-09-10'
, 1
, '2001-09-10'
);
/*TODO:
* Change phone number data type
*/