You are on page 1of 23

Database System 2002Y(3)

Table of Contents
Table of Contents........................................................................................................................i Description of system and nature of problem ...........................................................................1 OBJECTIVES............................................................................................................................2 LIST OF PROBLEMS...............................................................................................................3 Origin and Forms of Data...........................................................................................................3 Forms of Data........................................................................................................................3 Fig1....................................................................................................................................5 Fig1.........................................................................................................................................5 Origin of Data ........................................................................................................................5 Entity Relationship Diagram......................................................................................................7 Normalisation.............................................................................................................................9 0NF: Unnormalised form ......................................................................................................9 1NF: First Normal Form........................................................................................................9 2NF: Second Normal Form..................................................................................................10 3NF: Third Normal Form ....................................................................................................11 Constraint.................................................................................................................................15 Create Tables............................................................................................................................16 Table Customers...................................................................................................................16 Table Drugs..........................................................................................................................16 Table Suppliers.....................................................................................................................16 Table Sales...........................................................................................................................16 Table Purchases....................................................................................................................16 Triggers....................................................................................................................................17 Indexes.....................................................................................................................................18 Functions..................................................................................................................................19 Function:NewDrug()............................................................................................................19 Function: Del_Drug()...........................................................................................................20 Function: change_d_name().................................................................................................20 Function: Drug_description()...............................................................................................21 Function: get_D_name()......................................................................................................21 Cursors.....................................................................................................................................22

Database System 2002Y(3)

Description of system and nature of problem


GYANEE PHARMACY is a private business. It is located at Royal Road 7th Mile, Triolet. The pharmacy opens at 09:00 and closes at 17:30 and it works five days a week. The owner of the pharmacy, Mr. Umesh Kumar GEEANEE, employs 2 workers; one of them is his brother, Mr. S.Geeanee who is the casjier of the pharmacy, and the other is Mr. Rahul. He helps Mr. U.K. GEEANEE at the desk to search for drugs and create sales orders for customers. GYANEE PHARMACY is aiming at providing the best customer service to his customers. But Mr. Geeanee Umesh Kumar, the owner of the pharmacy, has some problems with the actual systems of his business. Each time a customer asks for a certain product, he has to wait for one of the employees to search for the drug in the unsorted list of drugs which is time consuming. Since the list of drugs is not arranged in any particular order, it is also very difficult for the latter to do any amendments to a specific drug or even to add or to delete any details about a drug. Also, the existence of any drug cannot be determined quickly. Therefore, quite a large number of customers will be queuing up; so on panic the employees have often wrongly calculated the cost of the drug including the vat and the total price of the products for the customer to pay. They have also often misread the price of a drug when transferring the price from the list of drug to the sales orders. Thus, the customers will prefer to go to another pharmacy to buy the drugs they require, in doing so the business of Mr. Geeanee is losing clients and thus he is making loss. Also to produce a sale order, the employees have to recopy the details of the drug from the list of drug, after finding the drug required which is time consuming, in the sales orders. This takes quite a long time and mistakes can be made such as recopying the unit price of a drug. It is also complicated to produce a more updated list of drugs. It is also hard to produce the report of the sales as the copies of receipts have often been lost. So Mr. Geeanee finds it troublesome to organise his business properly.

Database System 2002Y(3) The GYANEE PHARMACY Information System is very slow, therefore Mr. Geeanee has asked us, as analysts, to design and implement a new and more efficient database package to perform data processing easily on drug file, to create sales orders easily, to output receipt easily and to process queries quickly and accurately.

OBJECTIVES
The system should be designed in such a way that: It is less time consuming for Mr. U.K. GEEANEE to search for a Data on all the drugs, the sales of drugs and data on the suppliers and It is easier to search information on drugs, on the sales of drugs and on Facilities to perform data processing in the files such as looking for

specific drug in the drug list and sales transaction. their respective transaction should be stored in a database file. the sales transaction the first record, the next, the last and the previous record are provided along with that to save, add, modify and delete a record from files so that navigation through the files is easy. Details on a drug must be automatically displayed on input of its keyfield so that Mr. U.K. GEEANEE does not waste is time copying the details himself and so errors are reduced. The keyfield should be validated for presence, existence or nonexistence and useful error messages should be provided so that Mr. U.K. GEEANEE, the owner, will know what error has been made and how to correct these errors. Mr. Rahul will be able to print listings and reports, such as the list of drugs, report on the sales, report on the purchase of goods and the sales and purchases orders as receipts easier. To calculate the total price automatically via the unit price and quantity bought or sold so that Mr. U.K. GEEANEE will not have to calculate them as he may make mistake in doing the calculation by himself.

Database System 2002Y(3)

LIST OF PROBLEMS
proned. Manual record of information on drugs, on the sales of drugs, purchase There are no facilities to perform data processing in the files such as It is time consuming for Mr. U.K. GEEANEE to manually search for a Data on all the drugs, the sales of drugs, the purchase of drugs and data specific drug in the drug list and for a sales order number. on the sales and purchase orders are stored on a manual database which is error

looking for the first record, the next, the last and the previous record, making navigation through the files difficult. Data being input can hardly be verified. Mr. U.K. GEEANEE has to waste his time in copying the details of There are no form of key field that could be validated for presence,

drugs during the transactions which increases probability of errors to occur. existence or non-existence and no form of notification is possible in a manual system like the one existing so that Mr. U.K. GEEANEE, the owner, will know what error has been made and how to correct these errors. Mr. Rahul is not able to create automatically listings and reports, such as the list of drugs, report on the sales and purchases and the sales and purchase orders as receipts. Mr. U.K. GEEANEE has to calculate the total price(i.e. unit price*qty bought or supplied) manually, which is as a result proned to errors.

Origin and Forms of Data


Forms of Data
By now, the system holds data in the following files: 3

Database System 2002Y(3) o List of Drugs o Sales Book o Suppliers book

Mr. U.K. GEEANEE is actually using manual files to store all the information about the drugs. The main files are the List of drugs (figure1.1) and the Sale of drugs and the purchase of new stock of drugs. The drug file is divided into the following fields as shown in Table 1.0 below:

Database System 2002Y(3)

Fig1

Fig1

Origin of Data
Here is one type of Document that circulates round the system and that brings data in and out of the system

Database System 2002Y(3)

Figure 1.1 Picture of the GYANEE PHARMACY Paid Invoice

Database System 2002Y(3)

Entity Relationship Diagram

Customer

Drugs

Supplier

Explanation1

Explanation2

Inventory_leve l Normally, customers like to get everything at one place itself; likewise, at the pharmacy Reorder_leve l Country_of_origi also they would prefer to get all the drugs at one place itself. Gyanee pharmacy does provide n Cus_id customers with a variety of drugs they and they can buy all at a time. In fig1 above, we can see that the customer has bought 2 different types of drugs i.e. the Dname customer can buy many drugs at a time. Address Mfg_date This implies that the customer has a one-to-many relationship drugs. D_id Fname Exp_dat Explanation2 e Orde Drugs(EXP5) Customer(EXP1)the customers relationship, suppliers also can provide Gyanee pharmacy with rs(EX Similar to P2) different products at a time. Qty ContactN o sname Balance Unit Price Date Supplie d by(EXP5) Balance Unit price

Explanation 1

Date

Supplier (EXP3) Countr y 7 ContactN o Address

Sup_id Comp_nam e

Database System 2002Y(3)

Explanation1 Entity customer has different attributes such as customerId, surname, and first name, Address and contact number. The customer ID uniquely identifies the customer record. Explanation2 A customer orders drugs, such having more attributes like transaction number, date of transaction, the unit selling price, qty bought and balance. Note: balance is in dotted lines as it depends on the quantity bought and the unit selling price. Explanation 3 The entity supplier with the different attributes like supplier ID which uniquely identifies a supplier, company name, address, contact number Explanation4 Suppliers supply us with different types of drugs and the transaction process involves the transaction number, date of transaction, buying price, qty supplied and the balance. Note: balance is in dotted lines as it depends on the quantity bought and the unit buying price. Explanation5 The entity drugs has attributes such as the drug Id which uniquely identifies a drug in the drug file, drug name, mfg date, exp date, description, country of origin, inventory level, reorder level and unit selling and buying price.

Database System 2002Y(3)

Normalisation
Normalisation is the process for assigning attributes to entities Reduces data redundancies Helps eliminate data anomalies Produces controlled redundancies to link tables Normalization stages 1NF -First normal form 2NF -Second normal form 3NF -Third normal form

0NF: Unnormalised form


The following table shows how data are being duplicated there are even null values in some rows which cannot be stored in a database. Therefore we must proceed by inserting all the values that is the first normal form.

1NF: First Normal Form


In this process we identify the primary keys and the corresponding attributes, i.e. the attributes that depends on the primary key thus intending to reduce redundancy (duplication).

Here we can see that data is being duplicated and is everything fully depends on the primary key.

Database System 2002Y(3)

2NF: Second Normal Form


A relation that is in 1NF and every non-primary-key attribute is fully functionally dependent on the primary key (no partial dependency)
Exp*

Customers= {cus_id, sname, fname, cus_address, cus_contact, qty_bought, date_delivered} Drugs= {d_id, d_name, mfg_date, exp_date,country_of _origin, description, inv_level, reorder_level, unitB_price, units_price} Suppliers= {sup_id, sup_name, sup_contact,sup_address,date_supplied,qty_supplied} Exp* The attributes sname, fname cus_address.in the table customers depend on the primary key Cus_id. Likewise the attribute d_name mfg_dateof table drugs depend on the primary key d_id.

10

Database System 2002Y(3)

3NF: Third Normal Form


A relation that is in 1NF and 2NF and in which no non-primary-key attribute is transitively dependent on the primary key. Customers= {cus_id, sname, fname, cus_address, Sales= {cus_id, d_id, qty_bought, unitS_price, date_delivered Ref from 2NF Customers= {cus_id, sname,fname,cus_address, cus_contact, qty_bought, date_delivered Here we can see that the attributes quantity bought, date delivered and units priced fully depend on the primary key. The attributes sname, fname are being duplicated in the table customers in the 2NF form. So it is better to split the table further in sales and customers where the primary key cus_id identifies the record of customers. Drugs= {d_id, d_name, mfg_date,inv_level, reorder_level, exp_date,country_of _origin, description} Suppliers= {sup_id, sup_name, sup_contact, sup_address} Purchases= {sup_id, d_id, qty_supplied, unitB_price, date_supplied } Sales Table:

Purchases Table:

Customer Table: 11

Database System 2002Y(3)

Drug Table:

Supplier Table:

12

Database System 2002Y(3) Customers table FIELD_NAME CONSTRAINTS CUS_ID PRIMARY KEY SNAME FNAME ADDRESS CONTACT_N O Drugs table FIELD_NAME D_ID NOT NULL NOT NULL

DESCRIPTION Uniquely identifies a record Specifies the surname of the customer Specifies the firstname of the customer Specifies the address of the customer Specifies the contact number of the customer

DATATYPE varchar varchar varchar Text numeric

FIELD_SIZE 5 15 15 -

CONSTRAINTS DESCRIPTION DATATYPE PRIMARY KEY Uniquely varchar identifies a record D_NAME NOT NULL Specifies Drug Varchar name MFG_DATE NOT NULL Date the drug Date was manufactured EXP_DATE NOT NULL Date the drug Date will expire COUNTRY_OF_ORIGIN NOT NULL Country drug is Text imported from DESCRIPTION Type of drug Text ex: laxative or vitamins INV_LEVEL NOT NULL Stock level of Integer drugs REORDER_LEVEL NOT NULL Minimum stock Integer of drug to be reorder Suppliers Table. FIELD_NAME SUP_ID CONSTRAINTS DESCRIPTION DATATYPE PRIMARY KET Uniquely Varchar identifies supplier SUP_NAME NOT NULL Name of Varchar supplier SUP_CONTACT NOT NULL Telephone Numeric number of supplier SUP_ADDRESS NOT NULL Head Office Text address of supplier 13

FIELD_SIZE 5 15 -

FIELD_SIZE 5 15 -

Database System 2002Y(3) Purchases Table. FIELD_NAME CONSTRAINTS DESCRIPTION TRANS_IN_NO PRIMARY KEY Purchases invoice number SUP_ID FOREIGN KEY Helps to identify the suppliers P_QTY NOT NULL Quantity of drugs purchased UNITB_PRICE NOT NULL Price of drug when purchased P_DATE Date purchased

DATATYPE FIELD_SIZE Varchar 5 varchar integer integer date 5 -

Sales Table FIELD_NAME CONSTRAINTS DESCRIPTION TRANS_OUT_NO PRIMARY KEY Sales invoice number CUS_ID FOREIGN KEY Helps to identify the Customers SALES_QTY NOT NULL Quantity of drugs sold UNITB_PRICE NOT NULL Price of drug when sold SALES_Date Date of sales

DATATYPE FIELD_SIZE Varchar 5 varchar integer integer date 5 -

14

Database System 2002Y(3)

Constraint
Customer Table: 1) Primary key on customer id It will uniquely identify a customer record in the table which will allow to avoid duplication of data 2) Customer ID should have no null value because if try to search for a particular customer details we have to run a query by the customer ID. Drug Table 3) Primary key on Drug ID It will uniquely identify a drug record in the table 4) Expiry date should have no null value because it is important for the employees to check for the date of expiry to layout the medicine. Supplier Table 5) primary key on supplier id it will uniquely identify a supplier record 6) supplier contact number should not be null because when the inventory level reaches the reorder level, they have to contact the supplier to reorder the goods. Sales table 7) customer id is a foreign key the system will search for the customer details in the customer file through the customer id 8) drug id as foreign key will be used to search for drug detail in the drug table Purchases Table 9) supplier id as foreign key the system will search for the supplier details in the supplier table 10) drug id as foreign key the system will search for the drug details in the drug table.

15

Database System 2002Y(3)

Create Tables
Table Customers
Create Table customers( cus_id integer Primary key sname varchar(5), fname varchar(15), cus_Address varchar(25), cus_Contact numeric);

Table Drugs
Create Table Drugs( D_ID varchar(5) Primary key NOT NULL, D_name varchar(15), mfg_date date, exp_date date, country_of_origin text, description text, inv_level integer, reorder_level integer );

Table Suppliers
Create Table Suppliers( Sup_ID varchar(5)Primary key NOT NULL, Sup_name varchar(15), Sup-contact numeric, sup_add text );

Table Sales
Create Table sales( trans_out_no varchar(5) Primary key, cust_ID varchar(5) foreign key, sales_qty integer, unitb_price integer, sales_date );

Table Purchases
Create Table purchases( trans_out_no varchar(5) Primary key, sup_id varchar(5) foreign key, p_qty integer, unitb_price integer, p_date date ); 16

Database System 2002Y(3)

Triggers
This trigger is implemented on the table Customer. It will check whether data in the fields SNAME and CUS_CONTACT has a value as it is specified that these fields cannot be empty(NOT NULL). CREATE TABLE CUSTOMER( CUS_ID VARCHAR(5)PRIMARY KEY, SNAME VARCHAR(15) NOT NULL, FNAME VARCHAR(15), CUS_ADDRESS TEXT, CUS_CONTACT NUMERIC NOT NULL ); CREATE FUNCTION CUSTOMER_STAMP()RETURNS TRIGGER AS' BEGIN IF SNAME IS NULL THEN RAISE NOTICE ''SURNAME CANNOT BE NULL''; END IF; IF CUS_CONTACT IS NULL THEN RAISE NOTICE ''CONTACT NUMBER MISSING''; END IF; END; 'LANGUAGE 'PLPGSQL' This trigger ensures that insert, update or delete of a row in the customer table is recorded (i.e audited) in the customer_audit table. The current customer address and customer fname are stamped into the row, together with the type of operation performed on it. Create table customer1( sname varchar NOT NULL, cus_contact numeric ); Create table customer_audit( operation char(1) NOT NULL, sname varchar(15) NOT NULL, fname varchar(15) NOT NULL, cus_Address text NOT NULL, Cus_contact numeric ); create or replace function process_customer_audit() Returns TRIGGER AS' Begin 17

Database System 2002Y(3) IF (TG_OP=''Delete'') Then Insert into customer_audit Select ''D'', now(), user, old.sname, old.cus_contact; Return old; ELSIF (TG_OP=''Update'') Then Insert into customer_audit Select ''U'', now(), user, New.sname, New.cus_contact; Return New; ELSIF (TG_OP=''Insert'') Then Insert into customer_audit Select ''I'', now(), user, New.sname, New.cus_contact; Return New; END IF; Return NULL; END; ' language' plpgsql' Create TRIGGER customer_audit AFTER INSERT OR UPDATE OR DELETE ON customer1 FOR EACH ROW EXECUTE PROCEDURE process_customer_audit();

Indexes
Index allow the databases performance to be better it aid to process query quickly without taking time even if the table is large. It is similar to the index in a book. In our system indexes will aid Mr. UK Gyanee if ever he has to look for a drug in a table with thousands of drug and the drug to be search is at index 500 then he wont have to go though all 499 drug names preceding what he is searching for. CREATE INDEX drug_id_index ON drug(drug_id);

18

Database System 2002Y(3)

Functions
Function:NewDrug()
The market of drug sees the introduction of new medicine quite frequently . so each time a new drug enters the market gyanee has to insert the necessary info about it in the database so this function will do just that. create or replace function NewDrug() Returns text AS' Declare drug_id varchar; dname varchar; mfg_date date; exp_date date; description text; country_of_origin text; inv_level integer; reorder_level integer; Begin Insert into Drugs values(drug_id, dname,mfg_date,exp_date,description, country_of_origin, inv_level,reorder_level); if found Then Return ("New Drug inserted"); END IF; END; 'language'plpgsql';

19

Database System 2002Y(3)

Function: Del_Drug()
If a drug has been remove from the market because of a problem or prohibition due to its composition then the pharmacy will no longer need that drug in the database so it will have to be deleted. This function will be use to delete the drug upon deletion of the drug a msg will be displayed for confirmation. create or replace function Del_Drug(varchar) Returns text AS' Declare Begin Delete from drug where D_ID=$1; if found Then Return ("Drug has been deleted from list"); END IF; END; 'language 'plpgsql';

Function: change_d_name()
This function will be used if the name of a drug changes. create or replace function change_d_name(varchar)Returns text AS' Declare Begin update D_name set Dname=Panadoloo where D_ID=$1; if found Then Return("Drug name changed successfully"); END if; END; 'language 'plpgsql';

20

Database System 2002Y(3)

Function: Drug_description()
If a customer enquires on any drug he will only have the name of the drug so it is preferable take drug name as input for this function The function will take drug name for input and will check it with the database for similar names if there is any similarity then it will display the description of the drug. CREATE or REPLACE function Drug_description(text) Returns text AS $$ Declare D_name varchar; Begin Select description From Drug where D_name=D_name; Return ("Drug description :"),description; END; $$ language 'plpgsql'; Select Drug_description(Panadol); The result will be a table with only panadol and its description.

Function: get_D_name()
Receiving a purchase invoice with only drug id in it Gyanee has to know the name of the Drug thus this function come in handy where he will only insert the D_ID and the name of the Drug will appear if it still exist in the Drug table. CREATE or REPLACE function get_D_name(varchar) Returns text As $$ Declare D_id varchar; Begin Select D_name From Drug where D_id=d_id; Return D_name; END; $$ language 'plpgsql';

21

Database System 2002Y(3)

Cursors
-- Function: query_customer_order() -- DROP FUNCTION query_customer_order(); CREATE OR REPLACE FUNCTION query_customer_order() RETURNS text AS $$ DECLARE cus_id tbl_cus_order.customer_id%TYPE; del_date tbl_cus_order.delivery_date%TYPE; date_cursor CURSOR FOR SELECT customer_id, delivery_date FROM tbl_cust_order ORDER BY delivery_date ASC; ret text:=''; BEGIN OPEN date_cursor; LOOP FETCH date_cursor INTO id, del_date; EXIT WHEN NOT FOUND; ret:=ret||'Customer ID: '||id||' Delivery date: '||del_date ||'\n'; END LOOP; CLOSE date_cursor; RETURN ret; END; $$ LANGUAGE 'plpgsql';

22

You might also like