-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.js
547 lines (432 loc) · 20 KB
/
database.js
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
545
546
547
var mysql = require('mysql');
var db;
function connectDatabase() {
if (!db) {
db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'mysql',
database : 'DBMS'
});
db.connect(function(err){
if(!err) {
let createTodos1 = `CREATE TABLE if not exists USER (
id INT NOT NULL AUTO_INCREMENT,
email varchar(100) COLLATE utf8_unicode_ci NOT NULL UNIQUE,
password varchar(255) COLLATE utf8_unicode_ci NOT NULL,
created datetime NOT NULL,
role varchar(100),
PRIMARY KEY(id))
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos1, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('User table created');
});
let createTodos3 = `CREATE TABLE if not exists STUDENT
(
Student_id INT NOT NULL AUTO_INCREMENT,
First_Name VARCHAR(255),
Middle_Name VARCHAR(255),
Last_Name VARCHAR(255),
User_id INT NOT NULL,
Email VARCHAR(255),
Phone_Number NUMERIC(11),
Roll_Number VARCHAR(255),
PRIMARY KEY (Student_id),
FOREIGN KEY (User_id) REFERENCES USER(id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos3, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Student table created');
});
let createTodos4 = `CREATE TABLE if not exists INSTRUCTOR
(
Instructor_id INT NOT NULL AUTO_INCREMENT,
User_id INT NOT NULL,
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Middle_Name VARCHAR(255),
Email VARCHAR(255),
Phone_Number NUMERIC(11),
Department_id INT,
PRIMARY KEY (Instructor_id),
FOREIGN KEY (User_id) REFERENCES USER(id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos4, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Instructor table created');
});
let createTodos5 = `CREATE TABLE if not exists COURSE
(
Course_id INT NOT NULL AUTO_INCREMENT,
Course_Title VARCHAR(255) NOT NULL,
Course_Code VARCHAR(255) NOT NULL,
Instructor_id INT NOT NULL,
Department_id INT,
Introduction TEXT,
Description TEXT,
Requirements TEXT,
PRIMARY KEY (Course_id),
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos5, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Course table created');
});
let createTodos6 = `CREATE TABLE if not exists COURSE_MATERIALS
(
Material_id INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(255),
Remark VARCHAR(255),
Material TEXT,
Course_id INT NOT NULL,
Instructor_id INT NOT NULL,
PRIMARY KEY (Material_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos6, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Course Material table created');
});
let createTodos7 = `CREATE TABLE if not exists TESTS
(
Test_id INT NOT NULL AUTO_INCREMENT,
Test_Name VARCHAR(100),
Maximun_Marks INT,
Question_Paper TEXT,
Answer_Key TEXT,
Course_id INT NOT NULL,
Instructor_id INT NOT NULL,
Date DATE,
PRIMARY KEY (Test_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos7, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Tests table created');
});
let createTodos8 = `CREATE TABLE if not exists TEST_RESULTS
(
Result_id INT NOT NULL AUTO_INCREMENT,
Marks_Obtained INT NOT NULL,
Test_id INT NOT NULL,
Student_id INT NOT NULL,
PRIMARY KEY (Result_id),
FOREIGN KEY (Test_id) REFERENCES TESTS(Test_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos8, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Test_Results table created');
});
let createTodos9 = `CREATE TABLE if not exists GRADE
(
Grade_id INT NOT NULL AUTO_INCREMENT,
Grade_Obtained CHAR(1) NOT NULL,
Final_Consolidated_Marks INT NOT NULL,
Student_id INT NOT NULL,
Course_id INT NOT NULL,
PRIMARY KEY (Grade_id),
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id) ON DELETE CASCADE,
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos9, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Grade table created');
});
let createTodos10 = `CREATE TABLE if not exists ANOUNCEMENT
(
Anouncement_id INT NOT NULL AUTO_INCREMENT,
Short_Description VARCHAR(50),
Anouncement_Details VARCHAR(255),
Link1 VARCHAR(1000),
Link2 VARCHAR(1000),
Link3 VARCHAR(1000),
Posted_on DATETIME,
Course_id INT,
Instructor_id INT NOT NULL,
PRIMARY KEY (Anouncement_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos10, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Announcement table created');
});
let createTodos11 = `CREATE TABLE if not exists Q_A
(
Q_A_id INT NOT NULL AUTO_INCREMENT,
Question VARCHAR(1000) NOT NULL,
Subject VARCHAR(1000) NOT NULL,
Student_id INT,
Instructor_id INT,
Name VARCHAR(255),
Course_id INT NOT NULL,
PRIMARY KEY (Q_A_id),
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos11, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Q_A table created');
});
let createTodos12 = `CREATE TABLE if not exists ANSWER
(
Answer_id INT NOT NULL AUTO_INCREMENT,
Answer VARCHAR(2500) NOT NULL,
Q_A_id INT NOT NULL,
Instructor_id INT,
Student_id INT,
Course_id INT,
Name VARCHAR(255),
PRIMARY KEY (Answer_id),
FOREIGN KEY (Q_A_id) REFERENCES Q_A(Q_A_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id),
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos12, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Answer table created');
});
let createTodos13 = `CREATE TABLE if not exists ENROLLED
(
Course_id INT NOT NULL,
Student_id INT NOT NULL,
PRIMARY KEY (Course_id, Student_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos13, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Enrolled table created');
});
let createTodos14 = `CREATE TABLE if not exists ASSIGNMENT
(
Assignment_id INT NOT NULL AUTO_INCREMENT,
Assignment_Name VARCHAR(100),
Maximun_Marks INT,
Assignment_File TEXT,
Solutions TEXT,
Course_id INT NOT NULL,
Instructor_id INT NOT NULL,
Date DATE,
PRIMARY KEY (Assignment_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos14, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Assignment table created');
});
let createTodos15 = `CREATE TABLE if not exists SUBMISSION
(
Submission_id INT NOT NULL AUTO_INCREMENT,
Submission_File TEXT,
Course_id INT NOT NULL,
Assignment_id INT NOT NULL,
Student_id INT NOT NULL,
PRIMARY KEY (Submission_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id) ON DELETE CASCADE,
FOREIGN KEY (Assignment_id) REFERENCES ASSIGNMENT(Assignment_id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos15, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Submission table created');
});
let createTodos16 = `CREATE TABLE if not exists ATTENDENCE
(
Attendence_id INT NOT NULL AUTO_INCREMENT,
Date DATE,
Attended INT,
Course_id INT NOT NULL,
Student_id INT NOT NULL,
PRIMARY KEY (Attendence_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id) ON DELETE CASCADE
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos16, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Attendance table created');
});
let createTodos17 = `CREATE TABLE if not exists REPLY
(
Reply_id INT NOT NULL AUTO_INCREMENT,
Reply VARCHAR(2500) NOT NULL,
Q_A_id INT NOT NULL,
Answer_id INT NOT NULL,
Instructor_id INT,
Student_id INT,
Course_id INT,
Name VARCHAR(255),
PRIMARY KEY (Reply_id),
FOREIGN KEY (Answer_id) REFERENCES ANSWER(Answer_id) ON DELETE CASCADE,
FOREIGN KEY (Q_A_id) REFERENCES Q_A(Q_A_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id),
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos17, function(err, results, fields) {
if (err) {
console.log(err.message);
}
else
console.log('Reply table created');
});
let createTodos18 = `CREATE TABLE if not exists PERMISSION
(
Permission_id INT NOT NULL AUTO_INCREMENT,
Course_id INT NOT NULL,
Student_id INT,
PRIMARY KEY (Permission_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos18, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Permission table created');
});
let createTodos19 = `CREATE TABLE if not exists PASSWORD
(
Password_id INT NOT NULL AUTO_INCREMENT,
User_id INT NOT NULL,
Secret VARCHAR(1000),
PRIMARY KEY (Password_id),
FOREIGN KEY (User_id) REFERENCES USER(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos19, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Password table created');
});
let createTodos181 = `CREATE TABLE if not exists Exams
(
Exam_id INT NOT NULL AUTO_INCREMENT,
Exam_Name VARCHAR(100),
Total_Questions INT,
Course_id INT NOT NULL,
Secret INT NOT NULL,
Instructor_id INT NOT NULL,
Date DATE,
PRIMARY KEY (Exam_id),
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos181, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Exams table created');
});
let createTodos191 = `CREATE TABLE if not exists Questions
(
Question_id INT NOT NULL AUTO_INCREMENT,
Question VARCHAR(100),
Op1 VARCHAR(100),
Op2 VARCHAR(100),
Op3 VARCHAR(100),
Op4 VARCHAR(100),
Correct_Option INT NOT NULL,
Exam_id INT NOT NULL,
Course_id INT NOT NULL,
Instructor_id INT NOT NULL,
PRIMARY KEY (Question_id),
FOREIGN KEY (Exam_id) REFERENCES Exams(Exam_id) ON DELETE CASCADE,
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Instructor_id) REFERENCES INSTRUCTOR(Instructor_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos191, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Questions table created');
});
let createTodos20 = `CREATE TABLE if not exists Exam_Submission
(
Exam_Submission_id INT NOT NULL AUTO_INCREMENT,
Question_id INT NOT NULL,
Answer_id INT ,
Mark INT,
Exam_id INT NOT NULL,
Course_id INT NOT NULL,
Student_id INT NOT NULL,
PRIMARY KEY (Exam_Submission_id),
FOREIGN KEY (Question_id) REFERENCES Questions(Question_id) ON DELETE CASCADE,
FOREIGN KEY (Exam_id) REFERENCES Exams(Exam_id) ON DELETE CASCADE,
FOREIGN KEY (Course_id) REFERENCES COURSE(Course_id) ON DELETE CASCADE,
FOREIGN KEY (Student_id) REFERENCES STUDENT(Student_id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`;
db.query(createTodos20, function(err, results, fields) {
if (err) {
console.log(err.message);
}
console.log('Exam-submission table created');
});
db.end(function(err) {
if (err) {
return console.log(err.message);
}
});
console.log('Database is connected!');
} else {
throw err;
}
});
}
return db;
}
module.exports = connectDatabase();