You are on page 1of 50

CST2355 - Database Systems

Assignment #3 Database Construction and Processing [15%]

This assignment relates to the following Course Learning Requirements:

CLR 2 - Develop Advanced Database Design and Normalization


CLR 4 - Use Oracle Procedural programming language (PL/SQL) to write programs
that contain SQL statements
CLR5 - Develop advanced Database Queries

Background

You are working for a college, designing a new grading system for students. From
talking with the administration, you drafted a conceptual data model. From it you
will construct a database and implement basic processing on the data.

Conceptul Data Model

1
2
Instructions

Follow the steps and ONLY submit your own work.


Adhere to the Academic Integrity policy -- no sharing, no copying, no group work

Use ONLY the following resources and tools --

 Modules 8 and 9
 Complex Programming video
 Tools specified by the instructions and your facilitator (no substitutions)
 Scripts provided with this assignment

Screenshot Check List. Make sure you have the following in each screenshots when
applicable --

 Maximize the output window


 Include ONLY the output specified in the instructions
 Include ONLY the information requested in a single screen shot
(exclude menus, toolbars, and object browsers)
 Put at the left upper corner your student name, student number, and
the today’s date in all screen shots
 Apply the correct naming standards in diagrams and manually created
SQL scripts for entities, tables, attributes, fields, and constraints
 Resize each object so all text you modified is visible
 Add your first and last name initials at the end of each table’s name
 Enlarge and crop your screen shot for ease of viewing

3
1
Intersection Tables

The conceptual data model specifies that there is a N:M relationship for PROGRAM
and COURSE. Currently the database implementation is a 1:N. You will change the
PROGRAM -- COURSE relationship to N:M by adding an intersection table.

A) Go to the SQL CLI tool and run the script


CST2355 - A3 - Create DB.SQL

B) Run the script again to ensure there are no run errors

C) Go to the SQL GUI tool and create a new script. Scan the Create DB script for
snippets of code to complete the following steps.

D) Add a statement to drop the foreign key constraint for PROGRAM -- COURSE

E) Add a CREATE TABLE statement

F) Name the table to be created based on the position of the PROGRAM entity to
the COURSE entity. If COURSE is left of PROGRAM the name is
COURSE_PROGRAM_INT_NN. If the COURSE is right of PROGRAM the name is
PROGRAM_COURSE_INT_NN. NN is your first and last name initials.

G) Add to the CREATE TABLE a primary key column ProgramCourseIntID and


primary key constraint ProgramCourseIntPK

H) Add to the CREATE TABLE a foreign key column ProgramID and a foreign key
constraint IntProgramFK for PROGRAM -- PROGRAM_COURSE_INT

I) Add TO THE CREATE TABLE a foreign key column CourseID and a foreign key
constraint IntCourseFK for COURSE -- PROGRAM_COURSE_INT

J) Before the CREATE TABLE statement, add a DROP TABLE statement to drop the
intersection table

K) After the CREATE TABLE statement add the following statement to add the data
to the intersection table by filling in the <intersection table> and <primary id>

INSERT INTO <intersection table> (<primary id>, ProgramID, CourseID)


SELECT C.CourseID, C.ProgramID, C.CourseID
FROM PROGRAM P JOIN COURSE C ON P.ProgramID = C.ProgramID;

COMMIT;

L) Add your name, student number, and today’s date as a comment on the first line
of your script

M) Go to the SQL CLI, clear your screen, maximize your screen, and then copy/paste
to run your script

4
N) Add a single screenshot of your script’s SQL and its results on the next page

5
Submit Intersection Table Script and Results

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

6
2
Database Change Request

In the conceptual data model there is a SectionNumber. This column was


implemented as a number data type. The school now wants SectionNumber to be
an alpha-numeric (containing both numbers and letters). You will change
SectionNumber to a VARCHAR2 column.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Create DB script and locate the code that
changes the size of the description field. Create a new script and copy/paste that
code into that script.

C) Change your new script so when ran it replaces the SectionNumber data type in
SECTION to VARCHAR2 (10)

D) Add your name, student number, and today’s date as a comment on the first line
of your script

E) Go to the SQL CLI, clear your screen, maximize your screen and then copy/paste
to run your script

F) Run DESC SECTION

G) Add a single screenshot of your script’s SQL and its results on the next page

7
Submit Database Change Request Script

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

8
Optional Parent and Mandatory Child

The benefits of a mandatory parent optional child is that no additional code needs to be added to
enforce the relationship. However, the conceptual data model shows PROFESSOR -- GRADE and
SECTION -- PROFESSOR are optional parent - mandatory child relationships. This means you need to
add additional code to enforce these relationships. You will use mandatory-mandatory code to create
optional-mandatory code.

3
Optional-Mandatory Insert

Create a triggers with the following logic


- inserting a professor and a new section if one doesn’t exist
- inserting only the new section when there is no professor specified
- inserting a new section and new professor when that professor doesn’t exist

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Mandatory - Mandatory script and locate the
INSERT section. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Change the comment from Mandatory - Mandatory to Optional - Mandatory

E) Rename columns, variables, and triggers. If named department (parent) then


change to professor (parent). If named program (child) then change to section
(child). Follow the naming standard (UPPER_CASE / ProperCase) used in the script.

F) In the PROFESSOR_INSERT trigger change the INSERT INTO PROFESSOR statement


to insert all new column values into professor

G) In the SECTION_INSERT trigger change the IF statement to specify that both


ProfessorCount = 0 and the new ProfessorID IS NOT NULL. Change the INSERT INTO
SECTION statement to insert all new column values accept for SectionID

H) Go to the SQL CLI, clear your screen, and then copy/paste to run your script

I) Add a single screenshot of your script’s SQL and its compiled results on the next
page

9
Submit Optional-Mandatory Insert Script

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

10
4
Testing Optional-Mandatory Insert

A) Go to the SQL CLI

B) Setup your tests by running the following…


DELETE FROM V_SECTION WHERE SectionID >= 31;
DELETE FROM V_PROFESSOR WHERE ProfessorID >= 6;
SET LINESIZE 200
COLUMN SectionID FORMAT 99
COLUMN Semester FORMAT A3
COLUMN SectionName FORMAT A20
COLUMN CourseID FORMAT 99
COLUMN ProfessorID FORMAT 99
COLUMN SectionNumber FORMAT A4
COLUMN FirstName FORMAT A10
COLUMN LastName FORMAT A10
COLUMN Email FORMAT A10

C) Clear the screen and add your name, student number, and today’s date as a
comment

D) Test to insert a SECTION with no PROFESSOR by running the following…


INSERT INTO V_SECTION (Semester, SectionNumber, StartDate, EndDate,
CourseID, ProfessorID)
VALUES ('22F', '450A', CURRENT_DATE, CURRENT_DATE, 1, null);
COMMIT;
SELECT * FROM SECTION WHERE SectionID >= 31;

E) Test to insert a SECTION and the new PROFESSOR specified…


INSERT INTO V_SECTION (Semester, SectionNumber, StartDate, EndDate,
CourseID, ProfessorID)
VALUES ('22F', '450B', CURRENT_DATE, CURRENT_DATE, 1, 6);
COMMIT;
SELECT * FROM SECTION WHERE SectionID >= 31;
SELECT * FROM PROFESSOR WHERE ProfessorID >= 6;

F) Add a single screenshot of your test results on the next page

11
Submit Optional-Mandatory Insert Test

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

12
5
Optional-Mandatory Delete

Create a trigger to unassociate a section from a professor to delete the professor

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Mandatory - Mandatory script and locate the
DELETE section. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Remove the PROGRAM_DELETE trigger


-- Optional - Mandatory
-- Student Name: Mishal Aboud
-- Student Number: 041048436
-- Date: 7/05/2023

-- Mandatory - Mandatory - INSERT --------------------------------------------

CREATE OR REPLACE VIEW V_DEPARTMENT AS SELECT * FROM DEPARTMENT;


CREATE OR REPLACE VIEW V_PROGRAM AS SELECT * FROM PROGRAM;

CREATE OR REPLACE TRIGGER DEPARTMENT_INSERT


INSTEAD OF INSERT ON V_DEPARTMENT FOR EACH ROW
BEGIN
INSERT INTO DEPARTMENT (DepartmentID, DepartmentName, Description)
VALUES (:NEW.DepartmentID, :NEW.DepartmentName, :NEW.Description);

INSERT INTO PROGRAM (DepartmentID)


VALUES (:NEW.DepartmentID);
END;
/

CREATE OR REPLACE TRIGGER PROGRAM_INSERT


INSTEAD OF INSERT ON V_PROGRAM FOR EACH ROW
DECLARE
varDepartmentCount NUMBER;
BEGIN
SELECT COUNT(*) INTO varDepartmentCount FROM DEPARTMENT WHERE
DepartmentID = :NEW.DepartmentID;
IF varDepartmentCount = 0 THEN
INSERT INTO DEPARTMENT (DepartmentID) VALUES (:NEW.DepartmentID);
END IF;

INSERT INTO PROGRAM (ProgramName, Description, DepartmentID)


VALUES (:NEW.ProgramName, :NEW.Description, :NEW.DepartmentID);
END;
/

13
-- Mandatory - Mandatory - UPDATE --------------------------------------------

CREATE OR REPLACE TRIGGER PROGRAM_UPDATE


INSTEAD OF UPDATE ON V_PROGRAM FOR EACH ROW
DECLARE
varDepartmentCount NUMBER;
BEGIN
UPDATE PROGRAM
SET ProgramName = :NEW.ProgramName,
Description = :NEW.Description
WHERE ProgramID = :OLD.ProgramID;

SELECT COUNT(*) INTO varDepartmentCount FROM DEPARTMENT WHERE


DepartmentID = :NEW.DepartmentID;

IF varDepartmentCount > 0 THEN


UPDATE PROGRAM
SET DepartmentID = :NEW.DepartmentID
WHERE ProgramID = :OLD.ProgramID;
END IF;

END;
/

-- Mandatory - Mandatory - DELETE --------------------------------------------

CREATE OR REPLACE TRIGGER DEPARTMENT_DELETE


INSTEAD OF DELETE ON V_DEPARTMENT FOR EACH ROW
BEGIN
DELETE FROM PROGRAM WHERE DepartmentID = :OLD.DepartmentID;
DELETE FROM DEPARTMENT WHERE DepartmentID = :OLD.DepartmentID;
END;
/
E) Rename columns, variables, and triggers. If named department (parent) then
change to professor (parent). If named program (child) then change to section
(child). Follow the naming standard (UPPER_CASE / ProperCase) used in the script.
-- Optional - Mandatory
-- Student Name: Mishal Aboud
-- Student Number: 041048436
-- Date: 7/05/2023

-- Mandatory - Mandatory - INSERT --------------------------------------------

CREATE OR REPLACE VIEW V_PROFESSOR AS SELECT * FROM DEPARTMENT;


CREATE OR REPLACE VIEW V_SECTION AS SELECT * FROM PROGRAM;

CREATE OR REPLACE TRIGGER PROFESSOR_INSERT


INSTEAD OF INSERT ON V_PROFESSOR FOR EACH ROW
BEGIN

INSERT INTO DEPARTMENT (DepartmentID, DepartmentName, Description)


VALUES (:NEW.DepartmentID, :NEW.DepartmentName, :NEW.Description);

INSERT INTO PROGRAM (DepartmentID)


VALUES (:NEW.DepartmentID);
END;

14
/

CREATE OR REPLACE TRIGGER SECTION_INSERT


INSTEAD OF INSERT ON V_SECTION FOR EACH ROW
DECLARE
varDepartmentCount NUMBER;
BEGIN

SELECT COUNT(*) INTO varDepartmentCount FROM DEPARTMENT WHERE


DepartmentID = :NEW.DepartmentID;
IF varDepartmentCount = 0 THEN
INSERT INTO DEPARTMENT (DepartmentID) VALUES (:NEW.DepartmentID);
END IF;

INSERT INTO PROGRAM (ProgramName, Description, DepartmentID)


VALUES (:NEW.ProgramName, :NEW.Description, :NEW.DepartmentID);
END;
/

-- Mandatory - Mandatory - UPDATE --------------------------------------------

CREATE OR REPLACE TRIGGER SECTION_UPDATE


INSTEAD OF UPDATE ON V_SECTION FOR EACH ROW
DECLARE
varDepartmentCount NUMBER;
BEGIN

UPDATE PROGRAM
SET ProgramName = :NEW.ProgramName,
Description = :NEW.Description
WHERE ProgramID = :OLD.ProgramID;

SELECT COUNT(*) INTO varDepartmentCount FROM DEPARTMENT WHERE


DepartmentID = :NEW.DepartmentID;

IF varDepartmentCount > 0 THEN


UPDATE PROGRAM
SET DepartmentID = :NEW.DepartmentID
WHERE ProgramID = :OLD.ProgramID;
END IF;

END;
/

-- Mandatory - Mandatory - DELETE --------------------------------------------

CREATE OR REPLACE TRIGGER PROFESSOR_DELETE


INSTEAD OF DELETE ON V_PROFESSOR FOR EACH ROW
BEGIN
DELETE FROM PROGRAM WHERE DepartmentID = :OLD.DepartmentID;
DELETE FROM DEPARTMENT WHERE DepartmentID = :OLD.DepartmentID;
END;
/

CREATE OR REPLACE TRIGGER SECTION_DELETE


INSTEAD OF DELETE ON V_SECTION FOR EACH ROW

15
DECLARE
varProgramCount NUMBER;
BEGIN
DELETE FROM PROGRAM WHERE ProgramID = :OLD.ProgramID;

SELECT COUNT(*) INTO varProgramCount FROM PROGRAM WHERE


DepartmentID = :OLD.DepartmentID;

IF varProgramCount = 0 THEN
DELETE FROM DEPARTMENT WHERE DepartmentID = :OLD.DepartmentID;
END IF;
END;

F) In the SECTION_DELETE trigger change DELETE FROM SECTION to UPDATE


SECTION and have it set the ProfessorID to NULL. Do not change the WHERE clause.
CREATE OR REPLACE TRIGGER SECTION_DELETE
INSTEAD OF DELETE ON V_SECTION FOR EACH ROW
DECLARE
varProgramCount NUMBER;
BEGIN
UPDATE PROGRAM
SET ProfessorID = NULL
WHERE ProgramID = :OLD.ProgramID;

DELETE FROM PROGRAM WHERE ProgramID = :OLD.ProgramID;

SELECT COUNT(*) INTO varProgramCount FROM PROGRAM WHERE


DepartmentID = :OLD.DepartmentID;

IF varProgramCount = 0 THEN
DELETE FROM DEPARTMENT WHERE DepartmentID = :OLD.DepartmentID;
END IF;
END;
/
G) Go to the SQL CLI, clear your screen, and then copy/paste to run your script

H) Add a single screenshot of your script’s SQL and its compiled results on the next
page

16
Submit Optional-Mandatory Delete

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

17
18
19
6
Batch Exports and Imports

In Windows you can do extracts using .BAT files in DOS. Each .BAT file runs a .CTL
or .CTRL file when extracting from and importing into the database. You will extract
records from and import records into your GRADE table.

A) Extract CST2355 - A3 - EXTRACT-IMPORT.ZIP to the folder C:\CST2355\A3.


Rename each file accordingly --
- EXTRACT_PROG.CTL and rename to EXTRACT_GRADE.CTL
- IMPORT_PROG.CTL and rename to IMPORT_GRADE.CTL

B) Modify EXTRACT.BAT using a text editor. Edit the file to specify --


- user and password for where your GRADE table is located
- CST2355\A3 path and the EXTRACT_GRADE CTL file name

C) Modify EXTRACT_GRADE.CTL using a text editor. Edit the file to specify --


- CST2355\A3 path and GRADE.CSV to file name to be created
- selecting all columns and rows from the grade table

D) Modify IMPORT.BAT using a text editor. Edit the file to specify --


- user and password for where your GRADE table is located
- CST2355\A3 path and the IMPORT_GRADE CTL file file name

E) Modify IMPORT_GRADE.CTL using a text editor. Edit the file to specify --


- path and file name for then GRADE.CSV to be imported
- change table to GRADE
- replace the columns with those from GRADE (same order as in GRADE)

F) Ensure all files are saved. Go to DOS and maximize the window

G) Copy and paste the following into DOS then hit enter

CLS
CD C:\Users\mmish\Downloads\assign3dataBase\CST2355 - A3 - EXTRACT-
IMPORT\EXTRACT-IMPORT
EXTRACT.BAT
EXIT
IMPORT.BAT

I) On the next line add your name, student number, and today’s date using REM.
For example --

Bob Smith, 0123456789, Jan 10, 2010

J) Add a single screenshot of DOS window on the next page

20
Submit Batch Export / Import Screenshot

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

21
7
Rollups

The GROUP BY options ROLLUP and CUBE allow you store a total in the same
column as the values that determine that total. It does this by adding addition rows
to your results.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - ProgramCount script and locate the ROLLUP
section. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Change your new script so it produces a ROLLUP like the following. Join the
PROGRAM_COURSE_INT_MA and COURSE tables. Then change the field used in the
COUNT aggregate function.

DepartmentName ProgramName CourseCount


Business Finance 3
Computer Science Computer 3
Programming
Engineering Electrical 3
Engineering
Humanities Philosphy 3
Mathematics Applied Math 3
Business 3
Computer Science 3
Engineering 3
Humanities 3
Mathematics 3
15

E) Go to the SQL CLI, clear your screen, and then copy/paste to run your script

F) Add a single screenshot of your script’s SQL and its results on the next page

22
Submit ROLLUP SELECT and Results

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

23
8
Pivots

Pivoting query results is heavily used for reporting. It allows users to compare
numbers and see patterns. A PIVOT is where you take data that is in rows and
convert it to show that same data as column headings.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - ProgramCount script and locate PIVOT section.
Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Run the script in SQL GUI and review the results. Determine how the results are
determined by how DEPARTMENT parent and PROGRAM child are used in the
query.

E) Change your new script to display course counts under three programs. For this
query there is a PROGRAM parent and COURSE child. Output should resemble the
following.

CourseName 'Applied 'Electrical 'Finance'


Math' Engineering'
BUS1000 0 0 1
BUS2000 0 0 1
BUS3000 0 0 1
CST1000 0 0 0
CST2000 0 0 0
CST3000 0 0 0
ENG1000 0 1 0
ENG2000 0 1 0
ENG3000 0 1 0
HUM1000 0 0 0
HUM2000 0 0 0
HUM3000 0 0 0
MAT1000 1 0 0
MAT2000 1 0 0
MAT3000 1 0 0

F) Go to the SQL CLI, clear your screen, and then copy/paste to run your script

G) Add a single screenshot of your script’s SQL and its results on the next page

24
Submit PIVOT SELECT and Results

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

25
9
Functions

You will create a new function that determines the letter grade for a student for a
student.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Create DB script and locate ACADEMIC


PACKAGE/PACKAGE BODY. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Change the package and package body names to GRADE_ENTRY (four changes)
and remove the PROCEDURE from the package and package body

E) Rename the StudentCount function in the package and package body to


GradeLookup (three changes). Change the function to have only the input
parameter IN_PercentGrade IN NUMBER in the package and package body. Change
the return datatype to VARCHAR in the package and package body.

F) Change all instances of varStudentCount to varLetterGrade. Change the


varLetterGrade to datatype VARCHAR(2).

G) Examine the GRADE_LOOKUP table CREATE statement in


CST2355 - A3 - Create DB

H) Replace the SELECT with a brand new SELECT that looks up the LetterGrade from
the GRADE LOOKUP table. For the WHERE clause use IN_PercentGrade,
varLetterGrade and the columns MinPercent & MaxPercent with the BETWEEN
operator.

H) Go to the SQL CLI, clear your screen, and then copy/paste to run your script. If
you get compile errors, use your SQL GUI to trouble shoot.

I) Add a single screenshot of your script’s SQL and its compile results on the next
page

26
Submit Function Script

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshots here>

27
28
10
Functions and Procedures in Triggers

You will use the GradeLoolup function in the GRADE_UPDATE Trigger.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Create DB script and locate GRADE_UPDATE


trigger. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Remove the IF statements

E) Set the :NEW.LetterGrade with the result from the GRADE_ENTRY.GradeLookup


function. The function uses the input parameter :NEW.PercentGrade

F) Go to the SQL CLI, clear your screen, and then copy/paste to run your script. If
you get errors, use the SQL GUI to trouble shoot.

G) To test your script run…


UPDATE GRADE SET LetterGrade = 'F';
SELECT * FROM GRADE;

H) Add a single screenshot of your script’s SQL, its compile results and your test
results on the next page

29
Submit Trigger Script and Test

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

30
11
Views

Views are objects created on base tables. A view can be treated like a table in
SELECT statements.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - View script and locate the CREATE VIEW
statement. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) For the FirstName and LastName being selected, change it to


STUDENT.FirstName and STUDENT.LastName

E) Add the the following fields to the SELECT. If the column is located in multiple
tables you will need to specify which table it’s coming from.

SectionID, CourseID, ProfessorID, SectionName, CourseName,


ProfessorFirstName, ProfessorLastName

F) INNER JOIN the COURSE to SECTION, and PROFESSOR to SECTION


-- Mishal Aboud - 7/10/2023 - 041048436
-- CST2355 - A3 - View: StudentManagement

CREATE OR REPLACE VIEW StudentManagement AS


SELECT
STUDENT.StudentID,
GRADE.GradeID,
STUDENT.FirstName,
STUDENT.LastName,
GRADE.PercentGrade,
SECTION.SectionID,
SECTION.CourseID,
SECTION.ProfessorID,
SECTION.SectionName,
COURSE.CourseName,
PROFESSOR.FirstName AS ProfessorFirstName,
PROFESSOR.LastName AS ProfessorLastName
FROM STUDENT
INNER JOIN GRADE ON GRADE.StudentID = STUDENT.StudentID
INNER JOIN SECTION ON SECTION.SectionID = GRADE.SectionID
INNER JOIN COURSE ON COURSE.CourseID = SECTION.CourseID
INNER JOIN PROFESSOR ON PROFESSOR.ProfessorID = SECTION.ProfessorID;
G) Go to the SQL CLI

H) Set up your test


SET LINESIZE 200
COLUMN StudentID FORMAT 99
COLUMN SectionID FORMAT 99

31
COLUMN CourseID FORMAT 99
COLUMN ProfessorID FORMAT 99
COLUMN FirstName FORMAT A10
COLUMN LastName FORMAT A10
COLUMN PercentGrade FORMAT 99
COLUMN SectionName FORMAT A15
COLUMN CourseName FORMAT A7
COLUMN ProfessorFirstName FORMAT A10
COLUMN ProfessorLastName FORMAT A10

I) Clear your screen, and then copy/paste to run your script

J) Test your view by running…


SELECT * FROM StudentManagement WHERE ROWNUM < 10;

K) Add a single screenshot of your script’s SQL, its compile results, and test results
on the next page

32
Submit VIEW Script and Results

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

33
12
Updatable Views

INSTEAD OF triggers are used on denormalized views to make the view update-able.
INSTEAD OF triggers override the behaviour. Instead of the INSERT/UPDATE/DELETE
being handled automatically by the database, you program the specific SQL to be
done. You will make the SectionName, CourseName in your view updatable.

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - View script and locate the


StudentManagement_UPDATE trigger. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Examine the script.

E) Copy the logic that makes FirstName updatable. Paste it after all the IF
statements. Change the IF and UPDATE to make CourseName updatable.

-- Mishal Aboud - 7/10/2023 - 041048436

CREATE OR REPLACE TRIGGER StudentManagement_UPDATE


INSTEAD OF UPDATE ON StudentManagement FOR EACH ROW
BEGIN
IF :OLD.FirstName <> :NEW.FirstName THEN
UPDATE STUDENT SET FirstName = :NEW.FirstName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.CourseName <> :NEW.CourseName THEN


UPDATE COURSE SET CourseName = :NEW.CourseName
WHERE CourseID = :OLD.CourseID;
END IF;

IF :OLD.LastName <> :NEW.LastName THEN


UPDATE STUDENT SET LastName = :NEW.LastName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.PercentGrade <> :NEW.PercentGrade THEN


UPDATE GRADE SET PercentGrade = :NEW.PercentGrade
WHERE GradeID = :OLD.GradeID;
END IF;

END;
/

SET PAGESIZE 30;

34
F) Copy the logic that makes FirstName and LastName updatable. Change the IF and
UPDATE to make ProfessorFirstName and ProfessorLastName updatable.
Note that the columns in the PROFESSOR table are FirstName and
LastName, while the :NEW variables in the trigger are ProfessorFirstName
and ProfessorLastName.
-- Mishal Aboud - 7/10/2023 - 041048436

CREATE OR REPLACE TRIGGER StudentManagement_UPDATE


INSTEAD OF UPDATE ON StudentManagement FOR EACH ROW
BEGIN
IF :OLD.FirstName <> :NEW.FirstName THEN
UPDATE STUDENT SET FirstName = :NEW.FirstName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.LastName <> :NEW.LastName THEN


UPDATE STUDENT SET LastName = :NEW.LastName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.ProfessorFirstName <> :NEW.ProfessorFirstName THEN


UPDATE PROFESSOR SET FirstName = :NEW.ProfessorFirstName
WHERE ProfessorID = :OLD.ProfessorID;
END IF;

IF :OLD.ProfessorLastName <> :NEW.ProfessorLastName THEN


UPDATE PROFESSOR SET LastName = :NEW.ProfessorLastName
WHERE ProfessorID = :OLD.ProfessorID;
END IF;

END;
/

SET PAGESIZE 30;


G) Go to the SQL CLI, clear your screen, and then copy/paste to run your script. If
you get errors, use the SQL GUI to trouble shoot.

H) Add multiple screenshots if needed of your script’s SQL and its compile results on
the next page

35
Submit Updatable View Script

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

36
37
13
Updatable View Test

You will conduct a test confirming your view instead of trigger is working.

A) Go to the SQL GUI tool

B) Set up your test


SET LINESIZE 200
COLUMN StudentID FORMAT 99
COLUMN SectionID FORMAT 99
COLUMN CourseID FORMAT 99
COLUMN ProfessorID FORMAT 99
COLUMN FirstName FORMAT A10
COLUMN LastName FORMAT A10
COLUMN PercentGrade FORMAT 99
COLUMN SectionName FORMAT A15
COLUMN CourseName FORMAT A7
COLUMN ProfessorFirstName FORMAT A10
COLUMN ProfessorLastName FORMAT A10

C) Clear your screen and add as a comment your name, student number, and
today’s date

D) Run the following…

-- Mishal Aboud - 7/10/2023 - 041048436


SELECT * FROM StudentManagement WHERE ROWNUM <=5;
UPDATE StudentManagement SET CourseName = 'TST001'
WHERE GradeID = 2;
UPDATE StudentManagement SET ProfessorFirstName = 'TST002',
ProfessorLastName = 'TST003'
WHERE GradeID = 2;
SELECT * FROM StudentManagement WHERE ROWNUM <=5;

E) Add a single screenshot of your script’s SQL and its results on the next page

38
Submit Updatable View Test

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

39
14
Unassigning Professors

You will unassign a professor from a section when both their first and last names
are blank.

A) Go to the SQL GUI tool and your modified StudentManagement_UPDATE trigger

B) After all the IF statements add an IF statement with logic where


both :NEW.ProfessorFirstName and :NEW.ProfessorLastName are NULL

C) Add an UPDATE to the IF statement to update SECTION. In the update SET


SectionID to NULL where it has the ProfessorID that matches :NEW.ProfessorID

-- Mishal Aboud - 7/10/2023 - 041048436

CREATE OR REPLACE TRIGGER StudentManagement_UPDATE


INSTEAD OF UPDATE ON StudentManagement FOR EACH ROW
BEGIN
IF :OLD.FirstName <> :NEW.FirstName THEN
UPDATE STUDENT SET FirstName = :NEW.FirstName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.LastName <> :NEW.LastName THEN


UPDATE STUDENT SET LastName = :NEW.LastName
WHERE StudentID = :OLD.StudentID;
END IF;

IF :OLD.ProfessorFirstName <> :NEW.ProfessorFirstName THEN


UPDATE PROFESSOR SET FirstName = :NEW.ProfessorFirstName
WHERE ProfessorID = :OLD.ProfessorID;
END IF;

IF :OLD.ProfessorLastName <> :NEW.ProfessorLastName THEN


UPDATE PROFESSOR SET LastName = :NEW.ProfessorLastName
WHERE ProfessorID = :OLD.ProfessorID;
END IF;

IF :NEW.ProfessorFirstName IS NULL AND :NEW.ProfessorLastName IS NULL THEN


UPDATE SECTION SET SectionID = NULL
WHERE ProfessorID = :NEW.ProfessorID;
END IF;

END;
/

SET PAGESIZE 30;


SET PAGESIZE 30;
D) Go to the SQL CLI, clear your screen, and then copy/paste to run your script

40
E) Scroll your script to the bottom showing the IF statement you created

F) As a comment add your name, student number, and today’s date

G) Add a screenshot of your script’s SQL and its compile results on the next page

41
Submit Unassigning Professors

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your screenshot here>

42
15
Renumbering Sections

You will create a new procedure that renumbers sections

A) Go to the SQL GUI tool and create a new script

B) Walk through the CST2355 - A3 - Create DB script and locate ACADEMIC


PACKAGE/PACKAGE BODY. Copy and paste the code into a new script.

C) Add your name, student number, and today’s date as a comment on the first line
of your script

D) Change the package and package body to SECTION_ENTRY (four changes) and
remove the FUNCTION from the package and package body
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE SetLevel(IN_CoursePrefix IN VARCHAR2, IN_NumberOfZeroes IN
NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE SetLevel(IN_CoursePrefix IN VARCHAR2, IN_NumberOfZeroes IN
NUMBER) AS
CURSOR CourseList IS
SELECT CourseID, CourseName
FROM COURSE
WHERE UPPER(CourseName) LIKE UPPER(IN_CoursePrefix) || '%'
ORDER BY CourseName;

varLevel NUMBER;
varZeroes VARCHAR2(10);
varI NUMBER;
BEGIN
varZeroes := '';

FOR varI IN 1..IN_NumberOfZeroes


LOOP
varZeroes := varZeroes || '0';
END LOOP;

varLevel := 1;

FOR varRecord IN CourseList


LOOP
UPDATE COURSE SET CourseName = UPPER(IN_CoursePrefix) || varLevel ||
varZeroes
WHERE CourseID = varRecord.CourseID;

varLevel := varLevel + 1;
END LOOP;

43
COMMIT;
END SetLevel;
END SECTION_ENTRY;
/
E) Rename the SetLevel procedure in the package and package body to
RenumberSection (three changes). Change the procedure to have only the input
parameter IN_CourseID IN NUMBER in the package and package body.
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR CourseSections IS
SELECT SectionID
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionID;

varLevel NUMBER := 1;
BEGIN
FOR varRecord IN CourseSections
LOOP
UPDATE SECTION SET SectionID = varLevel
WHERE SectionID = varRecord.SectionID;

varLevel := varLevel + 1;
END LOOP;

COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
F) Remove all variables and FOR LOOPs relating to varI and varZeroes
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR CourseSections IS
SELECT SectionID
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionID;

varLevel NUMBER := 1;
BEGIN
FOR varRecord IN CourseSections
LOOP
UPDATE SECTION SET SectionID = varLevel
WHERE SectionID = varRecord.SectionID;

varLevel := varLevel + 1;
END LOOP;

44
COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
G) Change all instances of varLevel to varNewSectionNumber and all instances of
CourseList to SectionList
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR SectionList IS
SELECT SectionID
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionID;

varNewSectionNumber NUMBER := 1;
BEGIN
FOR varRecord IN SectionList
LOOP
UPDATE SECTION SET SectionID = varNewSectionNumber
WHERE SectionID = varRecord.SectionID;

varNewSectionNumber := varNewSectionNumber + 1;
END LOOP;

COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
H) Change varNewSectionNumber to start at 450 and to increase every 10
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR SectionList IS
SELECT SectionID
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionID;

varNewSectionNumber NUMBER := 450;


BEGIN
FOR varRecord IN SectionList
LOOP
UPDATE SECTION SET SectionID = varNewSectionNumber
WHERE SectionID = varRecord.SectionID;

varNewSectionNumber := varNewSectionNumber + 10;


END LOOP;

45
COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
I) Change the SELECT for the cursor so it returns SectionID and SectionName. Return
only sections that have CourseID = IN_CourseID. Order the list by SectionNumber.
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR SectionList IS
SELECT SectionID, SectionName
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionNumber;

varNewSectionNumber NUMBER := 450;


BEGIN
FOR varRecord IN SectionList
LOOP
UPDATE SECTION SET SectionID = varNewSectionNumber
WHERE SectionID = varRecord.SectionID;

varNewSectionNumber := varNewSectionNumber + 10;


END LOOP;

COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
J) Change the UPDATE statement to update the SECTION’s SectionNumber where
the SectionID equals the varRecord.SectionID.
-- Mishal Aboud - 7/010/2023 - 041048436
CREATE OR REPLACE PACKAGE SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER);
END SECTION_ENTRY;
/
CREATE OR REPLACE PACKAGE BODY SECTION_ENTRY AS
PROCEDURE RenumberSection(IN_CourseID IN NUMBER) AS
CURSOR SectionList IS
SELECT SectionID, SectionName
FROM SECTION
WHERE CourseID = IN_CourseID
ORDER BY SectionNumber;

varNewSectionNumber NUMBER := 450;


BEGIN
FOR varRecord IN SectionList
LOOP
UPDATE SECTION SET SectionNumber = varNewSectionNumber
WHERE SectionID = varRecord.SectionID;

varNewSectionNumber := varNewSectionNumber + 10;

46
END LOOP;

COMMIT;
END RenumberSection;
END SECTION_ENTRY;
/
K) Go to the SQL CLI, clear your screen, and then copy/paste to run your script. If
you get errors, use SQL GUI to trouble shoot.

L) Test your procedure by running the following…


SELECT * FROM SECTION WHERE CourseID = 1;
CALL SECTION_ENTRY.RenumberSection(1);
SELECT * FROM SECTION WHERE CourseID = 1;

M) On the next page, add a screenshot of your script’s SQL and its compile results

N) On the next page, and a screen shot of the results of the test

47
Submit RenumberSection Procedure with Test

Confirm your screenshot satisfies the screenshot checklist on page 2

<paste your two screenshots here>

48
49
Submit PDF

A) Save your assignment submission as a PDF


B) Confirm all screenshots are not clipped
C) Submit you PDF in the assignment submission folder

End of Assignment 3

50

You might also like