Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

CP2503 Enterprise Database Systems - Oracle

Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Assignment Objectives & Structures:

This assignment provides experience with creating database tables, stored procedures and
triggers in Oracle database server. It also provides experience with creating simple web-based
database application using Oracle application express.

There are three parts in this assignment, and the marks distributions are

Part 1: Creating DB Tables using JDeveloper (35pts)


Part 2: Programming Stored Procedures and Triggers using PL/SQL (70pts)
Part 3: Creating Web-based DB application using Application Express (35pts)

Total : 140 pts

Assignment Submission:

 You are going to submit a single ZIP file containing your answers of this
assignment via LearnJCU.

 The ZIP file should contain the following 10 scripts


 You need to indicate your name and student number as comment at the
beginning of each script. For example,
-- Author: Carrie Lui 011111111
-- Filename : Part1a.sql
-- Description: This script create all the tables
-- Including Users, ExpenseReport, ExpenseItem and Asset
-- Last modified: 18/8/07

 You need to name each script following the guidelines in each task
 You need to name the ZIP file with your student number.
 You need to make sure that each script can run successfully without syntax
error
 Scripts with any syntax error will NOT be marked
 Submissions deviate from the naming and submission guidelines
mentioned above will NOT be marked

 Submission deadline extension may NOT be granted unless for special


circumstances; however, you must discuss this with your lecturer in advance.

1 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Part 1: Creating DB Tables using JDeveloper (total : 35pts)

You are going to create the database tables for the Expense Reporting database at ABCorp,
an IT consulting company. The Expense Reporting database contains tables to track users and
expenses reports, along with tables to track expense categories. The managers of the
company can use the database to understand the financial conditions of the company.

Task 1A (5pts)

You will need to create a database schema called ERDB to store all the database objects for
this assignment. You also need to grant the appropriate privileges to the ERDB users for
creating the database objects. You need to follow the principle of least privilege when
granting privileges.

Hint:
You may want to grant additional privileges to the user when you are working on PART2 of
this assignment. So, you may come back to this part again and include additional GRANT
statements. Make sure that the submitted script includes all the required GRANT statements.

SUBMISSION:
 A script called PART1a.sql which contains SQL statement(s) for
 creating the ERDB user
 granting appropriate privileges to the ERDB user

Task 1B (20pts)

You need to use JDeveloper to create the entity relationship diagram (ERD) and to generate
the Data Definition Language (DDL) for creating the tables and constraints specified below.

Table 1 briefly summarized the meaning of each database table.

Tables 2 to 9 described the columns in each table.

You should use the table and column names as shown in Table 1 to 9.

Choose the appropriate Oracle data type for each column.

2 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Task 1B Table Descriptions


Table 1: Tables in the Expense Reports Database
Table Name Description
Users Contains data about users who can submit expense reports
ExpenseReport Contains data on the headings of expense reports
ExpCat Contains data about expense categories in which expense items are associated
ExpenseItem Contains data on the detail lines of expense reports
Asset Contains data about company assets that might be associated with expense items
OrgUnit Contains data about the organizational units in which users are members
BudgetItem Contains data about expense budgets by organizational unit and expense
category for each financial year
Log_table Contains data about exceptional data insertion and update

Table 2: Columns in the Users Table


Column Name Data Type Comments
UserNo Positive whole number Primary key
UserFirstName Variable length string Max of 50 characters; nulls not allowed
UserLastName Variable length string Max of 50 characters; nulls not allowed
UserPhone Variable length string Max of 20 characters; nulls allowed
UserEmail Variable length character Max of 50 characters; unique; nulls not allowed
string
UserOrgNo Positive whole number Foreign Key to the OrgUnit table; nulls not allowed

Table 3: Columns in the ExpenseReport Table


Column Data Type Comments
ERNo Positive whole Primary key
number
ERDesc Variable length Max of 255 characters; nulls not allowed
string
ERSubmitDate Date/Time Date and time submitted; default should be the current date
and time (use a function to generate the default value); nulls
not allowed
ERStatusDate Date/Time Date and time when the status changed; defaults to the current
date and time when a record is inserted; changes when the
expense report is approved or denied; use a function to
generate the default value; nulls not allowed
ERStatus Variable length Default value is “PENDING” meaning that the expense report
string is waiting for approval; this field should be limited to one of the
following values “PENDING”, “APPROVED”, or “DENIED”;
nulls not allowed
SubmitUserNo Positive whole Foreign key to the Users table; identifies the user who
number submitted the expense report; nulls not allowed
ApprUserNo Positive whole Foreign key to the Users table; identifies the user who
number approves the expense report; nulls allowed for the case of
“PENDING” reports only

Table 4: Columns in the ExpCat Table


Column Data Type Comments
ECNo Positive whole number Primary key
ECName Variable length string Max of 255 characters; nulls not allowed
ECLimit Dollar Amount Amount allowed per expense category expanse item;
default is 0 (zero); nulls not allowed

3 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Table 5: Columns in the ExpenseItem Table


Column Data Type Comments
EINo Positive whole Primary key
number
ExpDesc Variable length Max of 255 characters; nulls not allowed
string
ExpenseDate Date/Time Date and time that the expense was incurred; default should be the
current date and time (use a function to generate the default value)
ExpAmt Dollar amount Amount of the expense; nulls not allowed; default is 0 (zero)
ExpApprAmt Dollar amount Approved amount of the expense; default is 0 (zero)
ERNo Positive whole Foreign key to the ExpenseReport table; identifies the expense
number report that contains the item; nulls not allowed
ECNo Positive whole Foreign key to the ExpCat table; nulls not allowed
number
AssetNo Positive whole Foreign key to the Asset table; Nulls allowed
number
ExpRecID Positive whole Length of 38 digits.
number Use to store the file id of the scanned receipt image related to the
expense item. Nulls allowed
ExpRecImg Image File Use to store the scanned image of the receipt related to the
expense item. Nulls allowed

Table 6: Columns in the Asset Table


Column Data Type Comments
AssetNo Positive whole number Primary key
AssetDesc Variable length string Max of 255 characters; nulls not allowed

Table 7: Columns in the OrgUnit Table


Column Data Type Comments
OrgNo Positive whole number Primary key
OrgName Variable length string Maximum length of 50 characters; nulls not allowed
OrgParentNo Positive whole number Refers to OrgNo; nulls allowed for the case of a parent
organizational unit

Table 8: Columns in the BudgetItem Table


Column Data Type Comments
BINo Positive whole number Primary key
BIYear Number Must be greater than or equal to 1900; default should be
2005; nulls not allowed
BIAmt Dollar Amount The budgeted amount for the year; nulls not allowed;
default is 0 (zero)
BIActual Dollar Amount An ongoing sum of the actual expenses approved for the
category/org unit combination in the year; default is 0 (zero)
OrgNo Positive Whole Number Foreign Key to the OrgUnit table; nulls not allowed
ECNo Positive whole number Foreign Key to the ExpCat table; nulls not allowed

Table 9: Columns in the Log_Table Table


Column Data Type Comments
ExcNo Positive whole number Primary key
ExcTrigger Variable length string Trigger name in which the exception occurred
Max of 30 characters; null not allowed
ExcTable Variable length string Name of the table containing the exception
Max of 30 characters; null not allowed
ExcKeyValue Positive whole number Primary key value of the row with the exception
ExcDate Timestamp Timestamp when the exception occurred
ExcText Variable length string Message explaining the error

4 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Task 1B Referential Integrity Constraints

Define referential integrity constraints as defined in Tables 2 through 8. You should


enforce each referential integrity constraint.

You should choose appropriate actions when referenced rows are deleted. When an
expense report is deleted, both the heading row in the ExpenseReport table and the associated
detail rows in the ExpenseItem table should be deleted. For other referential integrity
constraints involving required foreign keys (nulls not allowed), deleting a referenced row in a
parent table should not be permitted if there are associated referencing rows in a child table.
For referential integrity constraints involving optional foreign keys (nulls allowed) except
ExpenseReport.ApprUserNo, deleting a referenced row should make the foreign key of the
referencing row null. For ExpenseReport.ApprUserNo, you should restrict deletion of related
User rows.

Task 1B Check Constraints and Unique Constraints

Define the following check constraints or unique constraind in the related tables.

• Email contains an @
• ERStatusDate >= ERSubmitDate
• BIYear>=1900
• ERStatus is ‘PENDING’ ‘APPROVED’, or ‘DENIED’
• ExpApprAmt <= ExpAmt
• The combination of BIYear, OrgNo, and ECNo is unique in the BudgetItem
table.
• Nulls are allowed for ExpenseReport.ApprUserNo only if ERStatus is equal to
PENDING

SUBMISSION:
 A script called PART1b.sql generated by JDeveloper which contains SQL
statement(s) for
 creating the required database tables and constraints

Task 1C Loading Data, Creating Sequences and Users (10pts)

You should use the files (in data.zip) provided to construct INSERT statements to load the
data into the tables. You need to create a sequence generator for table to facilitate the auto
generation of primary key values. The INSERT statements should make use of the sequence
generators. The script InsertBlob.sql provided in LearnJCU shows an example of inserting
Blob data into the table. You also need to create a database user account for each user found
in the USER table. These users can submit expense reports; however, they cannot delete any
data.

SUBMISSION:
 A script called PART1c.sql which contains SQL statement(s) for
 creating the required sequence generators
 inserting data into the tables
 creating the database users accounts and granting appropriate privileges

5 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Part 2: Creating Stored Procedures and Triggers using PL/SQL (total : 70pts)

You should write SQL and PL/SQL to define the triggers and stored procedure as specified in
the following subsections. You should also include SQL statements to test the triggers. Make
sure that you test single row as well as multiple row actions. Include comments for each
testing statement to indicate the trigger in which it applies and the expected results of the
trigger firing.

Task 2A Creating the Expense Amount Exceeding Limit Trigger (10pts)

You should create a trigger called TR_ExceedingLimitNotice. This trigger fires after
inserting a row in the ExpenseItem table or updating the expense amount (ExpAmt). The
trigger body compares the expense amount to the category limit (ECLimit) in the related row
of the ExpCat table. If the expense amount is greater than the category limit, insert a row into
the exception log table (Log_Table).

SUBMISSION:
 A script called PART2a.sql which contains SQL or PL/SQL for
 creating the required trigger
 inserting data into the table for testing trigger
 showing the testing results, e.g. SELECT statement against the Log_Table

Task 2B Creating the Check Approving User Trigger (10pts)

You should create a trigger called TR_CheckApprovingUser.This trigger fires before


updating the approving user number ApprUserNo in the ExpenseReport table. The trigger
body compares the organization number of the approving user to the organization number of
the submitted user. The organization number of the approving user must be the same as the
organization number of the submitting user or the organization number of the parent
organization of the submitting user. If the organization number of the approving user does not
match either, raise an application error.

SUBMISSION:
 A script called PART2b.sql which contains SQL or PL/SQL for
 creating the required trigger
 update data of the table for testing trigger

Task 2C Change Case Trigger (10pts)

You should create a trigger called TR_ChangeStatusCase. This trigger fires before
inserting an ExpenseReport row or updating the expense report status (ERStatus). The body
of the trigger should change the case of the ERStatus column to upper case. You can use the
Oracle UPPER function in the trigger body.

SUBMISSION:
 A script called PART2c.sql which contains SQL or PL/SQL for
 creating the required trigger
 inserting data of the table for testing trigger
 showing the testing results

6 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Task 2D Update Items Expense Date Trigger (10pts)

You should create a trigger called TR_ExpenseDate.When the ExpenseDate column


of the ExpenseItems table is updated, ensure that the value is less than or equal to the
ERSubmitDate column in the related row of the ExpenseReports table. If this constraint is
violated, raise an application error. This trigger needs to fire on INSERT and UPDATE
statements that change the ExpenseDate column in the ExpenseItem table.

SUBMISSION:
 A script called PART2d.sql which contains SQL or PL/SQL for
 creating the required trigger
 inserting and updating data of the table for testing trigger

Task 2E Rollup Expense Item Procedure (10pts)

You should create a stored procedure called spRollupExpenseItem. This procedure updates
the related row in the BudgetItem table given an expense report number and expense category
number. The procedure contains three input parameters, the ERNo value, the ECNo value,
and the rollup amount, along with one Boolean output parameter.

Here is the logic of the procedure.

• If the status (ERStatus) of the related ExpenseReport row is equal to DENIED or


PENDING, an application error is raised. The output parameter is set to False.

• Else, the related BudgetItem row is updated. The actual amount (BIActual) in the
related BudgetItem row are incremented by the rollup amount input parameter.

o The output parameter is set to True after performing the update


operation.

SUBMISSION:
 A script called PART2e.sql which contains SQL or PL/SQL for
 creating the required stored procedure

7 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Task 2F Create Rollup Expense Item Trigger (20pts)

You should create a trigger called TR_RollupExpAmt. This trigger fires after updating the
expense approved amount (ExpApprAmt) or deleting a row of the ExpenseItem table. The
following points explain the logic of this trigger:

• If updating the ExpApprAmt column, the rollup amount should be the difference
of the New.ExpApprAmt minus the Old.ExpApprAmt.

• If deleting a row of the ExpenseItem table, the rollup amount should be the
negative of the Old.ExpApprAmt.

• To perform the rollup, you should call the spRollupExpenseItem procedure


described in Task 2E.

• If the output parameter of the spRollupExpenseItem procedure is true, insert a row


into the exception log table (Log_Table).

o The ExcText (message explaining the error) value should indicate the
operation (update or delete) and the rollup amount.

o If the output parameter of the spRollupExpenseItem procedure is false,


do nothing.

SUBMISSION:
 A script called PART2f.sql which contains SQL or PL/SQL for
 creating the required trigger
 updating data in the table for testing the trigger
 deleting data in the table for testing the trigger
 showing the testing results, e.g. SELECT statements against the
Log_Table and the BudgetItem Table

8 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Part 3: Creating Web-based DB application using Application Express (total: 35pts)


You need to create a simple web-based database application in Oracle application express.

Task 3a Create a SQL Calendar (5pts)


You need to create a SQL calendar. The calendar is a monthly calendar. In each cell (each
day) of the monthly calendar, it will show the sum of amount of expense and the sum of the
approved amount of the expense.

The resulting page should look like the following,

9 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

Task 3b Create Pages for Add/Delete/Update Expense Report and Expense Item (30pts)

EXPENSE REPORT PAGE


The expense report page list all the records in the ExpenseReport table, it allows user to view
and edit expense report records. It also allows user to create new record when the “Create”
button is clicked.

EDIT EXPENSE REPORT PAGE (Creating new record)

When the user clicks the “Create” button on the Expense Report page, it will display the form
below. This form allows user to enter new record. Notice that you should use include features
such as List of values to facilitate user input. You will also need to modify the label of the
items to show meaning description.

10 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

EDIT EXPENSE REPORT PAGE (Editing existing record)

This is the screenshot for the edit expense report page. Related expense items are shown, and
user is allowed to edit or add expense items.

EDIT EXPENSE ITEM PAGE (Creating new record)


When the user clicks the “Add” button on the Related Expense Items Region, it will display
the form below. This form allows user to enter new record. Notice that the Report# value is
not editable. The value is based on the value from the EDIT EXPENSE REPORT PAGE.

11 / 12
CP2503 Enterprise Database Systems - Oracle
Assignment 1 Due: 9 Oct 2008, 5pm (CP2503: 20%, CP5503:15%)

EDIT EXPENSE ITEM PAGE (Editing existing record)


This is the screenshot for the edit expense item page. User can upload new scanned receipt
image to replace the exiting one via this page.

SUBMISSION:
• A script called PART3.sql which contains script exported from Oracle Application
Express for Task3a and Task3b.

<< This is the End of this Assignment>>

12 / 12

You might also like