Professional Documents
Culture Documents
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.
1
2
Instructions
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 --
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.
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
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.
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>
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
6
2
Database Change Request
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
G) Add a single screenshot of your script’s SQL and its results on the next page
7
Submit Database Change Request Script
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
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
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
10
4
Testing Optional-Mandatory Insert
C) Clear the screen and add your name, student number, and today’s date as a
comment
11
Submit Optional-Mandatory Insert Test
12
5
Optional-Mandatory Delete
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
13
-- Mandatory - Mandatory - UPDATE --------------------------------------------
END;
/
14
/
UPDATE PROGRAM
SET ProgramName = :NEW.ProgramName,
Description = :NEW.Description
WHERE ProgramID = :OLD.ProgramID;
END;
/
15
DECLARE
varProgramCount NUMBER;
BEGIN
DELETE FROM PROGRAM WHERE ProgramID = :OLD.ProgramID;
IF varProgramCount = 0 THEN
DELETE FROM DEPARTMENT WHERE DepartmentID = :OLD.DepartmentID;
END IF;
END;
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
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.
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 --
20
Submit Batch Export / Import Screenshot
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.
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.
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
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.
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.
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
25
9
Functions
You will create a new function that determines the letter grade for a student for a
student.
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
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
27
28
10
Functions and Procedures in Triggers
C) Add your name, student number, and today’s date as a comment on the first line
of your script
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.
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
30
11
Views
Views are objects created on base tables. A view can be treated like a table in
SELECT statements.
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
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.
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
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
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.
C) Add your name, student number, and today’s date as a comment on the first line
of your 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.
END;
/
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
END;
/
H) Add multiple screenshots if needed of your script’s SQL and its compile results on
the next page
35
Submit Updatable View Script
36
37
13
Updatable View Test
You will conduct a test confirming your view instead of trigger is working.
C) Clear your screen and add as a comment your name, student number, and
today’s date
E) Add a single screenshot of your script’s SQL and its results on the next page
38
Submit Updatable View Test
39
14
Unassigning Professors
You will unassign a professor from a section when both their first and last names
are blank.
END;
/
40
E) Scroll your script to the bottom showing the IF statement you created
G) Add a screenshot of your script’s SQL and its compile results on the next page
41
Submit Unassigning Professors
42
15
Renumbering Sections
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 := '';
varLevel := 1;
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;
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;
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;
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.
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
48
49
Submit PDF
End of Assignment 3
50