You are on page 1of 60

-1-

-2-

CONTENTS

Problem Domain Analysis ……………………………………………………………………………………… [03]

Entity Relationship Diagram …………………………………………………………………………… [07]

Relational Schemas
Un-Normalized Form ……………………………………………………………………………………… [09]
1st Normalized Form ……………………………………………………………………………………… [10]
2nd Normalized Form ……………………………………………………………………………………… [11]
3rd Normalized Form ……………………………………………………………………………………… [12]

Normalization Process of Relational Databases …………………………… [18]

SQL Codes for the Complete Database ……………………………………………………… [20]

Database Creation and Authentication …………………………………………………… [49]

Database Testing ………………………………………………………………………………………………………… [56]

Conclusion ………………………………………………………………………………………………………………………… [60]


-3-

PROBLEM DOMAIN ANALYSIS

According to the assignment specification given to me at


the end of the module "Data Analysis and Design", we are
expected to come up with a complete database design solution
to a "Rent-a-Car" service called "Car Rental Corporation
(CRC)". As an initiation to this project, these sections of
the assignment documentation will start by doing an
introductory analysis to the problem at hand. Further, under
this section I intend to lay down the assumptions made during
the assignment completion process. It will make it easier to
understand how this assignment was approached and what limits
its performance. The best way to analyze a problem like this
according to my experience is to describe the problem on my
own words and give it the meaning I got from it.

So, during the process of analyzing the problem domain, I


realized that the database should consist of the following
main tables in it.

1. Cars
2. Sub-contractors
3. Customers
4. Revenues
5. Expenditures

So using these tables as a ground, I kept on analyzing


the problem further deep and deep. Then I was able to
identify further sub divisions to these main tables, thus
-4-

creating more entities to be looked in to in the ERD


development phase. This deeper categorization is as follows.

1. Cars
2. Sub-contractors
a. Credit Allowed sub-contractors
b. Regular sub-contractors
3. Customers
a. Casual customers
b. Privileged customers
4. Expenditures
a. Immediate expenditures
b. Credit liability expenditures
5. Revenues

Now that we have a some what abstract but detailed enough


table set, I intend to explain the purpose and constituent of
each table in my own words.

1. Car: The car entity will hold the information that are
relevant to a car or a vehicle owned by this company.
These vehicles are rented to the customers on a payment
basis. Though the entity is taken as car due to the fact
that most of the vehicles rented are cars, it is possible
that there may be other kind of vehicles like vans in the
table. Registration number of the car, fuel type of the
car, the number of passengers that can travel
simultaneously in the car, insurance information, model
of the car, date of purchase, size of the engine, make of
the car, price at which the car was purchased, the year
-5-

the car was produced and price at which the car is rented
must be the content of this entity.

2. Sub-contractors: The sub contractors entity will hold the


information that are relevant to the sub contractors like
out side garages with which the CRC keeps business
relationships. The key thing about sub contractors is
that some of them allow credit based services to our
company while others require immediate payments for their
services. A distinct identification number given to the
contractors, services provided to the company by the sub
contractors, name of the contractor, address of the
contractors and such should be associated with this
table.

3. Customers: The customer’s entity will hold the


information that is relevant to the customers who rent
cars, make deals or reserve cars from CRC. It is one of
the key areas where CRC emphasizes on since for the
success of a business, customers are a key factor. The
company recognizes two distinct types of customers
namely, regular customers who randomly visit CRC and rent
cars on immediate payment basis and privileged customers
who are constantly on business with the CRC. These
privileged customers get special credit facilities and
further they are entitled to reservation of cars before
hand. License number of a customer, name of the
customer, address of a customer and telephone number are
the main data items that must be associated with the
customer’s entity.

4. Expenditures: The expenditures entity will hold the


information that is relevant to the expenditures that are
to be bared by the CRC. All these expenditures are of
-6-

two kinds namely, immediate expenditures where the


payment to the external party must be done immediately
and credit expenditures where the payment can be done on
a credit basis. The arrangements for credits are all
done prior to the transaction. The number of the receipt
made on the payment, the amount paid, the date the
payment is done, a small description of the payment and
the duration of the credits if applicable.

5. Revenues: The revenues entity will be used to hold the


information that are relevant to all the incomes and
revenues of the CRC. All these revenues come in
different form and they are all associated with the cars
the organization has. They can be immediate or credit
based. The method of payment can be credit card or cash.
But since the accounting process focuses on the amount
received, these details are considered insignificant.
The unique number given to the receipts when they are
prepared at the acceptance of the revenues, the amount
received, date the payment was received and the type of
payment are part of this entity.
-7-

ERD: CRC Database System


-8-
-9-

Relational Schemas
-10-
-11-
-12-
-13-

Un-Normalized Form:

• Car [PK:regNo, fuelType, passengerCtr, model,


purchaseDate, make, engineSize, purchasePrice,
producedYear, rentPrise, insuranceDetails]

• SubContractor [PK:conID, conName, conAddress,


serviceInfo, creditLimit]

• Expenditures [PK:receiptID, amount, desc, date]

• CreditLiabilities [PK:receiptID, amount, desc, date,


duration]

• Customers [PK:custID, name, address, TP, status,


licenseNo]

• Revenues [PK:receiptID, amount, date]

• CreditAssets [PK:receiptID, amount, date, duration]

• CarRental [PK:rentID, date, duration]

• CarReservation [PK:reserveID, date, duration]

• CarService [PK:serviceID, date]


-14-

1st Normalized Form:

• Car [PK:regNo, fuelType, passengerCtr, model,


purchaseDate, make, engineSize, purchasePrice,
producedYear, rentPrice, insuranceNo, issueDate,
expireDate, note]

• SubContractor [PK:conID, conName, conAddress,


serviceID, serviceName, serviceCost, creditLimit]

• Expenditures [PK:receiptID, amount, desc, date]

• CreditLiabilities [PK:receiptID, amount, desc, date,


duration]

• Customers [PK:custID, firstName, lastName, address, TP,


status, licenseNo]

• Revenues [PK:receiptID, amount, date]

• CreditAssets [PK:receiptID, amount, date, duration]

• CarRental [PK:rentID, date, duration]

• CarReservation [PK:reserveID, date, duration]

• CarService [PK:serviceID, date]


-15-

2nd Normalized Form:

• Car [PK:regNo, fuelType, passengerCtr, model,


purchaseDate, make, engineSize, purchasePrice,
producedYear, rentPrice, insuranceNo, issueDate,
expireDate, note]

• SubContractor [PK:conID, conName, conAddress,


serviceID, serviceName, serviceCost, creditLimit]

• Expenditures [PK:receiptID, amount, desc, date]

• CreditLiabilities [PK:receiptID, amount, desc, date,


duration]

• Customers [PK:custID, firstName, lastName, address, TP,


status]

• Lisence [PK:licenseNo, custID]

• Revenues [PK:receiptID, amount, date]

• CreditAssets [PK:receiptID, amount, date, duration]

• CarRental [PK:rentID, date, duration]

• CarReservation [PK:reserveID, date, duration]

• CarService [PK:serviceID, date]


-16-

3rd Normalized Form:

• Car [PK:regNo, purchaseDate, purchasePrice,


producedYear, rentPrice, model(FK:CarModel.model),
insuranceNo(FK:InsuranceInfo.insuranceNo)]

• CarModel [PK:model, make, fuelType, passengerCtr,


engineSize]

• InsuranceInfo [PK:insuranceNo, issueDate, expireDate,


note]

• SubContractor [PK:conID, conName, conAddress,


creditLimit]

• Services [PK:serviceID, serviceName, serviceDesc]

• ContractorServices [PK:requestID,
serviceID(FK:Services.serviceID),
conID(FK:SubContractor.conID), serviceCost]

• Expenditures [PK:receiptID, amount, desc, date,


conID(FK:SubContractor.conID)]

• CreditLiabilities [PK:receiptID, amount, desc, date,


duration, conID(FK:SubContractor.conID)]

• Customers [PK:custID, firstName, lastName, address, TP,


status]

• Lisence [PK:licenseNo, custID(FK:Customer.custID)]

• Revenues [PK:receiptID, amount, date,


custID(FK:Customers.custID)]
-17-

• CreditAssets [PK:receiptID, amount, date, duration,


custID(FK:Customers.custID)]

• CarRental [PK:rentID, date, duration,


regNo(FK:Car.regNo), custID(FK:Customers.custID)]

• CarReservation [PK:reserveID, date, duration,


regNo(FK:Car.regNo), custID(FK:Customers.custID)]

• CarService [PK:serviceID, date,


requestID(FK:ContractorServices.requestID),
regNo(FK:Car.regNo)]
-18-

Normalization Process of
Relational Databases

In the field of relational database design and related


concepts, normalization is a systematic way of ensuring that a
database structure is suitable for general-purpose querying
and free of certain undesirable characteristics could lead to
a loss of data integrity, data redundancy or stability. E.F.
Codd, the inventor of the relational database model,
introduced the concept of normalization and what we now know
as the first normal form in 1970. He went on to define
the second and third normal forms in 1971,and Codd and Raymond
F. Boyce defined the Boyce-Codd normal form in 1974. Higher
normal forms were defined by other theorists in subsequent
years, the most recent being the sixth normal form introduced
by Chris Date, Hugh Darwen, and Nikos Lorentzos in 2002.

Informally, a relational database table is often


described as "normalized" if it is in the third normal
form (3NF). Most 3NF tables are free of insertion, update,
and deletion anomalies. A standard piece of database design
guidance is that the designer should begin by fully
normalizing the design, and selectively denormalize only in
places where doing so is absolutely necessary to
address performance issues.

When an attempt is made to modify (update, insert into,


or delete from) a table, undesired side-effects may follow.
-19-

Not all tables can suffer from these side-effects; rather, the
side-effects can only arise in tables that have not been
sufficiently normalized. An insufficiently normalized table
might have one or more of the following characteristics:

• The same information can be expressed on multiple rows;


therefore updates to the table may result in logical
inconsistencies.

• There are circumstances in which certain facts cannot be


recorded at all.

• There are circumstances in which the deletion of data


representing certain facts necessitates the deletion of
data representing completely different facts.

When a fully normalized database structure is extended to


allow it to accommodate new types of data, the pre-existing
aspects of the database structure can remain largely or
entirely unchanged. As a result, applications interacting with
the database are minimally affected. Normalized tables, and
the relationship between one normalized table and another,
mirror real-world concepts and their interrelationships.
Normalized tables are suitable for general-purpose querying.
This means any queries against these tables, including future
queries whose details cannot be anticipated, are supported. In
contrast, tables that are not normalized lend themselves to
some types of queries, but not others.
-20-

Database Implementation in
MySQL using SQL

/*=========================================================*

*SCRIPT NAME : CRCSQL.SQL *

*PURPOSE : This SQL script file contains all the SQL*

* queries required to create the database *

* system that can be used by CRC in their *

* daily operations. *

*AUTHOR : Sandeep Ellawala *

*CONTACT : sandeep.ellawala@gmail.com *

* +94(0)774055644 *

*=========================================================*/

USE hnd32;
-21-

/*---------------------------------------------------------*

* TABLE CREATION QUERIES *

*---------------------------------------------------------*/

CREATE TABLE car_model (

model INT NOT NULL,

make CHAR(20),

fuelType CHAR(7),

passengerCtr TINYINT(2),

engineSize TINYINT(3),

PRIMARY KEY (model),

CONSTRAINT chk_carmodel CHECK (

(model > 0)

);

CREATE TABLE insurance_info (

insuranceNo INT NOT NULL,

issueDate DATE,

expireDate DATE,

note TINYTEXT,

PRIMARY KEY (insuranceNo),

CONSTRAINT chk_insurance CHECK (

(insuranceNo > 0) AND

(DATE_ADD(CURDATE(), INTERVAL 1 YEAR) > expireDate) AND

(CURDATE() >= issueDate)

);
-22-

CREATE TABLE car (

regNo INT NOT NULL,

purchaseDate DATE,

purchasePrice DECIMAL(9, 2),

producedYear SMALLINT(4),

rentPrice DECIMAL(8, 2),

model INT NOT NULL,

insuranceNo INT NOT NULL,

carStat CHAR(9),

PRIMARY KEY (regNo),

FOREIGN KEY (model) REFERENCES car_model(model)

ON DELETE CASCADE

ON UPDATE CASCADE,

FOREIGN KEY (insuranceNo) REFERENCES insurance_info(insuranceNo)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_car CHECK (

(regNo > 0) AND

(purchaseDate <= CURDATE()) AND

(producedYear <= YEAR(CURDATE()))

);
-23-

CREATE TABLE sub_contractor (

conID INT NOT NULL,

conName CHAR(30),

conAddress TINYTEXT,

creditLimit DECIMAL(9, 2),

PRIMARY KEY (conID),

CONSTRAINT chk_subcon CHECK (

(conID > 0)

);

CREATE TABLE services (

serviceID INT NOT NULL,

serviceName CHAR(40),

serviceDesc TINYTEXT,

PRIMARY KEY (serviceID),

CONSTRAINT chk_services CHECK (

(serviceID > 0)

);

CREATE TABLE contractor_services (

requestID INT NOT NULL,

serviceID INT NOT NULL,

conID INT NOT NULL,

serviceCost DECIMAL(9, 2),

PRIMARY KEY (requestID),

FOREIGN KEY (serviceID) REFERENCES services(serviceID)


-24-

ON DELETE CASCADE

ON UPDATE CASCADE,

FOREIGN KEY (conID) REFERENCES sub_contractor(conID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_conser CHECK (

(requestID > 0)

);

CREATE TABLE expenditures (

receiptID INT NOT NULL,

amount DECIMAL(9, 2),

description TINYTEXT,

dateSpent DATE,

conID INT NOT NULL,

PRIMARY KEY (receiptID),

FOREIGN KEY (conID) REFERENCES sub_contractor(conID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_expen CHECK (

(receiptID > 0) AND

(dateSpent = CURDATE())

);

CREATE TABLE credit_liabilities (

receiptID INT NOT NULL,


-25-

amount DECIMAL(9, 2),

description TINYTEXT,

dateSpent DATE,

duration TINYINT(2),

conID INT NOT NULL,

PRIMARY KEY (receiptID),

FOREIGN KEY (conID) REFERENCES sub_contractor(conID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_liabilities CHECK (

(receiptID > 0) AND

(dateSpent = CURDATE()) AND

(duration >= 7)

);

CREATE TABLE customers (

custID INT NOT NULL,

firstName CHAR(20),

lastName CHAR(20),

address TINYTEXT,

TP CHAR(10),

cusStat CHAR(12),

PRIMARY KEY (custID),

CONSTRAINT chk_customers CHECK (

(custID > 0)

);
-26-

CREATE TABLE license (

licenseNo INT NOT NULL,

custID INT NOT NULL,

PRIMARY KEY (licenseNo),

FOREIGN KEY (custID) REFERENCES customers(custID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_license CHECK (

(licenseNo > 0)

);

CREATE TABLE revenues (

receiptID INT NOT NULL,

amount DECIMAL(9, 2),

incomeDate DATE,

custID INT NOT NULL,

PRIMARY KEY (receiptID),

FOREIGN KEY (custID) REFERENCES customers(custID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_revenues CHECK (

(receiptID > 0) AND

(incomeDate = CURDATE())

);
-27-

CREATE TABLE credit_assets (

receiptID INT NOT NULL,

amount DECIMAL(9, 2),

incomeDate DATE,

duration TINYINT(2),

custID INT NOT NULL,

PRIMARY KEY (receiptID),

FOREIGN KEY (custID) REFERENCES customers(custID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_assets CHECK (

(receiptID > 0) AND

(incomeDate = CURDATE()) AND

(duration >= 7)

);

CREATE TABLE car_rental (

rentID INT NOT NULL,

rentDate DATE,

duration TINYINT(2),

regNo INT NOT NULL,

custID INT NOT NULL,

PRIMARY KEY (rentID),

FOREIGN KEY (regNo) REFERENCES car(regNo)

ON DELETE CASCADE

ON UPDATE CASCADE,
-28-

FOREIGN KEY (custID) REFERENCES customers(custID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_rental CHECK (

(rentID > 0) AND

(rentDate = CURDATE()) AND

(duration > 0) AND

(duration <= 30)

);

CREATE TABLE car_reservation (

reserveID INT NOT NULL,

resDate DATE,

duration TINYINT(2),

regNo INT NOT NULL,

custID INT NOT NULL,

PRIMARY KEY (reserveID),

FOREIGN KEy (regNo) REFERENCES car(regNo)

ON DELETE CASCADE

ON UPDATE CASCADE,

FOREIGN KEY (custID) REFERENCES customers(custID)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_reserv CHECK (

(reserveID > 0) AND

(resDate = CURDATE) AND

(duration > 0) AND


-29-

(duration <= 30)

);

CREATE TABLE car_service (

serviceID INT NOT NULL,

serviceDate DATE,

requestID INT NOT NULL,

regNo INT NOT NULL,

PRIMARy KEY (serviceID),

FOREIGN KEY (requestID) REFERENCES contractor_services(requestID)

ON DELETE CASCADE

ON UPDATE CASCADE,

FOREIGN KEY (regNo) REFERENCES car(regNo)

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT chk_service CHECK (

(serviceID > 0) AND

(serviceDate = CURDATE())

);
-30-

/*---------------------------------------------------------*

* TRIGGER CREATION QUERIES *

*---------------------------------------------------------*/

DELIMITER //

CREATE TRIGGER model_bi_trg

BEFORE INSERT ON car_model

FOR EACH ROW

BEGIN

DECLARE tmpval INTEGER;

IF NEW.model <= 0 THEN

SET NEW.model = NULL;

END IF;

END//

CREATE TRIGGER insurance_bi_trg

BEFORE INSERT ON insurance_info

FOR EACH ROW

BEGIN

IF NEW.insuranceNo <= 0 THEN

SET NEW.insuranceNo = NULL;

END IF;

IF DATE_ADD(CURDATE(), INTERVAL 1 YEAR) > NEW.expireDate THEN

SET NEW.insuranceNo = NULL;

END IF;
-31-

IF CURDATE() < NEW.issueDate THEN

SET NEW.insuranceNo = NULL;

END IF;

END//

CREATE TRIGGER car_bi_trg

BEFORE INSERT ON car

FOR EACH ROW

BEGIN

IF NEW.regNo <= 0 THEN

SET NEW.regNo = NULL;

END IF;

IF NEW.purchaseDate > CURDATE() THEN

SET NEW.regNo = NULL;

END IF;

IF NEW.producedYear > YEAR(CURDATE()) THEN

SET NEW.regNo = NULL;

END IF;

END//

CREATE TRIGGER subcon_bi_trg

BEFORE INSERT ON sub_contractor

FOR EACH ROW

BEGIN
-32-

IF NEW.conID <= 0 THEN

SET NEW.conID = NULL;

END IF;

END//

CREATE TRIGGER services_bi_trg

BEFORE INSERT ON services

FOR EACH ROW

BEGIN

IF NEW.serviceID <= 0 THEN

SET NEW.serviceID = NULL;

END IF;

END//

CREATE TRIGGER conser_bi_trg

BEFORE INSERT ON contractor_services

FOR EACH ROW

BEGIN

IF NEW.requestID <= 0 THEN

SET NEW.requestID = NULL;

END IF;

END//

CREATE TRIGGER expen_bi_trg

BEFORE INSERT ON expenditures

FOR EACH ROW

BEGIN

IF NEW.receiptID <= 0 THEN


-33-

SET NEW.receiptID = NULL;

END IF;

IF NEW.dateSpent != CURDATE() THEN

SET NEW.receiptID = NULL;

END IF;

END//

CREATE TRIGGER liabilities_bi_trg

BEFORE INSERT ON credit_liabilities

FOR EACH ROW

BEGIN

DECLARE conlim DECIMAL;

IF NEW.receiptID <= 0 THEN

SET NEW.receiptID = NULL;

END IF;

IF NEW.dateSpent != CURDATE() THEN

SET NEW.receiptID = NULL;

END IF;

IF NEW.duration < 7 THEN

SET NEW.receiptID = NULL;

END IF;

SELECT sub_contractor.creditLimit

INTO conlim
-34-

FROM sub_contractor

WHERE sub_contractor.conID = NEW.conID;

IF conlim < NEW.amount THEN

SET NEW.receiptID = NULL;

END IF;

IF NEW.receiptID != NULL THEN

UPDATE sub_contractor SET

sub_contactor.creditLimit = conlim - NEW.amount

WHERE sub_contractor.conID = NEW.conID;

END IF;

END//

CREATE TRIGGER customers_bi_trg

BEFORE INSERT ON customers

FOR EACH ROW

BEGIN

IF NEW.custID <= 0 THEN

SET NEW.custID = NULL;

END IF;

END//

CREATE TRIGGER license_bi_trg

BEFORE INSERT ON license

FOR EACH ROW

BEGIN

IF NEW.licenseNo <= 0 THEN


-35-

SET NEW.licenseNo = NULL;

END IF;

END//

CREATE TRIGGER revenues_bi_trg

BEFORE INSERT ON revenues

FOR EACH ROW

BEGIN

IF NEW.receiptID <= 0 THEN

SET NEW.receiptID = NULL;

END IF;

IF NEW.incomeDate != CURDATE() THEN

SET NEW.receiptID = NULL;

END IF;

END//

CREATE TRIGGER assets_bi_trg

BEFORE INSERT ON credit_assets

FOR EACH ROW

BEGIN

DECLARE cstat CHAR(12);

IF NEW.receiptID <= 0 THEN

SET NEW.receiptID = NULL;

END IF;

IF NEW.incomeDate != CURDATE() THEN


-36-

SET NEW.receiptID = NULL;

END IF;

IF NEW.duration < 7 THEN

SET NEW.receiptID = NULL;

END IF;

SELECT customers.cusStat

INTO cstat

FROM customers

WHERE customers.custID = NEW.custID;

IF cstat != "privileged" THEN

SET NEW.receiptID = NULL;

END IF;

END//

CREATE TRIGGER rental_bi_trg

BEFORE INSERT ON car_rental

FOR EACH ROW

BEGIN

DECLARE cstat CHAR(12);

IF NEW.rentID <= 0 THEN

SET NEW.rentID = NULL;

END IF;

IF NEW.rentDate != CURDATE() THEN


-37-

SET NEW.rentID = NULL;

END IF;

IF NEW.duration <= 0 THEN

SET NEW.rentID = NULL;

END IF;

IF NEW.duration > 30 THEN

SET NEW.rentID = NULL;

END IF;

SELECT customers.cusStat

INTO cstat

FROM customers

WHERE customers.custID = NEW.custID;

IF cstat != "unprivileged" THEN

SET NEW.rentID = NULL;

END IF;

END//

CREATE TRIGGER reserve_bi_trg

BEFORE INSERT ON car_reservation

FOR EACH ROW

BEGIN

DECLARE cstat CHAR(12);

IF NEW.reserveID <= 0 THEN


-38-

SET NEW.reserveID = NULL;

END IF;

IF NEW.resDate != CURDATE() THEN

SET NEW.reserveID = NULL;

END IF;

IF NEW.duration <= 0 THEN

SET NEW.reserveID = NULL;

END IF;

IF NEW.duration > 30 THEN

SET NEW.reserveID = NULL;

END IF;

SELECT customers.cusStat

INTO cstat

FROM customers

WHERE customers.custID = NEW.custID;

IF cstat != "privileged" THEN

SET NEW.reserveID = NULL;

END IF;

END//

CREATE TRIGGER service_bi_trg

BEFORE INSERT ON car_service

FOR EACH ROW


-39-

BEGIN

IF NEW.serviceID <= 0 THEN

SET NEW.serviceID = NULL;

END IF;

IF NEW.serviceDate != CURDATE() THEN

SET NEW.serviceID = NULL;

END IF;

END//

DELIMITER ;

/*---------------------------------------------------------*

* TABLE DROPPING QUERIES *

*---------------------------------------------------------*/

DROP TABLE car_service;

DROP TABLE car_reservation;

DROP TABLE car_rental;

DROP TABLE credit_assets;

DROP TABLE revenues;

DROP TABLE license;


-40-

DROP TABLE customers;

DROP TABLE credit_liabilities;

DROP TABLE expenditures;

DROP TABLE contractor_services;

DROP TABLE services;

DROP TABLE sub_contractor;

DROP TABLE insurance_info;

DROP TABLE car_model;

DROP TABLE car;


-41-

/*---------------------------------------------------------*

* TRIGGER DROPPING QUERIES *

*---------------------------------------------------------*/

DROP TRIGGER model_bi_trg;

DROP TRIGGER insurance_bi_trg;

DROP TRIGGER car_bi_trg;

DROP TRIGGER subcon_bi_trg;

DROP TRIGGER services_bi_trg;

DROP TRIGGER conser_bi_trg;

DROP TRIGGER expen_bi_trg;

DROP TRIGGER liabilities_bi_trg;

DROP TRIGGER customers_bi_trg;

DROP TRIGGER license_bi_trg;

DROP TRIGGER revenues_bi_trg;

DROP TRIGGER assets_bi_trg;


-42-

DROP TRIGGER rental_bi_trg;

DROP TRIGGER reserv_bi_trg;

DROP TRIGGER service_bi_trg;

/*---------------------------------------------------------*

* DATA INSERT QUERIES *

*---------------------------------------------------------*/

INSERT INTO car_model VALUES (

1,

'nissan',

'diesel',

4,

120

);

INSERT INTO insurance_info VALUES (

1,

"2009.1.10",

"2012.1.9",

''

);

INSERT INTO car VALUES (


-43-

1,

"2009.4.26",

1000000,

"2008",

10000,

1,

1,

''

);

INSERT INTO sub_contractor VALUES (

1,

"DMC",

"666, Maharagama.",

);

INSERT INTO services VALUES (

1,

"paint job",

"Do the exterior painting"

);

INSERT INTO contractor_services VALUES (

1,

1,

1,

30000
-44-

);

INSERT INTO expenditures VALUES (

1,

30000,

"spare service",

"2009.4.27",

);

INSERT INTO credit_liabilities VALUES (

1,

10000,

"",

"2009.4.27",

14,

);

INSERT INTO customers VALUES (

2,

"Rajiv",

"Weerasinghe",

"12, Galle",

"0714054646",

"unprivileged"

);
-45-

INSERT INTO license VALUES (

2,

);

INSERT INTO revenues VALUES (

1,

10000,

"2009.4.27",

);

INSERT INTO credit_assets VALUES (

1,

10000,

"2009.4.27",

10,

);

INSERT INTO car_rental VALUES (

1,

"2009.4.27",

2,

1,

);
-46-

INSERT INTO car_reservation VALUES (

1,

"2009.4.27",

4,

1,

);

INSERT INTO car_service VALUES (

1,

"2009.4.27",

1,

);

/*---------------------------------------------------------*

* DATA RETRIEVAL QUERIES *

*---------------------------------------------------------*/

SELECT car.*

FROM car, car_model

WHERE car.model = car_model.model AND

car_model.fuelType = "diesel";

SELECT car.*

FROM car, car_model

WHERE car.model = car_model.model AND


-47-

car_model.fuelType = "petrol";

SELECT *

FROM car

WHERE car.producedYear < 1998;

SELECT sub_contractor.*

FROM sub_contractor, services, contractor_services

WHERE services.serviceName = "paint job" AND

contractor_services.serviceID = services.serviceID AND

sub_contractor.conID = contractor_services.conID;

SELECT car.*

FROM car, car_model

WHERE car.model = car_model.model AND

car_model.passengerCtr = 4;

SELECT car.*

FROM car, car_model

WHERE car.model = car_model.model AND

car_model.passengerCtr > 4;

SELECT *

FROM car

WHERE car.purchasePrice = (

SELECT MAX(purchasePrice)

FROM car

);
-48-

/*---------------------------------------------------------*

* MISC QUERIES *

*---------------------------------------------------------*/

SHOW TRIGGERS;

SHOW TABLES;
-49-

Proper Database Creation and


User Authentication

Due to a number of technical difficulties faced by me


during the installation of Microsoft SQL Server 2005, I had to
abandon my efforts without successfully completing it.
Therefore I took permission before hand from our lecturer to
use a different free SQL server commonly available in the
Internet to be downloaded freely. This version I used is
called MySQL 5.1 and it is freely available from the website
http://www.mysql.com and this is the same link I used to
download this software.

In order to create a database in MySQL, I had to use a


special GUI tool called “MySQL Administrator”. When I need to
use this application, I will be prompted for an authenticated
user name and a password. This ensures that only an actual
user with required privileges can work with the server. The
following image shows how to locate and run this application
is a standard Desktop PC environment running Microsoft Windows
XP with Service Pack 2.
-50-

• Once we execute the MySQL Administrator application it


opens up a small window where user name and password for
authentication are requested.

• In this prompt the following details must be filled


properly.

o Server Host (The IP address of the server host that


runs the MySQL server. In our case we run the
server in our own PC. Therefore the loop back
address is given as the IP address of the MySQL
server.): 127.0.0.1

o Username (This is where we give a valid user name


that has authorization to access the system. All
the MySQl servers come with a default administrator
user called “root” and it is used for initial
administration and system maintenance.): root

o Password (This is the password you provide in the


install time to the root user. Since it is not a
must, it may be blank too.)
-51-

o Port (This is the TCP port being used by the MySQL


server on the host server machine. This is by
default set to the port 3306. But the server can be
configured to accept connections to the database
server by listening to other custom selected ports
too.): 3306

• Once we provide the proper login information, we are


given the permission to administrate the MySQL server and
related services. MySQL Administrator opens up a window
and gives us access to all the related commands and
administrator interfaces.
-52-

• Once we are inside this Administration panel, it is


easier to continue and create a database and give proper
access privileges to it. In the context of MySQL, a
database is called a schema and therefore the next step
is to create the schema for our solution.
-53-

• Then we will have to provide a valid name for the


database in the prompt that appears. Then a new schema
will be created with the given name.

• Once the database is created, we must create a user


account so that the user can access the database and
perform allowed operations.

• Then valid details for the new user must be provided as


required by the MySQL Administator.
-54-

• Once we create the user, the privileges must be granted


to the user for the given schema. The following window
shows how this is done using the Administrator GUI.
-55-

• In this given scenario, I intend to give privileges of a


root user to this user except for the GRANT access. This
means, it can do any thing the root can do on this
particular, but it can not assign some one else any kind
of such authority over this database.
-56-

Database Testing

Databases in today’s context are sophisticated computer


based system holding valuable corporate data and they have
higher importance in industrial excellence and gaining
competitive advantage. Any tool of such importance must be
reliable, timely, secure, and task suitable. Such qualities
must be established in a database through well organized
processes of testing. The more thorough the tests are the
better the system is going to be. Therefore a significant
amount of time is dedicated by any sound organization in
testing their databases for integrity, reliability, security
and redundancy. These tests are done in many forms and
following are the main categories of tests performed on
validating a database before using it in the industrial
context.

• Data Validity Testing: Data validity is the correctness


and reasonableness of data. Reasonableness of data means
that for example, registration numbers of cars falling
within range, numeric data like prices for rentals being
all digits, dates having valid month, day and year and
customer names having only alphabetical letters. Data
validity errors are the most common, and most difficult
to detect errors in a database. Data validity errors are
usually caused by incorrect data entries, most probably
when a large volume of data being entered in a short
period of time.
-57-

• Data Integrity Testing: When performing data integrity


checks and tests, it is important that we verify if we
can create, modify and delete any data in the tables.
Further, when a particular set of data is saved to the
database, each value gets saved fully, and the truncation
of strings and rounding of numeric values must not occur.
Default values must come in to act when a particular data
is not available in the database records. Integrity must
also be checked up on old hardware and software platforms
used by the organization as well as the new platforms.
All these tests will verify the completeness, soundness,
and wholeness of the stored record data.

• Testing of Triggers, Procedures and Functions: Triggers


hold an important role in the successful implementation
of a database. Triggers are used to put constraints on
data, take actions by monitoring certain event like
insert or update of data in to a table. Error control
becomes very easy when triggers are used. Since
procedures and functions are not that much used in this
assignment and they are out of the scope of this
assignment, I focus tests of this category to triggers
only. Triggers can be validated by fulfilling the
trigger events with various data sets that will produce
random results. By doing so, we can surely identify what
situations the trigger can correctly handle and in what
ways these triggers can be improved.

The following sections contain data sets for SQL queries that
can be used to verify the solidness of triggers used in the
database I created for this assignment.
-58-

Trigger Name Data Sets Used

[1, 'nissan', 'diesel', 4, 120] [1, 'toyota',


model_bi_trg
'petrol', 6, 100] [0, 'toyota', 'petrol', 6, 100]

[1, "2009.1.10", "2012.1.9", ""][0, "2009.1.10",


insurance_bi_trg "2012.1.9", ""][1, "2010.1.10", "2012.1.9", ""][1,
"2009.1.10", "2009.12.9", ""]

[1, "2009.4.26", 1000000, "2008", 10000, 1,


1][0, "2009.4.26", 1000000, "2008", 10000, 1,
car_bi_trg 1][1, "2010.4.26", 1000000, "2008", 10000, 1,
1][1, "2009.4.26", 1000000, "2010", 10000, 1,
1][1, "2009.4.26", 1000000, "2008", 10000, 1, 0]

[1, "DMC", "666, Maharagama", 0][0, "DMC",


subcon_bi_trg
"666, Maharagama", 0]

[1, "paint job", "Do the exterior painting"][0,


services_bi_trg
"paint job", "Do the exterior painting"]

[1, 1, 1, 30000][0, 1, 1, 30000][1, 0, 1, 30000][1,


conser_bi_trg
1, 0, 30000]

[1, 30000, "spare service", "2009.4.30", 1][0,


30000, "spare service", "2009.4.30", 1][1,
expen_bi_trg
30000, "spare service", "2010.4.30", 1][1,
30000, "spare service", "2009.4.30", 0]

[1, 10000, "", "2009.4.27", 14, 2][0, 10000, "",


"2009.4.27", 14, 2][1, 10000, "", "2010.4.27", 14,
liabilities_bi_trg
2][1, 10000, "", "2009.4.27", 14, 1][1, 10000, "",
"2009.4.27", 14, 0]

[1, "Sandeep", "Ellawala", "47, Maharagama",


"0712178369", "privileged"][0, "Sandeep",
customers_bi_trg "Ellawala", "47, Maharagama", "0712178369",
"privileged"][1, "Sandeep", "Ellawala", "47,
Maharagama", "0712178369", "unprivileged"]

license_bi_trg [1, 1][0, 1][1, 0][0, 0][2, 1]

[1, 10000, "2009.4.30", 1][0, 10000,


"2009.4.30", 1][1, 10000, "2010.4.30", 1][1,
revenues_bi_trg
10000, "2009.4.30", 0][1, 10000, "2009.4.30",
NULL]
-59-

[1, 10000, "2009.4.27", 10, 1][0, 10000,


"2009.4.27", 10, 1][1, 10000, "2010.4.27", 10,
assets_bi_trg 1][1, 10000, "2009.4.27", 5, 1][1, 10000,
"2009.4.27", 7, 1][1, 10000, "2009.4.27", 10,
0][NULL, 10000, "2009.4.27", 10, 1]

[1, "2009.4.30", 2, 1, 2][0, "2009.4.30", 2, 1,


rental_bi_trg 2][1, "2010.4.30", 2, 1, 2][1, "2009.4.30", 0, 1,
2][1, "2009.4.30", 2, 0, 2][1, "2009.4.30", 2, 1, 0]

[1, "2009.4.30", 4, 1, 1][0, "2009.4.30", 4, 1,


1][1, "2010.4.30", 4, 1, 1][1, "2009.4.30", 0, 1,
reserv_bi_trg
1][1, "2009.4.30", 40, 1, 1][1, "2009.4.30", 4, 0,
1][1, "2009.4.30", 4, 1, 0]

[1, "2009.4.30", 1, 1][0, "2009.4.30", 1, 1][1,


"2010.4.30", 1, 1][1, "2009.4.30", 0, 1][1,
service_bi_trg
"2009.4.30", 1, 0][1, "2009.4.30", 0, 0][NULL,
"2009.4.30", 1, 1]

All the above test data were selected specifically to


check if the Primary Key constraints are working, if the
foreign key reference constraints are fulfilled, if NULL
values handled well, and if the data limits are properly
checked in the triggers. By using these data sets in a simple
INSERT query, these triggers can be activated and tested.
-60-

Assignment Finalization and


Conclusion
As we started this assignment about one month ago, the
first challenge I faced was to get the MS-SQL server installed
on my PC. Actually this was not a challenge faced just by me.
I came across many students who went through the same issues
as I did. The SQL server CD provided to us by the ICBT campus
did not help us at all since configuring the server on a
desktop was nearly impossible without either previous
experience or hands on guidance. Because of this, we shifted
our selves to another SQL server called MySQL 5.1 which is
freely available in the Internet to be downloaded.

This MySQL was a free SQL server widely used in the real
world applications with a lot of other free software
components like the Apache web server. There are some minor
and major differences between these two software platforms,
when it comes to the SQL syntax being used. Still using the
reference manual provided online with MySQL, I managed to
understand the syntax and create a working solution.

In the reservation table, I use all privileged customer


rentals. This could lead to confusion though it did not
become a problem for me since I had the logic in my head all
the time. Inactiveness of constraints is a major reason why
even slightest conditions had to be moved to a trigger. This
could be eliminated in an environment with active constraints.
All in all, it was an adventurous experience to use MySQL and
do this assignment.

You might also like