-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject_database.sql
385 lines (171 loc) · 5.7 KB
/
project_database.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
-- Table Creation
CREATE TABLE Users (
userID INT AUTO_INCREMENT,
phoneNumber VARCHAR(15) NOT NULL,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
numberOfContacts INT DEFAULT 0,
firstName VARCHAR(50) NOT NULL,
middleName VARCHAR(50),
lastName VARCHAR(50) NOT NULL,
zip VARCHAR(10) NOT NULL,
state VARCHAR(50) NOT NULL,
city VARCHAR(50) NOT NULL,
streetNumber VARCHAR(10) NOT NULL,
streetName VARCHAR(100) NOT NULL,
PRIMARY KEY (userID)
);
CREATE TABLE User_Social_Contact (
userID INT NOT NULL,
phoneNumber VARCHAR(15) NOT NULL,
firstName VARCHAR(50) NOT NULL,
middleName VARCHAR(50),
lastName VARCHAR(50) NOT NULL,
PRIMARY KEY (userID, phoneNumber),
FOREIGN KEY (userID) REFERENCES Users(userID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE Reminder (
reminderID INT AUTO_INCREMENT,
content TEXT NOT NULL,
frequency VARCHAR(20) NOT NULL,
PRIMARY KEY (reminderID)
);
CREATE TABLE Refers (
reminderID INT NOT NULL,
phoneNumber VARCHAR(15) NOT NULL,
userID INT NOT NULL,
PRIMARY KEY (reminderID, phoneNumber, userID),
FOREIGN KEY (reminderID) REFERENCES Reminder(reminderID)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (userID, phoneNumber) REFERENCES User_Social_Contact(userID, phoneNumber)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE Reminds (
userID INT NOT NULL,
reminderID INT NOT NULL,
format VARCHAR(20) NOT NULL,
PRIMARY KEY (userID, reminderID),
FOREIGN KEY (userID) REFERENCES Users(userID)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (reminderID) REFERENCES Reminder(reminderID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE Payment (
paymentID INT AUTO_INCREMENT,
amount DECIMAL(10, 2) NOT NULL,
invoiceNumber VARCHAR(50) NOT NULL,
paymentDate DATE NOT NULL,
cardAddress VARCHAR(200),
type_of_card VARCHAR(50),
cardName VARCHAR(100),
CVV VARCHAR(4),
cardNumber VARCHAR(20),
cardExpDate DATE,
echeckAddress VARCHAR(200),
echeckAccNumber VARCHAR(20),
echeckRoutingNumber VARCHAR(20),
echeckName VARCHAR(100),
echeckFlag BOOLEAN,
cardFlag BOOLEAN,
PRIMARY KEY (paymentID)
);
CREATE TABLE Assist (
userID INT NOT NULL,
paymentID INT NOT NULL,
PRIMARY KEY (userID, paymentID),
FOREIGN KEY (userID) REFERENCES Users(userID)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (paymentID) REFERENCES Payment(paymentID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
-- Data Population
-- Insert into Users
INSERT INTO Users (
phoneNumber, email, password, numberOfContacts, firstName, middleName, lastName, zip, state, city, streetNumber, streetName
) VALUES
('1234567890', '[email protected]', 'password123', 3, 'John', 'A.', 'Doe', '12345', 'TN', 'Nashville', '12', 'Main St'),
('0987654321', '[email protected]', 'password456', 5, 'Jane', 'B.', 'Smith', '54321', 'NY', 'New York', '34', 'Broadway');
-- Insert into Reminder
INSERT INTO Reminder (content, frequency) VALUES
('Meeting with client', 'Weekly'),
('Pay credit card bill', 'Monthly');
-- Insert into Reminds
INSERT INTO Reminds (userID, reminderID, format) VALUES
(1, 1, 'Email Notification'),
(2, 2, 'SMS Notification');
-- Insert into User_Social_Contact
INSERT INTO User_Social_Contact (userID, phoneNumber, firstName, middleName, lastName) VALUES
(1, '6159876543', 'Michael', 'J.', 'Fox'),
(2, '2125678943', 'Sarah', 'L.', 'Connor');
-- Insert into Refers
INSERT INTO Refers (reminderID, phoneNumber, userID) VALUES
(1, '6159876543', 1),
(2, '2125678943', 2);
-- Insert into Payment
INSERT INTO Payment (
amount,
invoiceNumber,
paymentDate,
cardAddress,
type_of_card,
cardName,
CVV,
cardNumber,
cardExpDate,
echeckAddress,
echeckAccNumber,
echeckRoutingNumber,
echeckName,
echeckFlag,
cardFlag
) VALUES
(150.75, 'INV12345', '2024-11-01', '123 Card St', 'Credit', 'Visa', '123', '4111111111111111', '2025-12-01', NULL, NULL, NULL, NULL, FALSE, TRUE),
(200.50, 'INV67890', '2024-11-02', NULL, 'ECheck', NULL, NULL, NULL, NULL, '123 Echeck St', '987654321', '123456789', 'John Doe', TRUE, FALSE);
-- Insert into Assist
INSERT INTO Assist (userID, paymentID) VALUES
(1, 1),
(2, 2);
-- Sample Queries
-- 1. Query a single table
SELECT * FROM Users;
-- 2. Join query
SELECT u.firstName, r.content, p.amount
FROM Users u
JOIN Reminds rm ON u.userID = rm.userID
JOIN Reminder r ON rm.reminderID = r.reminderID
JOIN Assist a ON u.userID = a.userID
JOIN Payment p ON a.paymentID = p.paymentID;
-- 3. Aggregation query
SELECT SUM(amount) AS TotalPayments FROM Payment;
-- 4. Insert operation
INSERT INTO Users (
phoneNumber, email, password, numberOfContacts, firstName, middleName, lastName, zip, state, city, streetNumber, streetName
) VALUES
('1231231234', '[email protected]', 'newpassword', 2, 'Alice', 'C.', 'Brown', '67890', 'CA', 'Los Angeles', '56', 'Sunset Blvd');
-- 5. Update operation
UPDATE Payment SET amount = 175.00 WHERE paymentID = 1;
-- 6. Delete operation
DELETE FROM Reminder WHERE reminderID = 2;
-- 7. Total Payments Per User
SELECT
u.userID,
u.firstName,
u.lastName,
COUNT(p.paymentID) AS totalPaymentsMade,
SUM(p.amount) AS totalPaymentAmount
FROM
Users u
JOIN
Assist a ON u.userID = a.userID
JOIN
Payment p ON a.paymentID = p.paymentID
GROUP BY
u.userID, u.firstName, u.lastName;