-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD1_Patel_Script_Project.sql
More file actions
369 lines (317 loc) · 10.8 KB
/
D1_Patel_Script_Project.sql
File metadata and controls
369 lines (317 loc) · 10.8 KB
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
--drop all tables
drop table enrollments;
drop table courses;
drop table students;
drop table departments;
--drop all sequence
drop sequence seq_students;
drop sequence seq_courses;
drop sequence seq_departments;
drop sequence seq_enrollments;
-- create table departments
create table departments(
departmentID int primary key,
departmentName varchar(20)
);
--create table students
create table students (
studentID int primary key,
firstName varchar(30),
lastName varchar(30),
email varchar(80),
departmentID int,
foreign key (departmentID) references departments(departmentID)
);
--create table courses
create table courses (
courseID int primary key,
courseName varchar(50),
credits int,
departmentID int,
foreign key (departmentID) references departments(departmentID)
);
--create table enrollments
create table enrollments (
enrollmentID int primary key,
studentID int,
courseID int,
grade varchar(10),
foreign key (studentID) references students(studentID),
foreign key (courseID) references courses(courseID)
);
--create sequences
create sequence seq_students start with 1 increment by 1;
create sequence seq_departments start with 1 increment by 1;
create sequence seq_enrollments start with 1 increment by 1;
create sequence seq_courses start with 1 increment by 1;
--insert into departments table
insert into departments values(seq_departments.nextval, 'IS');
insert into departments values(seq_departments.nextval, 'CS');
insert into departments values(seq_departments.nextval, 'MATH');
insert into departments values(seq_departments.nextval, 'BIO');
--insert into courses
insert into courses values(seq_courses.NEXTVAL, 'IS 410', 3, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'IS 420', 3, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'IS 436', 3, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 202', 3, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 461', 3, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 447', 3, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'MATH 100', 3, find_department_id('MATH'));
insert into courses values(seq_courses.NEXTVAL, 'MATH 151', 3, find_department_id('MATH'));
insert into courses values(seq_courses.NEXTVAL, 'MATH 300', 3, find_department_id('MATH'));
insert into courses values(seq_courses.NEXTVAL, 'IS 610', 3, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'IS 620', 3, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'IS 700', 2, find_department_id('IS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 632', 3, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 620', 3, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'CMSC 700', 2, find_department_id('CS'));
insert into courses values(seq_courses.NEXTVAL, 'BIO 202', 3, find_department_id('BIO'));
insert into courses values(seq_courses.NEXTVAL, 'BIO 402', 3, find_department_id('BIO'));
insert into courses values(seq_courses.NEXTVAL, 'BIO 602', 3, find_department_id('BIO'));
insert into courses values(seq_courses.NEXTVAL, 'BIO 700', 2, find_department_id('BIO'));
--create helper function find_department_id
--input departmentname
--output departmentID
create or replace function find_department_id (dept varchar)
return int
IS
dept_id int;
begin
select departmentID into dept_id from departments where departmentName= dept;
return dept_id;
exception
when no_data_found then
return -1;
end;
/
--create helper function find_student_id_withname
--input student firstname, lastname
--output studentid
create or replace function find_student_id_withname(s_fname varchar, s_lname varchar) return int is
s_id int;
begin
select studentID into s_id from students where firstName= s_fname AND lastName=s_lname;
return s_id;
exception
when no_data_found then
return -1;
end;
/
--create helper function find_course_id_withname
--input course name
--output courseID
create or replace function find_course_id_withname(c_name varchar) return int is
c_id int;
begin
select courseID into c_id from courses where courseName= c_name;
return c_id;
exception
when no_data_found then
return -1;
end;
/
--create procedure add_student
--input student first name, last name , email , department name
--Output inserting student into students table
create or replace procedure add_student(fname varchar,lname varchar, e_mail varchar, dept varchar) is
dept_id int;
begin
dept_id:= find_department_id(dept); --getting dept id with use of helper
if dept_id<0 then
dbms_output.put_line('no department found with this name'); --exception error
else
--insertion into student table
insert into students values (seq_students.NEXTVAL,fname,lname,e_mail,dept_id);
end if;
exception
when others then
dbms_output.put_line('sql error'||sqlerrm);
end;
/
--create enroll_student procedure
--input student id, course id
--output inserting into enrollments table
create or replace procedure enroll_student(s_id int, c_id int) is
begin
if s_id >0 AND c_id >0 then
insert into enrollments values(seq_enrollments.NEXTVAL,s_id,c_id,'');
else
dbms_output.put_line('invalid student id or course id');
end if;
exception
when others then
dbms_output.put_line('sql error in procedure'||sqlerrm);
end;
/
--create annonymous block
set serveroutput on;
declare
s_id int;
c_id int;
-- cursor to print student table
cursor c_std is select * from students;
-- cursor to get courses from pattern
cursor c_courseid(course_pattern varchar) is select courseID from courses where courseName LIKE course_pattern||'%';
--cursor to select student and course name which are in CMSC or IS course
cursor c_output is select s.firstname, s.lastname, c.courseName from students s, courses c, enrollments e where s.studentid=e.studentid and c.courseid=e.courseID and (c.coursename Like 'CMSC%' OR c.coursename Like 'IS%');
begin
--inserting student with add_student procedure
begin
add_student('Collin','Cabobs','cabobs@gmail.com','IS');
exception
when others then
dbms_output.put_line('error while adding collin'||sqlerrm);
end;
begin
add_student('Isaac','Istiq','istiq@gmail.com','IS');
exception
when others then
dbms_output.put_line('error while adding Isaac'||sqlerrm);
end;
begin
add_student('Abel','Apron','apron@gmail.com','CS');
exception
when others then
dbms_output.put_line('error while adding Abel'||sqlerrm);
end;
begin
add_student('Ian','Iansson','iansson@gmail.com','CS');
exception
when others then
dbms_output.put_line('error while adding Ian'||sqlerrm);
end;
begin
add_student('Dick', 'Disney', 'disney@gmail.com', 'IS');
exception
when others then
dbms_output.put_line('error while adding Dick'||sqlerrm);
end;
begin
add_student('Mary', 'Myrtle', 'myrtle@gmail.com', 'MATH');
exception
when others then
dbms_output.put_line('error while adding mary myrtle'||sqlerrm);
end;
begin
add_student('Mary', 'Molley', 'molley@gmail.com', 'MATH');
exception
when others then
dbms_output.put_line('error while adding mary molley'||sqlerrm);
end;
begin
add_student('Mark', 'Miser', 'miser@gmail.com', 'MATH');
exception
when others then
dbms_output.put_line('error while adding mark'||sqlerrm);
end;
begin
add_student('Bruce', 'Benjie', 'benjie@gmail.com', 'BIO');
exception
when others then
dbms_output.put_line('error while adding Bruce'||sqlerrm);
end;
begin
add_student('Bandu', 'Bindu', 'bindu@gmail.com', 'BIO');
exception
when others then
dbms_output.put_line('error while adding Bindu'||sqlerrm);
end;
dbms_output.put_line(CHR(10));
-- select student table using cursor
dbms_output.put_line('Students table details');
dbms_output.put_line(CHR(10));
for i in C_std loop
dbms_output.put_line('Student first Name: '||i.firstName||' Last Name: '||i.lastName||' Email: '||i.email||' Department ID: '||i.departmentID);
end loop;
begin
s_id := find_student_id_withname('Mary','Myrtle');
c_id := find_course_id_withname('MATH 100');
if s_id>0 and c_id>0 then
enroll_student(s_id,c_id);
else
dbms_output.put_line('invalid student name or course name while inserting Mary');
end if;
exception
when others then
dbms_output.put_line('error while adding mary course');
end;
begin
s_id := find_student_id_withname('Mary','Myrtle');
c_id := find_course_id_withname('MATH 151');
if s_id>0 and c_id>0 then
enroll_student(s_id,c_id);
else
dbms_output.put_line('invalid student name or course name while inserting Mary');
end if;
exception
when others then
dbms_output.put_line('error while adding mary course');
end;
begin
s_id := find_student_id_withname('Mary','Myrtle');
c_id := find_course_id_withname('MATH 300');
if s_id>0 and c_id>0 then
enroll_student(s_id,c_id);
else
dbms_output.put_line('invalid student name or course name while inserting Mary');
end if;
exception
when others then
dbms_output.put_line('error while adding mary course');
end;
-- enrolling mark in all MATH course
begin
s_id := find_student_id_withname('Mark','Miser');
if s_id> 0 then
for i in c_courseid('MATH') loop
begin
enroll_student(s_id,i.courseID);
exception
when others then
dbms_output.put_line('error while enrolling Mark'||sqlerrm);
end;
end loop;
else
dbms_output.put_line('invalid student name in mark block');
end if;
end;
-- enrolling collin in all IS courses
begin
s_id := find_student_id_withname('Collin','Cabobs');
if s_id> 0 then
for i in c_courseid('IS') loop
begin
enroll_student(s_id,i.courseID);
exception
when others then
dbms_output.put_line('error while enrolling collin'||sqlerrm);
end;
end loop;
else
dbms_output.put_line('invalid student name in collin block');
end if;
end;
-- enrolling Abel in all CMSC courses
begin
s_id := find_student_id_withname('Abel','Apron');
if s_id>0 then
for i in c_courseid('CMSC') loop
begin
enroll_student(s_id,i.courseID);
exception
when others then
dbms_output.put_line('error while enrolling Abel'||sqlerrm);
end;
end loop;
else
dbms_output.put_line('invalid student name in abel block');
end if;
end;
--printing show all student names enrolled in either CMSC or IS courses and the courses they are enrolled in.
dbms_output.put_line('Students with CMSC or IS courses details');
dbms_output.put_line(CHR(10));
for i in C_output loop
dbms_output.put_line('Student first Name: '||i.firstName||' Last Name: '||i.lastName||' Course Name: '||i.courseName);
end loop;
commit;
end;