-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentManagementDB DDL.sql
494 lines (442 loc) · 13.2 KB
/
StudentManagementDB DDL.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
USE MASTER
GO
IF DB_ID('Student_Management_DB') IS NOT NULL
DROP DATABASE Student_Management_DB
GO
CREATE DATABASE Student_Management_DB
GO
USE Student_Management_DB
GO
-------------------------------------------------
--CREATE TABLES----------------------------------
-------------------------------------------------
CREATE TABLE tbl_Class(
ClassID INT IDENTITY NOT NULL,
ClassName VARCHAR(20) NOT NULL
CONSTRAINT PK_Class PRIMARY KEY(ClassID)
)
GO
CREATE TABLE tbl_Teacher(
TeacherID INT IDENTITY NOT NULL,
TeacherName VARCHAR(50) NOT NULL,
TPhone VARCHAR(11) NULL
CONSTRAINT PK_TeacherID PRIMARY KEY(TeacherID)
)
GO
CREATE TABLE tbl_Subject(
SubjectID INT IDENTITY NOT NULL,
SubjectName VARCHAR(30) NOT NULL,
CONSTRAINT PK_Course PRIMARY KEY(SubjectID)
)
GO
CREATE TABLE tbl_ExamType(
ExamTypeID INT IDENTITY NOT NULL,
ExamName VARCHAR(15) NOT NULL,
CONSTRAINT PK_ExamTypeID PRIMARY KEY(ExamTypeID)
)
GO
CREATE TABLE tbl_Grade(
GradeID INT IDENTITY NOT NULL,
GradeName VARCHAR(5) NOT NULL,
MinimumMarks INT NOT NULL,
MaximumMarks INT NOT NULL,
CONSTRAINT PK_Grade PRIMARY KEY(GradeID)
)
GO
CREATE TABLE tbl_AssignTeacher(
AssignTeacherID INT IDENTITY NOT NULL,
[YEAR] INT NOT NULL,
ClassID INT NOT NULL,
SubjectID INT NOT NULL,
TeacherID INT NOT NULL,
CONSTRAINT PK_CourseOffer PRIMARY KEY(AssignTeacherID),
CONSTRAINT FK_CourseOffer_ClassID FOREIGN KEY(ClassID) REFERENCES tbl_Class(ClassID),
CONSTRAINT FK_CourseOffer_SubjectID FOREIGN KEY(SubjectID) REFERENCES tbl_Subject(SubjectID),
CONSTRAINT FK_CourseOffer_TeacherID FOREIGN KEY(TeacherID) REFERENCES tbl_Teacher(TeacherID)
)
GO
CREATE TABLE tbl_Student(
StudentID INT IDENTITY NOT NULL,
[Name] VARCHAR(50) NOT NULL,
DateOfBirth DATE NULL,
SPhone VARCHAR(11) NULL,
StAddress VARCHAR(100) NULL
CONSTRAINT PK_Student PRIMARY KEY(StudentID)
)
GO
CREATE TABLE tbl_Addmission(
AddmissionID INT IDENTITY NOT NULL,
StudentID INT NOT NULL,
ClassID INT NOT NULL,
RollNumber INT NOT NULL,
AddmissionDate DATE DEFAULT(GETDATE()),
CONSTRAINT PK_AddmissionID PRIMARY KEY(AddmissionID),
CONSTRAINT FK_Addmission_Student FOREIGN KEY(StudentID) REFERENCES tbl_Student(StudentID),
CONSTRAINT FK_Addmission_Class FOREIGN KEY(ClassID) REFERENCES tbl_Class(ClassID)
)
GO
CREATE TABLE tbl_MarksEntry(
MarksEntryID INT IDENTITY NOT NULL,
AddmissionID INT NOT NULL,
SubjectID INT NOT NULL,
ExamTypeID INT NOT NULL,
Marks INT NOT NULL CHECK(Marks>=0 and Marks <=100),
EntryDate DATE NOT NULL DEFAULT(GETDATE()),
CONSTRAINT PK_MarksEntry PRIMARY KEY(MarksEntryID, AddmissionID),
CONSTRAINT FK_MarksEntry_AddmissionID FOREIGN KEY(AddmissionID) REFERENCES tbl_Addmission(AddmissionID),
CONSTRAINT FK_MarksEntry_SubjectID FOREIGN KEY(SubjectID) REFERENCES tbl_Subject(SubjectID),
CONSTRAINT FK_MarksEntry_ExamTypeID FOREIGN KEY(ExamTypeID) REFERENCES tbl_ExamType(ExamTypeID),
CONSTRAINT CHK_Marks CHECK(Marks >= 0 AND Marks <= 100)
)
GO
CREATE TABLE tbl_Audit(
ID INT IDENTITY NOT NULL,
AddmissionID INT NOT NULL,
SubjectID INT NOT NULL,
ExamTypeID INT NOT NULL,
Marks INT NOT NULL,
NewMarks INT NULL,
[Action] VARCHAR(15) NOT NULL,
[Date] DATETIME NOT NULL DEFAULT(GETDATE())
)
GO
----------------------------------------
--INDEX---------------------------------
----------------------------------------
CREATE NONCLUSTERED INDEX ix_auditNonCID
ON tbl_Audit(Marks)
GO
CREATE CLUSTERED INDEX ix_auditCID
ON tbl_Audit(ID)
GO
----------------------------------------
--CREATE PROCEDURE----------------------
----------------------------------------
--Procedure for Creating Class
CREATE PROCEDURE sp_CreateClass
@cName VARCHAR(20)
AS
BEGIN
INSERT tbl_Class(ClassName) VALUES (@cName)
END
GO
--Procedure for Adding Teachers
CREATE PROC sp_InsertTeacher
@tName VARCHAR(50), @tPhone VARCHAR(11)
AS
BEGIN
INSERT tbl_Teacher(TeacherName, TPhone) VALUES(@tName, @tPhone)
END
GO
--Procedure for Creating Subjects
CREATE PROC sp_CreateSubjects
@sName VARCHAR(30)
AS
BEGIN
INSERT tbl_Subject(SubjectName) VALUES(@sName)
END
GO
--Procedure for Creating Exam Types
CREATE PROC sp_CreateExams
@eName VARCHAR(15)
AS
BEGIN
INSERT tbl_ExamType(ExamName) VALUES(@eName)
END
GO
--Procedure for Creating Grades
CREATE PROCEDURE sp_InsertGrades
@gName VARCHAR(5), @MinMarks INT, @MaxMarks INT
AS
BEGIN
INSERT tbl_Grade(GradeName, MinimumMarks, MaximumMarks) VALUES(@gName, @MinMarks, @MaxMarks)
END
GO
--Procedure for Assign Teachers
CREATE PROCEDURE sp_AssignTeachers
@year INT, @classID INT, @subjectID INT, @teacherID INT
AS
BEGIN
IF NOT EXISTS (SELECT * FROM tbl_Class WHERE ClassID=@classID)
BEGIN
RAISERROR('Class is not found in system. Please add class first.', 16, 1)
END
IF NOT EXISTS (SELECT * FROM tbl_Subject WHERE SubjectID=@subjectID)
BEGIN
RAISERROR('Subject is not found in system. Please add subject first.', 16, 1)
END
IF NOT EXISTS (SELECT * FROM tbl_Teacher WHERE TeacherID=@teacherID)
BEGIN
RAISERROR('Teacher is not found in system. Please add teacher first.', 16, 1)
END
BEGIN TRY
BEGIN TRAN
INSERT tbl_AssignTeacher([YEAR], ClassID, SubjectID, TeacherID) VALUES(@year, @classID, @subjectID, @teacherID)
COMMIT TRAN
END TRY
BEGIN CATCH
PRINT 'Error occured for : ' + ERROR_MESSAGE()
ROLLBACK TRAN
END CATCH
END
GO
--Procedure for Adding Students
CREATE PROC sp_InsertStudents
@sName VARCHAR(50), @DOB DATE, @sPhone VARCHAR(11), @sAddress VARCHAR(100)
AS
BEGIN
INSERT tbl_Student([Name], DateOfBirth, SPhone, StAddress) VALUES(@sName, @DOB, @sPhone, @sAddress)
END
GO
--Procedure for Admit Students
CREATE PROC sp_AdmitStudent
@studentID INT, @classID INT, @rollNo INT
AS
BEGIN
IF NOT EXISTS(SELECT * FROM tbl_Student WHERE StudentID=@studentID)
BEGIN
RAISERROR('Student is not found in the system. Please add student first', 16, 1)
END
IF NOT EXISTS(SELECT * FROM tbl_Class WHERE ClassID=@classID)
BEGIN
RAISERROR('Class is not found in the system. Please add class first', 16, 1)
END
BEGIN TRY
BEGIN TRAN
IF @rollNo IS NULL OR @rollNo = 0
BEGIN
SELECT @rollNo = ISNULL(MAX(RollNumber), 0) + 1 FROM tbl_Addmission
WHERE ClassID=@classID
END
INSERT tbl_Addmission(StudentID, ClassID, RollNumber, AddmissionDate)
VALUES(@studentID, @classID, @rollNo, GETDATE())
COMMIT TRAN
END TRY
BEGIN CATCH
PRINT 'Error occured for : ' + ERROR_MESSAGE()
ROLLBACK TRANSACTION
END CATCH
END
GO
--Procedure for Marks Entry
CREATE PROCEDURE sp_MarksEntry
@addmissionID INT, @subjectID INT, @examTypeID INT, @marks INT
AS
BEGIN
IF NOT EXISTS(SELECT * FROM tbl_Addmission WHERE AddmissionID=@addmissionID)
BEGIN
RAISERROR('Student is not found in the system. Please add student first', 16, 1)
END
IF NOT EXISTS(SELECT * FROM tbl_Subject WHERE SubjectID=@subjectID)
BEGIN
RAISERROR('Subject is not found in the system. Please add subject first', 16, 1)
END
IF NOT EXISTS(SELECT * FROM tbl_ExamType WHERE ExamTypeID=@examTypeID)
BEGIN
RAISERROR('There is no exam like this. Please re-check it.', 16, 1)
END
BEGIN TRY
BEGIN TRAN
INSERT tbl_MarksEntry(AddmissionID, SubjectID, ExamTypeID, Marks) VALUES(@addmissionID, @subjectID, @examTypeID, @marks)
COMMIT TRAN
END TRY
BEGIN CATCH
PRINT 'Error occured for : '+ ERROR_MESSAGE()
ROLLBACK TRAN
END CATCH
END
GO
--Procedure to View Student By Grade
CREATE PROC sp_ViewGradePointByClass
@class VARCHAR(20)
AS
BEGIN
SELECT ClassName, RollNumber, [Name], SubjectName, AVG(Marks) AverageMarks,
(
SELECT GradeName FROM tbl_Grade
WHERE AVG(Marks) BETWEEN MinimumMarks AND MaximumMarks
) Grade
FROM tbl_Addmission ad
INNER JOIN tbl_MarksEntry mrk
ON ad.AddmissionID=mrk.AddmissionID
INNER JOIN tbl_Class cl
ON cl.ClassID=ad.ClassID
INNER JOIN tbl_Student st
ON st.StudentID=ad.StudentID
INNER JOIN tbl_Subject su
ON su.SubjectID=mrk.SubjectID
INNER JOIN tbl_ExamType ex
ON ex.ExamTypeID=mrk.ExamTypeID
WHERE ClassName = @class
GROUP BY ClassName, RollNumber, [Name], SubjectName
END
GO
--Procedure to View A Student Grade
CREATE PROC sp_ViewAStudentGrade
@class VARCHAR(20), @roll int
AS
BEGIN
SELECT ClassName, RollNumber, [Name], SubjectName, AVG(Marks) AverageMarks,
(
SELECT GradeName FROM tbl_Grade
WHERE AVG(Marks) BETWEEN MinimumMarks AND MaximumMarks
) Grade
FROM tbl_Addmission ad
INNER JOIN tbl_MarksEntry mrk
ON ad.AddmissionID=mrk.AddmissionID
INNER JOIN tbl_Class cl
ON cl.ClassID=ad.ClassID
INNER JOIN tbl_Student st
ON st.StudentID=ad.StudentID
INNER JOIN tbl_Subject su
ON su.SubjectID=mrk.SubjectID
INNER JOIN tbl_ExamType ex
ON ex.ExamTypeID=mrk.ExamTypeID
WHERE ClassName = @class and RollNumber=@roll
GROUP BY ClassName, RollNumber, [Name], SubjectName
END
GO
----------------------------------------------------------
--Update Procedure----------------------------------------
----------------------------------------------------------
--Update Teachers By Class And Subject
CREATE PROC sp_updateTeacher
@id int, @year int, @classID int, @subID int, @teacherID int
AS
BEGIN
UPDATE tbl_AssignTeacher
SET [YEAR]=@year, ClassID=@classID, SubjectID=@subID, TeacherID=@teacherID
WHERE AssignTeacherID=@id
END
GO
--Update Student Marks
CREATE PROC sp_updateMarks
@id int, @adID int, @subID int, @examID int, @marks int
AS
BEGIN
UPDATE tbl_MarksEntry
SET AddmissionID=@adID, SubjectID=@subID, ExamTypeID=@examID, Marks=@marks
WHERE MarksEntryID=@id
END
GO
------------------------------------
--VIEWS-----------------------------
------------------------------------
--Select Student By Class
CREATE VIEW vw_StudentByClass
AS
SELECT cl.ClassName [Class Name],
st.StudentID [Student ID],
ad.RollNumber [Roll Number],
st.[Name] [Student Name],
st.DateOfBirth [Date of Birth],
st.SPhone [Student Phone],
st.StAddress [Student Address],
ad.AddmissionDate [Addmission Date]
FROM tbl_Class cl
INNER JOIN tbl_Addmission ad
ON cl.ClassID=ad.ClassID
INNER JOIN tbl_Student st
ON st.StudentID=ad.StudentID
GO
--Select Teachers By Class
CREATE VIEW vw_TeacherByClass
AS
SELECT ass.[YEAR] [Year],
cl.ClassName [Class Name],
su.SubjectName [Subject Name],
te.TeacherName [Teacher Name],
te.TPhone [Teacher Phone]
FROM tbl_Class cl
INNER JOIN tbl_AssignTeacher ass
ON cl.ClassID=ass.ClassID
INNER JOIN tbl_Subject su
ON su.SubjectID=ass.SubjectID
INNER JOIN tbl_Teacher te
ON te.TeacherID=ass.TeacherID
GO
--View Marks By Students
CREATE VIEW vw_MarksByStudents
AS
SELECT ExamName, ClassName, RollNumber, [Name], SubjectName, Marks
FROM tbl_Addmission ad
INNER JOIN tbl_MarksEntry mrk
ON ad.AddmissionID=mrk.AddmissionID
INNER JOIN tbl_Class cl
ON cl.ClassID=ad.ClassID
INNER JOIN tbl_Student st
ON st.StudentID=ad.StudentID
INNER JOIN tbl_Subject su
ON su.SubjectID=mrk.SubjectID
INNER JOIN tbl_ExamType ex
ON ex.ExamTypeID=mrk.ExamTypeID
GO
CREATE VIEW vw_StudentMarksUpdate
AS
SELECT *
FROM tbl_Audit
WHERE [Action] = 'Updated'
GO
------------------------------------
--FUNCTIONS--------------------------
------------------------------------
CREATE FUNCTION fn_totalStudent(@classID int)
RETURNS INT
AS
BEGIN
SELECT @classID = ClassID FROM tbl_Addmission
RETURN
(
SELECT COUNT(StudentID)
FROM tbl_Addmission
WHERE classID=@classID
)
END
GO
--Table Valued Function To Find Top 3 Student By ClassName and ExamTypeID
CREATE FUNCTION fn_top3FromClasses(@class varchar(20),@exam varchar(15))
RETURNS TABLE
AS
RETURN
SELECT TOP 3 RollNumber, [Name], SUM(Marks) AS TotalMarks
FROM tbl_Addmission ad
INNER JOIN tbl_MarksEntry mrk
ON ad.AddmissionID=mrk.AddmissionID
INNER JOIN tbl_Class cl
ON cl.ClassID=ad.ClassID
INNER JOIN tbl_Student st
ON st.StudentID=ad.StudentID
INNER JOIN tbl_ExamType ex
ON ex.ExamTypeID=mrk.ExamTypeID
WHERE ClassName = @class and ex.ExamTypeID=@exam
GROUP BY RollNumber, [Name]
ORDER BY TotalMarks DESC
GO
----------------------------------------------
--TRIGGERS------------------------------------
----------------------------------------------
CREATE TRIGGER tr_MarksAuditInsert
ON tbl_MarksEntry
AFTER INSERT
AS
BEGIN
DECLARE @addID INT, @subID INT, @examID INT, @marks INT
SELECT @addID=AddmissionID,@subID=SubjectID,@examID=ExamTypeID,@marks=Marks FROM inserted
INSERT INTO tbl_Audit(AddmissionID, SubjectID, ExamTypeID, Marks, [Action])
VALUES(@addID, @subID, @examID, @marks, 'Inserted')
PRINT 'Successfully inserted Records in tbl_Audit table for further enquery.'
END
GO
CREATE TRIGGER tr_MarksAuditUpdate
ON tbl_MarksEntry
AFTER UPDATE
AS
BEGIN
DECLARE @addID INT, @subID INT, @examID INT, @marks INT, @newMarks int
SELECT @addID=AddmissionID,@subID=SubjectID,@examID=ExamTypeID,@marks=Marks FROM deleted
SELECT @newMarks=Marks from inserted
INSERT INTO tbl_Audit(AddmissionID, SubjectID, ExamTypeID, Marks, NewMarks, [Action])
VALUES(@addID, @subID, @examID, @marks, @newMarks, 'Updated')
PRINT 'Successfully inserted Records in tbl_Audit table for further enquery.'
END
GO