-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.sql
245 lines (209 loc) · 7.91 KB
/
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
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema quizza
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `quizza` ;
-- -----------------------------------------------------
-- Schema quizza
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `quizza` DEFAULT CHARACTER SET utf8mb4 ;
USE `quizza` ;
-- -----------------------------------------------------
-- Table `quizza`.`admins`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`admins` ;
CREATE TABLE IF NOT EXISTS `quizza`.`admins` (
`name` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`pass` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
PRIMARY KEY (`name`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`departments`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`departments` ;
CREATE TABLE IF NOT EXISTS `quizza`.`departments` (
`dept_code` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`dept_name` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
PRIMARY KEY (`dept_code`),
UNIQUE INDEX `dept_name_UNIQUE` (`dept_name` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`subjects`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`subjects` ;
CREATE TABLE IF NOT EXISTS `quizza`.`subjects` (
`subject_code` VARCHAR(8) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`subject_name` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`branch` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`semester` INT(2) NULL DEFAULT NULL,
`subject_passkey` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`no_of_quizzes` INT(3) NOT NULL,
PRIMARY KEY (`subject_code`),
INDEX `fk_subjects_1_idx` (`branch` ASC),
CONSTRAINT `fk_subjects_1`
FOREIGN KEY (`branch`)
REFERENCES `quizza`.`departments` (`dept_code`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`questions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`questions` ;
CREATE TABLE IF NOT EXISTS `quizza`.`questions` (
`question_id` INT(11) NOT NULL AUTO_INCREMENT,
`sub_code` VARCHAR(8) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`quiz_number` INT(3) NOT NULL,
`question` VARCHAR(400) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`answer` VARCHAR(80) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`option_2` VARCHAR(80) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`option_3` VARCHAR(80) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`option_4` VARCHAR(80) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
PRIMARY KEY (`question_id`),
INDEX `fk_questions_1` (`sub_code` ASC),
CONSTRAINT `fk_questions_1`
FOREIGN KEY (`sub_code`)
REFERENCES `quizza`.`subjects` (`subject_code`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
AUTO_INCREMENT = 83
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`quiz`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`quiz` ;
CREATE TABLE IF NOT EXISTS `quizza`.`quiz` (
`subject_code` VARCHAR(8) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`quiz_no` INT(3) NOT NULL,
PRIMARY KEY (`subject_code`, `quiz_no`),
CONSTRAINT `fk_quiz_1`
FOREIGN KEY (`subject_code`)
REFERENCES `quizza`.`subjects` (`subject_code`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`students`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`students` ;
CREATE TABLE IF NOT EXISTS `quizza`.`students` (
`usn` VARCHAR(10) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`name` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL,
`password` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`branch` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`semester` INT(1) NOT NULL,
PRIMARY KEY (`usn`),
INDEX `branch_idx` (`branch` ASC),
CONSTRAINT `branch`
FOREIGN KEY (`branch`)
REFERENCES `quizza`.`departments` (`dept_code`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
-- -----------------------------------------------------
-- Table `quizza`.`scores`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `quizza`.`scores` ;
CREATE TABLE IF NOT EXISTS `quizza`.`scores` (
`usn` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`subject_code` VARCHAR(45) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`quiz_no` INT(3) NOT NULL,
`score` INT(4) NOT NULL,
PRIMARY KEY (`usn`, `subject_code`, `quiz_no`),
INDEX `fk_scores_subcode_idx` (`subject_code` ASC),
INDEX `fk_scores_quizno_idx` (`quiz_no` ASC),
CONSTRAINT `fk_scores_subcode`
FOREIGN KEY (`subject_code`)
REFERENCES `quizza`.`quiz` (`subject_code`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_scores_usn`
FOREIGN KEY (`usn`)
REFERENCES `quizza`.`students` (`usn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
USE `quizza` ;
-- -----------------------------------------------------
-- procedure spGetQuizzes
-- -----------------------------------------------------
USE `quizza`;
DROP procedure IF EXISTS `quizza`.`spGetQuizzes`;
DELIMITER $$
USE `quizza`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `spGetQuizzes`( in student_usn varchar(10) )
BEGIN
select q.subject_code, subject_name, q.quiz_no
from quiz q, subjects s
where q.subject_code in
(
select s.subject_code
from subjects where
branch in
(
select branch from students where usn = student_usn
)
and
semester in
(
select semester from students where usn = student_usn
)
)
and
q.subject_code = s.subject_code
and
(q.subject_code, q.quiz_no)
not in
(
select sc.subject_code, sc.quiz_no
from scores sc, subjects s
where sc.usn = student_usn
)
order by quiz_no,subject_code;
END$$
DELIMITER ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
USE `quizza`;
DELIMITER $$
USE `quizza`$$
DROP TRIGGER IF EXISTS `quizza`.`questions_AFTER_INSERT` $$
USE `quizza`$$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `quizza`.`questions_AFTER_INSERT`
AFTER INSERT ON `quizza`.`questions`
FOR EACH ROW
BEGIN
IF(
select count(*)
from questions
where sub_code = ( new.sub_code )
and quiz_number = ( new.quiz_number )
) = 10
then
insert into quiz values (new.sub_code, new.quiz_number);
end if;
END$$
DELIMITER ;