-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDML.sql
416 lines (352 loc) · 9.8 KB
/
DML.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
/*
Group Name: Team FEC (Group 35)
Group Members: Noland Seigler & Jennifer Um
Project Title: Federal Election Committee’s Candidate Funding Sources Database
Assignment: Project Step 3 Draft
*/
/*
READ Entities
amendment_indicators
candidates
candidate_office_records
committee_types
committees
contributions
contributor_types
cycles
election_years
incumbent_challenger_statuses
office_types
party_types
report_types
transaction_types
*/
SELECT * FROM `candidates`;
SELECT `candidate_office_records`.id,
fec_cand_id,
`candidate_office_records`.name,
ttl_receipts,
trans_from_auth,
coh_bop,
coh_cop,
cand_contrib,
cand_loans,
other_loans,
cand_loan_repay,
other_loan_repay,
debts_owed_by,
ttl_indiv_contrib,
cand_office_st,
cand_office_district,
pol_pty_contrib,
cvg_end_dt,
indiv_refund,
cmte_refund,
`office_types`.name as office_type,
`candidates`.email as candidate_email,
`party_types`.short_name as party_type,
`incumbent_challenger_statuses`.name as incumbent_challenger_status
FROM `candidate_office_records`
INNER JOIN `office_types` on `candidate_office_records`.office_types_id = `office_types`.id
LEFT OUTER JOIN `candidates` on `candidate_office_records`.candidates_id = `candidates`.id
INNER JOIN `party_types` on `candidate_office_records`.party_types_id = `party_types`.id
INNER JOIN `incumbent_challenger_statuses` on `candidate_office_records`.incumbent_challenger_statuses_id = `incumbent_challenger_statuses`.id;
SELECT `committees`.id, 'cmte_id', 'name', 'city', 'state', 'zip_code', `committee_types`.name as committee_type
FROM `committees`
INNER JOIN `committee_types`
ON `committees`.committee_types_id = `committee_types`.id;
SELECT `contributions`.id,
transaction_pgi,
image_num,
transaction_dt,
transaction_amt,
trans_id,
file_num,
memo_cd,
memo_text,
sub_id,
`committees`.name as committee,
`report_types`.name as report_type,
`transaction_types`.name as transaction_type,
`amendment_indicators`.name as amendment_indicator,
`contributor_types`.name as contributor_type
FROM `contributions`
INNER JOIN `committees` on `contributions`.committees_id = `committees`.id
INNER JOIN `report_types` on `contributions`.report_types_id = `report_types`.id
INNER JOIN `transaction_types` on `contributions`.transaction_types_id = `transaction_types`.id
INNER JOIN `amendment_indicators` on `contributions`.amendment_indicators_id = `amendment_indicators`.id
INNER JOIN `contributor_types` on `contributions`.contributor_types_id = `contributor_types`.id;
SELECT * FROM cycles;
SELECT * FROM election_years;
SELECT * FROM office_types;
SELECT * FROM contributor_types;
SELECT * FROM committee_types;
SELECT * FROM transaction_types;
SELECT * FROM report_types;
SELECT * FROM party_types;
SELECT * FROM incumbent_challenger_statuses;
SELECT * FROM amendment_indicators;
SELECT * FROM candidate_office_records_committees;
SELECT * FROM candidate_office_records_contributions;
SELECT * FROM election_years_contributions;
SELECT * FROM election_years_candidate_office_records;
SELECT * FROM cycles_contributions;
SELECT * FROM cycles_candidate_office_records;
-- END Read
/*
WRITE Entities
INSERT
amendment_indicators
candidates
candidate_office_records
committee_types
committees
contributions
contributor_types
cycles
election_years
incumbent_challenger_statuses
office_types
party_types
report_types
transaction_types
DELETE
Entity:
M:M:
UPDATE(Set Null)
Entity: candidate_office_record.candidate_id
*/
-- Queries use a colon : character to
-- denote the variables that will have data from the backend programming language
INSERT INTO `office_types` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `contributor_types` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `report_types` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `party_types` (code, short_name)
VALUES
(:code_input, :short_name_input);
INSERT INTO `incumbent_challenger_statuses` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `amendment_indicators` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `election_years` (year)
VALUES
(:year_input)
INSERT INTO `transaction_types` (code, name)
VALUES
(:code_input, :name_input);
INSERT INTO `candidates` (first_name, last_name, middle_name, email)
VALUES
(:first_name_input, :last_name_input, :middle_name_input, :email_input)
INSERT INTO `candidate_office_records` (
fec_cand_id,
name,
ttl_receipts,
trans_from_auth,
coh_bop,
coh_cop,
cand_contrib,
cand_loans,
other_loans,
cand_loan_repay,
other_loan_repay,
debts_owed_by,
ttl_indiv_contrib,
cand_office_st,
cand_office_district,
pol_pty_contrib,
cvg_end_dt,
indiv_refund,
cmte_refund,
office_types_id,
candidates_id,
party_types_id,
incumbent_challenger_statuses_id
) VALUES (
:fec_cand_id_input,
:name_input,
:ttl_receipts_input,
:trans_from_auth_input,
:coh_bop_input,
:coh_cop_input,
:cand_contrib_input,
:cand_loans_input,
:other_loans_input,
:cand_loan_repay_input,
:other_loan_repay_input,
:debts_owed_by_input,
:ttl_indiv_contrib_input,
:cand_office_st_input,
:cand_office_district_input,
:pol_pty_contrib_input,
:cvg_end_dt_input,
:indiv_refund_input,
:cmte_refund_input,
(SELECT id FROM `office_types` WHERE code = :office_types_code_input),
:optional if present -> (SELECT id FROM `candidates` WHERE email = :candidates_email_input_from_drop_down),
(SELECT id FROM `party_types` WHERE short_name = :short_name_input),
(SELECT id FROM `incumbent_challenger_statuses` WHERE code = :incumbent_challenger_statuses_code_input)
);
INSERT INTO `committee_types` (code, name, explanation)
VALUES
(:code_input, :name_input, :explanation_input)
INSERT INTO `committees` (
cmte_id,
name,
city,
state,
zip_code,
committee_types_id
) VALUES
(
:cmte_id_input,
:name_input,
:city_input,
:state_input,
:zip_code_input,
(SELECT id FROM `committee_types` WHERE name = :committee_types_name_input)
);
INSERT INTO `contributions` (
transaction_pgi,
image_num,
transaction_dt,
transaction_amt,
trans_id,
file_num,
memo_cd,
memo_text,
sub_id,
committees_id,
report_types_id,
transaction_types_id,
amendment_indicators_id,
contributor_types_id
) VALUES
(
:transaction_pgi_input,
:image_num_input,
:transaction_dt_input,
:transaction_amt_input,
:trans_id_input,
:file_num_input,
:memo_cd_input,
:memo_text_input,
:sub_id_input,
(SELECT id FROM `committees` WHERE cmte_id = :committees_cmte_id_input_from_dropdown),
(SELECT id FROM `report_types` WHERE code = :report_types_code_input_from_dropdown),
(SELECT id FROM `transaction_types` WHERE code = :transaction_types_code_input_from_dropdown),
(SELECT id FROM `amendment_indicators` WHERE code = :amendment_indicators_code_input_from_dropdown),
(SELECT id FROM `contributor_types` WHERE code = :contributor_types_code_input_from_dropdown);
)
-- EVEN YEARS ONLY FOR CYCLES
INSERT INTO `cycles` (year)
VALUES
(:year_input);
INSERT INTO `candidate_office_records_committees`(
candidate_office_records_id,
committees_id
) VALUES
(
(
SELECT id FROM `candidate_office_records`
WHERE id = :candidate_office_records_id
),
(
SELECT id FROM `committees`
WHERE id = :committees_id
)
);
INSERT INTO `candidate_office_records_contributions`(
candidate_office_records_id,
contributions_id
) VALUES
(
(
SELECT id FROM `candidate_office_records`
WHERE id = :candidate_office_records_id
),
(
SELECT id FROM `contributions`
WHERE id = :contributions_id
)
);
INSERT INTO `election_years_contributions`(
election_years_year,
contributions_id
) VALUES
(
(
SELECT year FROM `election_years`
WHERE year = :election_years_year
),
(
SELECT id FROM `contributions`
WHERE id = :contributions_id
)
);
INSERT INTO `cycles_contributions`(
cycles_year,
contributions_id
) VALUES
(
(
SELECT year FROM `cycles`
WHERE year = :cycles_year
),
(
SELECT id FROM `contributions`
WHERE id = :contributions_id
)
);
INSERT INTO `election_years_candidate_office_records`(
election_years_year,
candidate_office_records_id
) VALUES
(
:election_years_year_input_from_drop_down,
(
SELECT id FROM `candidate_office_records`,
WHERE id = :candidate_office_records_id
)
);
INSERT INTO `cycles_candidate_office_records`(
cycles_year,
candidate_office_records_id
) VALUES
(
:cycles_year,
(
SELECT id FROM `candidate_office_records`
WHERE id = :candidate_office_records_id
)
);
-- UPDATE
UPDATE `candidate_office_records`
SET candidates_id = NULL | (
SELECT id FROM `candidates`
WHERE email = :candidates_email_input_from_dropdown
)
WHERE name = :name_input_from_drop_down;
UPDATE `candidate_office_records_committees`
SET
candidate_office_records_id = (
SELECT id FROM `candidate_office_records`
WHERE fec_cand_id = :updated_fec_cand_id
),
committees_id = (
SELECT id FROM `committees`
WHERE cmte_id = :updated_cmte_id
)
WHERE
candidate_office_records_id = :candidate_office_records_id
AND committees_id = :committees_id;
-- DELETE: This cascades all the M:M mappings but does not cascade the other "Mapped Table"
DELETE FROM `candidate_office_records`
WHERE name = :name_input_from_drop_down;