Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 91

Database project

Hospital management system


Case Study
Pharmacy is a place where medicine is sold or prepared. This project can store the record

of the medicine by their name, by their ID and by their weightage. This project can access the

record as well as sort the record by their name, by their ID also by their weightage. It can store

new records, edit previous records, delete previous records, add new records at the end of the

existing record. It can tell the location or block where the medicine is kept. It can store

manufacturing and expiry dates of medicines. It can also store which medicine is short in stock and

which one is near to finish soon? It can also store the record of the costumer. It can add new costumer

update the previous record and delete the previous record of the costumer. It can store the number of

medicines bought by the costumer and amount spent by that costumer. It can store the arrival date and

time of the costumer. It can also store the employee record of the medical store. It can store the hours

work rate and salary of the costumers. It can also manage the employee information. This database also

add views for the virtual representation of tables and use different operator to perform different

operation on data. This database also searches the value of the user desired. This pharmacy

management system database is user friendly database.

Features: -
1. CREATE TABLES.
2. STORE DATA IN TABLES.
3. ADD / DELETE DATA.
4. UPDATE DATA.
5. SEARCHING AND SORTING.
6. CREATE BILLS.

Entities and their Attribute


Medicine entity
Attributes of medicine entity
Medicine(Medicine_ID, medicine_name, medicine_discription, wietage, price
,companyID, expiry_Date, Expiry_Time, stock_num).

Costumer Entity
Attributes of Costumer Entity
Costumer(Costumer_ID , costumer_name, Arriving_time, contact,
amount_spent, num_of_bougth, Med_ID).

Employee Entity
Attributes of Employee Entity
Employee(Emplyee_ID, employee_name, employee_contact, User_name,
employee_passward, salary, num_hour_work, date_of_joining).

Entity company
Attributes of company Entity
Company(comp_ID, comp_name, comp_city, comp_state, comp_country,
med_ID)

Order Entity
Attributes of order entity
Order(order_ID, order_Disciption , medicine_ID, medicine_quantity, cost_ID,
order_price)

Relationships between entities: -


1. One Costumer order one or more medicine.
2. One Medicine is ordered by one or more costumer.
3. One Pharmacy has one or more employee.
4. One or more employee work in one and only one Pharmacy.
5. One Company contact with one or more pharmacy.
6. One pharmacy contact with zero or more company.
7. Pharmacy sells one or more medicine.
8. One medicine is sold by one or more pharmacy.

ERD Diagram

Normalizations
Normalization on Medicine Information Table
Medicin Med_na Med_wi Med_Pr Med_co Med_lo Expiry_ Expiry_ Compan
e_ID me etage ice mpany cation Date Time y_Name

1NF of Medicine Table: -


A relation is in first normal form if there are no repeating groups. In this
medicine table the attribute company_ID and Company_Name are repeated more
than one time. So, to take pharmacy in first normal form we ensure that every
intersection of rows and columns must contain atomic value. The attribute
medicine_ID and company_ID are being repeated for a single occurrence of
Medicine_Information table. The repeating groups are removed by entering
proper data in the blank fields.
Medicine_Information(M_ID, M_Name, wietage, price,
company_ID, comp_name, location, exp_date, exp_Time)

2NF of Medicine Table: -


A relation is in second normal form if it is in first normal form and every non-key
attribute is fully functionally dependent on the primary key. All non-key attribute
depend on all part of the primary key. The company_name attribute is not fully
functionally dependent on the primary key medicine_ID.
So, to take the medicine table in second normal form we will divide
this into two tables.

Medicine_Information(medicine_ID, medicine_Name, wietage ,price,


location, expiry_date, expiry_time).

Company_Information(medicine_ID,company_ID,comp_Name,comp_country
, comp_city)
The medicine_ID in the company_Information table work as foreign
key. So, the medicine table is in seond normal form.
3NF of Medicine table: -
A relation is in third normal form (3NF) if it is in 2NF and no transitive dependency
exists. It means that the value of any non-key attribute cannot be find by using
the value of another non-key attribute.
The transitive dependency in the Medicine_Information table is as follow.
Medicine_ID ,wietage --> location ,price,expiry_date, expiry_time.
To remove the transitive dependency, we create another table that only
store the medicne_ID, name and wietage.

Medicine_Information_1(medicine_ID, medicine_Name, medicine_wietage).


Medicine_Information_2(medicine_ID, location, price, expiry_date,
expiry_time)
In the above medicine information tables medicine_ID work as primary
key.

Medicine_Information_1 table

Medicine_Information_2 Table
Normalization on company table
The attributes of the company entity are as follow.

Company_Information(company_ID,company_name,company_city,company_
country,medicine_ID)

1NF of Company Information Table


In the above table one company create one or more medicines so these
attributes are repeating several times due to medicine_ID attribute of the
company_Information entity. So, to take the company_IOnformation table in first
normal form I create another table of the repeated groups. The values of
company table are repeating due to medicine_ID attribute so I create another
table for company information table that store the company_ID and Medicine_ID

Company_Information_1(company_ID,company_name,company_city,compa
ny_country)
Company_ID is a primary key in Company_Information_1 table.
For repeating groups the another table is

Company_Information_2(company_ID,medicine_ID)
Company_ID attribute in company_Information_2 table is a foreign key from the
company_Information_1 table and medicine_ID in company_Information_2 is a
foreign key from the medicine_Information_1 table.
So, the company_information_1 and company_information_2 table is in 1NF.

2NF of company Information Tables


The table is in 2Nf if it is in first normal form and every non-key attribute is fully
functional dependent on the primary key.
The company tables (Company_Information_1 and Company_Information_2) is
already in the second normal form because all non-key attributes are fully
functional dependent on the primary key.

3NF of company tables.


The relation is in 3NF if it is in 2NF and no transitive dependency exist. It means
that the value of non-key attribute cannot be find by using the value of another
non-key attribute.
The transitive dependency in the company_Information_1 is as follows.
Company_name --> company_country_company_city.
To remove this transitive dependency, I create another table to store
the name of the company.

Company_Information_3(company_ID,company_name).
After third normal form we have three tables to store the company information as
follow.

Company_Information_1(company_ID,company_country_company_city).
Company_Information_2(company_ID,medicine_ID)
Company_Information_3(company_ID,company_name).
Company_Information_3 table

Company_Information_1 Table
Company_Information_2 Table

Normalization on Costumer table


Costumer C_name Contact Arrival_D Amount_ Num_of_ Medicine_ID
_ID ate spent bougth

1NF of Patient table: -


The relation is in first normal form if there is no repeating groups.
Repeating groups can be removed by entering proper data in a blank field or
make a separate table for a repeating group.
In the Patient_Information table, one costumer has been buying
more than one medicine so we will create another table to store the
medicine_IDs bought by that person.

Patient_Information_1(costumer_ID,costumer_name,contact,arrival_date,arri
val_time,amount_spent, num_of_bougth).

Patient_Information_2(costumer_ID, medicine_ID)
In the above table costumer_Information_1 costumer_ID attribute work as
primary key. The costumer_ID is in costumer_Information_2 is a foreign key from
costumer_Information_1 table and medicine_ID is a foreign key from
Medicine_Information_1 table.

2NF of Costumer table: -


The relation is in 2NF if all non-key attribute is fully functionally dependent
on all part of the primary key. The above costumer relations (1 and 2) is already in
2NF beacsue all non-key attribute all fully functionally dependent on all part of
the primary key costumer_ID.

3NF of Patient table: -


The relation is in third normal form if it is in 2NF and no transitive
dependency exists.
The above Patient_Information_1 relation has transitive dependency as follow.
Costumer_name --> contact, arrival_date, arrival_time, amount_spent,
num_of_bougth.
To remove the above transitive dependency, we create another table to
store the costumer name.

Patient_Information_3(costumer_ID,costumer_name).
After 3NF we have three costumer tables to store the costumer Information as
follow.

Patient_Information_1(costumer_ID,arrival_date,arrival_time,amount_spent,
num_of_bought,contact)

Patient_Informaation_2(costumer_ID,medicine_ID).
Patient_Information_3(costumer_ID,costumer_name).
Patient_Informatio_3 table

Patient_Information_1 Table
Patient_information_2 Table

Normalization on Employee_table
Emp Emp_ Emp_ DOJ Num_hour user_ passwor country city contact
_ID name salary _work name d

The attributes of the employe entity are as follows.


Employee_Information(emp_ID,emp_name,contact,DOJ,emp_salary,NUm_Work_
Hour,emp_country,emp_city,emp_user_name,emp_passward).

1NF of Employee table: -


A relation is in first normal form if every intersection of rows and column
contains atomic values only. It means that the relation does not contain any
repeating groups.
The Employee_Information table is already I the first normal form
because does not contain any repeating groups.

2NF of Employee table: -


A relation is in 2NF if it is in first normal form and all non-key attributes are
fully functionally dependent on all part of the primary key.
The partially dependency in the employee information table is as follow.
Emp_ID,Email --> passward
So create another table employee_Information_3 to store the password of the
employee.

Employee_Information_2(emp_ID, password)
The emp_ID attribute in the Employee_Information_2 is a foreign key from
Employee_Information_1 table. After the 2NF we have 2 employee_Information
tables are as follow.
Employee_Information_1(emp_ID, emp_name, contact, DOJ, emp_salary,
NUm_Work_Hour,emp_country,emp_city,emp_user_name).
Employee_Information_2(emp_ID, emp_password).

3NF of Employee table: -


The relation is in third normal form if it is in 2NF and no transitive dependency
exits. It means no non-key attribute can be find from another non-key attribute.
The transitive dependencies in the above table are as folllow.
Emp_name->emp_salary, DOJ, NUM_hour_work,email.
Emp_user_name -->emp_salary, DOJ, NUM_hour_work,name.
Add the attribute emp_user_name to the employee_Informaion_2
table and mke another tble to store the name of the employee.

Emplyee_Information_3(emp_ID,email)
After third normal form we have three tables to store the information of the
employee are as follows.

Employee_Information_1(emp_ID,emp_contact,DOJ,num_hour_work,emp_c
ountry,emp_city,salary).
Employee_Information_2(emp_ID,emp_user_name,emp_password).
Employee_Information_3(emp_ID,emp_name,emp_status).

Employee_Information_1 Table

Employee_Information_2 Table
Employee_Information_3 Table

Normalization on Order Information Table


O_ID O_Disc O_Date Costum M_quant Order_time
er_ID M_I ity Order_total
D
1 Order1 12/21/20 5 1 4 80 6:30:12
19
1 Order1 12/21/20 5 5 2 300 6:30:12
19
1 Order1 12/21/20 5 7 3 90 6:30:12
19
3 Order3 12/20/20 10 2 2 400 7:10:30
20
3 Order3 12/20/20 10 5 5 750 7:10:30
20

The attributes of the entity order_information are as follows


Order_information (order_ID,order_disc, time, date, medicine_ID,
medicine_quantity, costumer_ID,order_total)
Order_ID attribute in Order_Information tabl work as Primary key. Medicine_ID
attribute in an Order_Information table is a foreign key from
Medicine_Information_1 table and costumer_ID attribute is a foreign key from
Patient_Information_1 table.

1NF of Order Table


The table is in first normal form if there are no repeating groups. Repeating
groups can be removed by creating separate table for the repeating groups. In
order_Information table one order contain one or more medicine. Medicine_ID
and medicine_Quantity attribute create repeating groups in the order table so
create another table to store the medicine_ID , medicine_quantity along with the
order_ID.

Order_Information_2(order_ID,medicine_ID,medicine_quantity).
The order_ID attribute in a Order_Information_1 table is a foreign key from the
Order_Information_1 table and medicine_ID is a foreign key from the
Medicine_Information_1 table.
The costumer_ID is a foreign key so create another table to store the value of the
costumer_ID along with the order_ID.

Order_Information_3(order_ID,costumer_ID)
In order_Information_3 table, order_ID attribute is a foreign key from the
order_Information_1 table and costumer_ID is a foreign key from
Patient_Information_1 table.
After first normal form we have two order_Information tables are as follows.

Order_Information_1(order_ID,order_disc,date,time,order_total).
Order_Information_2(order_ID,medicine_ID,medicine_quantity).
Order_Information_3(order_ID, costumer_ID)

2NF of Order Table.


A relation is in 2NF if it is in 1NF and every non-key attribute is fully functionally
dependent on the primary key. All non-key attributes are fully functionally
dependent on all part of the primary key.
The order_Information tables are already in the 2NF because all the non-key
attribute are fully functional dependent on all part of the primary key.

3NF of Order Information Table


A relation is in third normal form if it is in 2NF and no transitive dependency
exists. The transitive dependency is a type of functional dependency between two
or more non-key attribute. Transitive dependency exists if a non-key attribute
depends on any other non-key attribute. It means that a relation is not in 3NF if
the value of a non-key attribute can be obtained by knowing the value of another
non-key attribute.
The transitive dependency is a key factor in normalization.
Normalization removes transitive dependency from a relation in 2NF. A relation is
not in 3NF if the value of a non-key attribute can be obtained by knowing the
value of another non-key attribute.
The transitive dependency in the above table is
Ordr_disc -> date,time,total.
To remove the transitive dependency, we create another table to store the
medicine description.

Order_Information_4(order_ID,order_discription)
After 3NF we have four tables to store the information of orders.

Order_Information_1(oder_ID,order_date,Order_time,order_total).
Order_Information_2(order_ID,medicine_ID,medicine_quantity).
Order_Information_3(order_ID,costumer_ID).
Order_Information_4(Order_ID,order_discripion).
Order_Information_1 Table

Order_Information_2 Table
Order_Information_3 Table

Order_Information_4 Table
Create Table Medicine_Information
create table

Medicine_Information( medicine_ID

int primary key not null,

medicine_name varchar(30),

wietage float

Insert Values into Medicine_Information table

insert into Medicine_Information values(1,'Panadol',20);

insert into Medicine_Information values(3,'BlueDOLL',40);

insert into Medicine_Information values(5,'Disprine',20);

insert into Medicine_Information values(7,'Risik',40);

insert into Medicine_Information values(12,'inhale',40);

insert into Medicine_Information values(4,'Adderall',30);

insert into Medicine_Information values(56,'Elavil',40);

insert into Medicine_Information values(43,'Yervoy',20);

insert into Medicine_Information values(14,'Medicam',50);

insert into Medicine_Information values(9,'insta',20);

delete from Medicine_Information;

select *from Medicine_Information;


Create Table Medicine_Information_2
create table Medicine_Information_2(

medicine_ID int foreign key references Medicine_Information_1(medicine_ID),

expire_date date,

expire_time time,

loction varchar(20),

price float,

num_of_medicine float

select *from Medicine_Information_2;

Insert Value Into Medicine_Information_2


insert into Medicine_Information_2 values(1,'2008-11-11','19:30:10','A

BLock',30,50); insert into Medicine_Information_2 values(3,'2022-12-26','8:20:30','z

BLock',200,70); insert into Medicine_Information_2 values(5,'2028-4-8','9:20:40','p

BLock',10,500);
insert into Medicine_Information_2 values(7,'2028-4-8','9:20:40','c BLock',25,700);

insert into Medicine_Information_2 values(12,'2048-12-20','13:20:40','X BLock',330,300);

insert into Medicine_Information_2 values(4,'2038-12-20','12:20:40','Y BLock',430,550);

insert into Medicine_Information_2 values(56,'2023-11-11','12:30:10','B BLock',300,530);

insert into Medicine_Information_2 values(43,'2024-9-11','23:30:10','Z BLock',400,550);

insert into Medicine_Information_2 values(14,'2029-11-21','17:30:10','Y BLock',340,850);

insert into Medicine_Information_2 values(9,'2021-1-12','16:30:10','M BLock',32,200);

Create Table Company_Information_1


create table company_Information_1(

company_ID int primary key not null,

company_name varchar(30) unique

select *from company_Information_1;


Insert Values into Company_Information_1 table
insert into company_Information_1 values(1,'castle');

insert into company_Information_1 values(2,'health care');

insert into company_Information_1 values(3,'witle');

insert into company_Information_1 values(4,'Medtech');

insert into company_Information_1 values(5,'First Chjoice Healh');

insert into company_Information_1 values(6,'Philliphs');

insert into company_Information_1 values(7,'Pharmacutical');

insert into company_Information_1 values(8,'stable medical institute');

insert into company_Information_1 values(9,'express leberty');

insert into company_Information_1 values(10,'green earth medical');

Create Table Company_Information_2


create table company_Information_2(

company_ID int foreign key references company_Information_1(company_ID),

company_country varchar(35),

company_city varchar(30)

select *from company_Information_2;

Insert values into Company_Information_2


insert into company_Information_2 values(1,'Pakistan','Sialkot');

insert into company_Information_2 values(2,'Turkey','Istambul');

insert into company_Information_2 values(3,'America','New York');

insert into company_Information_2 values(4,'Pakistan','Gujrat');

insert into company_Information_2 values(5,'India','Mumbai');

insert into company_Information_2 values(6,'Afghanistan','Kabul');

insert into company_Information_2 values(7,'India','Delhi');

insert into company_Information_2 values(8,'Pakistan','Lahore');

insert into company_Information_2 values(9,'Turkey','Sogut');

insert into company_Information_2 values(10,'America','Washingtone');


Create Table Company_Medicine
create table Company_Medicine(

company_ID int references company_Information_1(company_ID),

medicine_ID int references Medicine_Information(medicine_ID)

select *from Company_Medicine;

Insert Values into Company_Medicine table


insert into Company_Medicine values(1,1);

insert into Company_Medicine values(1,3);

insert into Company_Medicine values(3,5);

insert into Company_Medicine values(4,7);

insert into Company_Medicine values(5,1);

insert into Company_Medicine values(5,12);

insert into Company_Medicine values(7,43);


insert into Company_Medicine values(7,56);

insert into Company_Medicine values(10,9);

insert into Company_Medicine

values(10,14);

Create Table Patient_Information_1


create table

Patient_Information_1( costumer_ID

int primary key not null,

costumer_name varchar(30),

select *from Patient_Information_1;

Insert values into Patient_Information_1 table


insert into Patient_Information_1 values(10,'Munazzam');

insert into Patient_Information_1 values(15,'Syed Faiz');

insert into Patient_Information_1 values(20,'Amaad Ali');


insert into Patient_Information_1 values(25,'Moazzam');

insert into Patient_Information_1 values(30,'AliHamza');

insert into Patient_Information_1 values(35,'Abdullah');

insert into Patient_Information_1 values(40,'Kashif');

insert into Patient_Information_1 values(45,'Aizaz');

insert into Patient_Information_1 values(50,'Danish');

insert into Patient_Information_1 values(55,'Ahmed');

Create Patient_Information_2 table


create table Patient_Information_2(

costumer_ID int foreign key references Patient_Information_1(costumer_ID),

arrival_date date,

arrival_time time,

amount_spent float,
num_of_bought int,

contact varchar(30)

select *from Patient_Information_2;

Insert Values into Patient_Information_2 table


insert into Patient_Information_2 values(10,'2020-12-31','12:10:11',230,2,'03328634712');

insert into Patient_Information_2 values(15,'2021-1-31','12:20:12',700,2,'0322634711');

insert into Patient_Information_2 values(20,'2019-1-11','13:30:13',67,3,'03344636612');

insert into Patient_Information_2 values(25,'2020-3-11','15:50:15',200,1,'03338655713');

insert into Patient_Information_2 values(30,'2020-3-11','15:50:15',440,2,'03668633714');

insert into Patient_Information_2 values(35,'2021-4-21','11:31:16',740,2,'03558443712');

insert into Patient_Information_2 values(40,'2017-5-12','17:32:17',35,2,'03328634702');

insert into Patient_Information_2 values(45,'2019-6-25','18:33:18',42,2,'03328222712');

insert into Patient_Information_2 values(50,'2021-7-25','19:34:19',57,2,'03328111732');

insert into Patient_Information_2 values(55,'2019-8-21','10:35:19',43,2,'03328666762');


Create costumer_medicine table
create table Costumer_Medicine(

costumer_ID int foreign key references Patient_Information_1(costumer_ID),

medicine_ID int foreign key references Medicine_Information(medicine_ID)

select *from Costumer_Medicine;

Insert values in costumer_medicine table.


insert into Costumer_Medicine values(10,1);

insert into Costumer_Medicine values(10,3);

insert into Costumer_Medicine values(15,56);

insert into Costumer_Medicine values(15,3);

insert into Costumer_Medicine values(20,5);

insert into Costumer_Medicine values(20,7);

insert into Costumer_Medicine values(20,9);


insert into Costumer_Medicine values(25,3);

insert into Costumer_Medicine values(30,4);

insert into Costumer_Medicine values(30,5);

insert into Costumer_Medicine values(35,14);

insert into Costumer_Medicine values(35,43);

insert into Costumer_Medicine values(40,5);

insert into Costumer_Medicine values(40,7);

insert into Costumer_Medicine values(45,5);

insert into Costumer_Medicine values(45,9);

insert into Costumer_Medicine values(50,7);

insert into Costumer_Medicine values(50,9);

insert into Costumer_Medicine values(55,5);

insert into Costumer_Medicine values(55,9);


Create employee_information_1 table
create table

Employee_Information_1( emp_ID int

primary key not null, emp_contact

varchar(30),

DOJ date,

num_hour_work time,

emp_country varchar(30),

emp_city varchar(30),

salary float

select *from Employee_Information_1

Insert values in employee_information_1 table


insert into Employee_Information_1 values(1,'03216123692','2019-1-
2','12:34:19','Pakistan','Sialkot',25000);

insert into Employee_Information_1 values(2,'03234523692','2012-2-5','15:34:19','India','Delhi',30000);

insert into Employee_Information_1 values(3,'03216777692','2013-3-


23','12:34:19','Pakistan','Peshawar',35000);

insert into Employee_Information_1 values(4,'03216123888','2014-4-


21','14:34:19','Pakistan','Karachi',20000);

insert into Employee_Information_1 values(5,'03456123692','2011-5-


22','16:34:19','Pakistan','Lahore',15000);

insert into Employee_Information_1 values(6,'03267823692','2015-2-


12','5:34:19','India','Mumbai',22333);

insert into Employee_Information_1 values(7,'03216111192','2016-2-


12','7:34:19','Pakistan','Lahore',20303);

insert into Employee_Information_1 values(8,'03216133392','2018-4-


14','12:34:19','Pakistan','Sialkot',40000);

insert into Employee_Information_1 values(9,'03216111192','2019-9-


15','12:34:19','Pakistan','Dhaska',35000);

insert into Employee_Information_1 values(10,'03416123692','2020-10-


1','12:34:19','Pakistan','Wazirabad',34332);
Create employee_information_2 table
create table Employee_Information_2(

employee_ID int foreign key references Employee_Information_1(emp_ID),

emp_user_name varchar(30) primary key,

passward varchar(30) unique

);

select*from Employee_Information_2;

Insert value in employee_information_2 table.


insert into Employee_Information_2 values(1,'Munazzam786','Munazzam_123');

insert into Employee_Information_2 values(2,'Sheraz123','Sheraz_454');

insert into Employee_Information_2 values(3,'Tayyab321','Tayyab_3434');

insert into Employee_Information_2 values(4,'Tahaa678','tahaaa_123');


insert into Employee_Information_2 values(5,'Moazzam931','Moazzam_345');

insert into Employee_Information_2 values(6,'Mamoor345','mamoor_000');

insert into Employee_Information_2 values(7,'Mooooor589','moooooor786');

insert into Employee_Information_2 values(8,'Sheyaan786','sheyan_1');

insert into Employee_Information_2 values(9,'Sheroz111','sheroz_1212');

insert into Employee_Information_2 values(10,'Majeed222','majeed_www');

Create employee_information_3 table.


create Table Employee_Information_3(

emp_ID int foreign key references Employee_Information_1(emp_ID),

emp_name varchar(30),

emp_status varchar(30)

)
select *from Employee_Information_3;

Insert into employee_information_3 table


insert into Employee_Information_3 values(1,'Munazzam','Manager');

insert into Employee_Information_3 values(2,'Sheraz','Accountant');

insert into Employee_Information_3 values(3,'Tayyab

Sajjad','Assistant'); insert into Employee_Information_3 values(4,'Taha

Sajjad','Owner'); insert into Employee_Information_3

values(5,'Moazzam','Owner'); insert into Employee_Information_3

values(6,'Mamoor','Accountant'); insert into Employee_Information_3

values(7,'Mooooor','Accountant'); insert into Employee_Information_3

values(8,'Sheyaan','Accountant'); insert into Employee_Information_3

values(9,'Sheroz','Accountant'); insert into Employee_Information_3

values(10,'Majeed','SecrityGaurd');
Create order_Information_1 table
create table order_Information_1(

order_ID int primary key not null,

order_date date,

order_time time,

order_total float

select *from Medicine_Information_2;

Insert values in order_information_1 table.


insert into order_Information_1 values(1,'2020-1-1','1:31:1',400);

insert into order_Information_1 values(2,'2020-1-2','2:32:9',75);

insert into order_Information_1 values(3,'2021-1-3','3:33:4',430);

insert into order_Information_1 values(4,'2020-1-4','6:34:7',64);

insert into order_Information_1 values(5,'2020-1-5','9:35:2',660);


Create order_Information_2 table
create table order_Information_2(

order_ID int foreign key references order_Information_1(order_ID),

medicine_ID int foreign key references Medicine_Information(medicine_ID),

medicine_quantity int

Insert values in order_information_2 table


insert into order_Information_2 values(1,3,2);

insert into order_Information_2 values(2,7,3);

insert into order_Information_2 values(3,4,1);

insert into order_Information_2 values(4,9,2);

insert into order_Information_2

values(5,12,2);
select *from order_Information_2;

Create tabele order_information_3 table


create table order_Information_3(

order_ID int foreign key references order_Information_1(order_ID),

costumer_ID int foreign key references Patient_Information_1(costumer_ID)

Insert values in order_information_3 table


insert into order_Information_3 values(1,10);

insert into order_Information_3 values(2,30);

insert into order_Information_3 values(3,40);

insert into order_Information_3 values(4,50);

insert into order_Information_3 values(5,35);

select *from order_Information_3;


Create order_Information_4 table
create table order_Information_4(

order_ID int foreign key references order_information_1(order_ID),

order_discription varchar(30)

Insert values in order_information_4 table


insert into order_Information_4

values(1,'order1'); insert into

order_Information_4 values(2,'order2'); insert

into order_Information_4 values(3,'order3');

insert into order_Information_4

values(4,'order4'); insert into

order_Information_4 values(5,'order5'); select

*from order_Information_4;
Views
Medicine View on Two Tables
create view Medicine_View
as select
m.medicine_ID,m.medicine_name,m.wietage,m1.expire_date,m1.expire_time,m1.loction,m1.n
um_of_medicine,m1.price
from Medicine_Information m,Medicine_Information_2 m1 where
m1.medicine_ID=m.medicine_ID;
drop view Medicine_View;
select *from Medicine_View;

Company Medicine View on 5 tables


create view Medicine_Company
as select
m.medicine_ID,m.medicine_name,m.wietage,m1.expire_date,m1.expire_time,m1.loction,m1.n
um_of_medicine,m1.price,
c1.company_ID,c2.company_name,c3.company_city,c3.company_country
from Medicine_Information m,Medicine_Information_2 m1,Company_Medicine
c1,company_Information_1 c2,company_Information_2 c3
where m.medicine_ID=m1.medicine_ID and m.medicine_ID=c1.medicine_ID and
c1.company_ID=c2.company_ID and c1.company_ID=c3.company_ID;
select *from Medicine_Company;
Patient View on three tables
create view Patient_View as
select
p.costumer_ID,p.costumer_name,p1.arrival_date,p1.arrival_time,p1.amount_spent,p1.num_of
_bought,p1.contact
,p2.medicine_ID from Patient_Information_1 p,Patient_Information_2 p1,Costumer_Medicine
p2
where p.costumer_ID=p1.costumer_ID and p1.costumer_ID=p2.costumer_ID;
select *from Patient_View;
Patient Medicine View on 5 tables.
create view Patient_Medicine_View as
select
p.costumer_ID,p.costumer_name,p2.arrival_date,p2.contact,p2.num_of_bought,m.medicine_I
D,
m.medicine_name,m.wietage,m1.loction,m1.price
from Patient_Information_1 p,Patient_Information_2 p2,Costumer_Medicine
cm,Medicine_Information m,Medicine_Information_2 m1
where cm.medicine_ID=m.medicine_ID and m.medicine_ID=m1.medicine_ID and
cm.costumer_ID=p.costumer_ID and cm.costumer_ID=p2.costumer_ID;
select *from Patient_Medicine_View;
Employee View on three tables.
create view Employee_View as
select
e.emp_ID,e.emp_contact,e.DOJ,e.num_hour_work,e.emp_city,e.salary,e1.emp_user_name,e1.
passward,e2.emp_name,e2.emp_status
from Employee_Information_1 e,Employee_Information_2 e1,Employee_Information_3 e2
where e.emp_ID=e1.employee_ID and e.emp_ID=e2.emp_ID;
drop view Employee_View;
select *from Employee_View;
Order View on four tables
create view Order_View as
select
o1.order_ID,o4.order_discription,o3.costumer_ID,o2.medicine_ID,o2.medicine_quantity,o1.ord
er_date,o1.order_time
,o1.order_total
from Order_Information_1 o1,order_Information_2 o2,order_Information_3
o3,order_Information_4 o4
where o1.order_ID=o2.order_ID and o1.order_ID=o3.order_ID and o1.order_ID=o4.order_ID;
select *from Order_View;
select *from company_Information_1;
select *from company_Information_2;
select *from company_Medicine;

Order_Costumer_Medicine_View on Six
Tables
create view Order_Costumer_Medicine_View as
select
o4.order_ID,o4.order_discription,o1.order_date,o1.order_time,o1.order_total,m1.medicine_ID
, m1.medicine_name,m1.wietage,o2.medicine_quantity,p1.costumer_ID,p1.costumer_name
from order_Information_1 o1,order_Information_2 o2,order_Information_3 o3,
order_Information_4 o4,Patient_Information_1 p1,Medicine_Information m1
where o1.order_ID=o2.order_ID and o1.order_ID=o3.order_ID and o1.order_ID=o4.order_ID
and o2.medicine_ID=m1.medicine_ID and o3.costumer_ID=p1.costumer_ID
select *from Order_Costumer_Medicine_View;

Company View on three tables


create view Company_View as
select
c1.company_ID,c1.company_name,cm.medicine_ID,c2.company_country,c2.company_city
from company_Information_1 c1,company_Information_2 c2,company_Medicine cm
where c1.company_ID=c2.company_ID and c1.company_ID=cm.company_ID;
select *from Company_View;

Company Medicine View on four tables


create view Company_Medicine_View as
select
c1.company_ID,c1.company_name,m.medicine_ID,m.medicine_name,m.wietage,c2.company_
city,c2.company_country
from company_Information_1 c1,company_Information_2 c2,company_Medicine
cm,Medicine_Information m
where c1.company_ID=c2.company_ID and c2.company_ID=cm.company_ID and
cm.medicine_ID=m.medicine_ID;
select *from Company_Medicine_View;
Joins
DISPLAY THE ALL RECORD OF THE MEDICINE USING
INNER JOIN
select
m1.medicine_ID,m1.medicine_name,m1.wietage,m2.expire_date,m2.expire_time
,m2.loction,m2.price,m2.num_of_medicine from
Medicine_Information_1 m1 inner join Medicine_Information_2 m2 on
m1.medicine_ID=m2.medicine_ID order by medicine_ID asc;
select *from Patient_Information_2;
Inner join Apply On four tables medicine_Information
,company information 1 and 2 and company medicine
table
select
m.medicine_ID,m.medicine_name,m.wietage,cm.company_ID,c1.company_name
,c.company_country,c.company_city from
(((Medicine_Information m inner join Company_Medicine cm on
cm.medicine_ID=m.medicine_ID) inner join company_Information_2 c
on c.company_ID=cm.company_ID) inner join company_Information_1 c1 on
c1.company_ID=cm.company_ID ) order by cm.company_ID asc;
Display All the Record of the Medicine Along with
Company_ID
select
cm.medicine_ID,m.medicine_name,m.wietage,c.company_ID,c.company_name
from
((company_Information_1 c inner join Company_Medicine cm on
cm.company_ID=c.company_ID)
inner join Medicine_Information m on m.medicine_ID=cm.medicine_ID)
Display the all record of medicine long with company
select
cm.medicine_ID,m.medicine_name,m.wietage,c.company_ID,c.company_name,
m2.expire_date,m2.expire_time,m2.loction,m2.price,m2.num_of_medicine
from ((( company_Information_1 c inner join Company_Medicine cm on
cm.company_ID=c.company_ID)
inner join Medicine_Information m on cm.medicine_ID=m.medicine_ID)
inner join Medicine_Information_2 m2 on m2.medicine_ID=m.medicine_ID)
order by m2.loction asc;

Display the record of the costumer


select
c.costumer_ID,c.costumer_name,c1.arrival_date,c1.arrival_time,c1.contact,c1.am
ount_spent,c1.num_of_bought from Patient_Information_1 c
inner join Patient_Information_2 c1 on c.costumer_ID=c1.costumer_ID;

Display the Patient record along with medicine_IDs


select c.costumer_ID,c.costumer_name,c1.medicine_ID from
Patient_Information_1 c
inner join Costumer_Medicine c1 on c.costumer_ID=c1.costumer_ID;
Display the All costumer record along with the medicine
ID by using three tables.
select
c.costumer_ID,c.costumer_name,c1.arrival_date,c1.arrival_time,c1.arrival_date,c
1.amount_spent,c1.num_of_bought,c1.contact,c2.medicine_ID
from ((Patient_Information_1 c inner join Patient_Information_2 c1 on
c.costumer_ID=c1.costumer_ID)
inner join Costumer_Medicine c2 on c2.costumer_ID=c.costumer_ID);
Left join Costumner_Medicine and Medicine_Information
table according to medicine_ID
select *from Costumer_Medicine c left join Medicine_Information m on c.medicine_ID=m.medicine_ID;
left join medicine_information and costumer_medicine table
according to medicine_ID
select *from Medicine_Information m left join Costumer_Medicine c on m.medicine_ID=c.medicine_ID;
Right join on medicine_information, costumer_information
and costumer_medicine table
select *from ((Medicine_Information m right join Costumer_Medicine c on
m.medicine_ID=c.medicine_ID)

right join Patient_Information_1 p on p.costumer_ID=c.costumer_ID);


Display the Employee record by using two tables.
select
e.emp_ID,e.emp_contact,e.DOJ,e.num_hour_work,e.salary,e.emp_country,e.emp
_city,e1.emp_user_name,e1.passward from Employee_Information_1 e
inner join Employee_Information_2 e1 on e.emp_ID=e1.employee_ID;
Display the employee record using two tables
select e.employee_ID,e.emp_user_name,e.passward,e1.emp_name from
Employee_Information_2 e
inner join Employee_Information_3 e1 on e.employee_ID=e1.emp_ID
Display the All employee record using three tables.
select
e.emp_ID,e1.emp_name,e.emp_contact,e.emp_country,e.salary,e.emp_city,e.DO
J,e.num_hour_work,e2.emp_user_name,e2.passward from
((Employee_Information_1 e inner join Employee_Information_3 e1 on
e.emp_ID=e1.emp_ID) inner join Employee_Information_2 e2
on e2.employee_ID=e1.emp_ID);
Display the Order Record and order medicines record
using three tables
select
o1.order_ID,o3.order_discription,o2.medicine_ID,o2.medicine_quantity,o1.order
_total from ((order_Information_1 o1
inner join order_Information_2 o2 on o1.order_ID=o2.order_ID)inner join
order_Information_4 o3 on o3.order_ID=o2.order_ID);
Display the order Record along with Patient record
using 4 tables.
select
o1.order_ID,o4.order_discription,o2.costumer_ID,o1.order_total,p.costumer_na
me from (((order_Information_1 o1 inner join
order_Information_3 o2 on o1.order_ID=o2.order_ID) inner join
Patient_Information_1 p on o2.costumer_ID=p.costumer_ID)
inner join order_Information_4 o4 on o2.order_ID=o4.order_ID);

Display All the Order Information Using Four tables


select
o1.order_ID,o4.order_discription,o1.order_date,o1.order_time,o2.medicine_ID,o
2.medicine_quantity,o3.costumer_ID,o1.order_total
from (((order_Information_1 o1 inner join order_Information_2 o2 on
o1.order_ID=o2.order_ID)
inner join order_Information_3 o3 on o1.order_ID=o3.order_ID)
inner join order_Information_4 o4 on o3.order_ID=o4.order_ID) ;
Display the order Information along with costumer and
Medicine Information using 6 tables
select
o1.order_ID,o4.order_Discription,o2.medicine_ID,m.medicine_name,o2.medicine
_quantity,o3.costumer_ID,p.costumer_name,o1.order_total
from(((((order_Information_1 o1 inner join order_Information_2 o2 on
o1.order_ID=o2.order_ID)
inner join Medicine_Information m on m.medicine_ID=o2.medicine_ID)
inner join order_Information_3 o3 on o3.order_ID=o1.order_ID)
inner join Patient_Information_1 p on p.costumer_ID=o3.costumer_ID)
inner join order_Information_4 o4 on o4.order_ID=o3.order_ID);
Subquery Nested Queries
subquery to display Order Information
select *from order_Information_1 o where o.order_ID in (select o1.order_ID from
order_Information_4 o1 where o.order_ID=o1.order_ID);

Subquery to display order_ID and order_tital with


respect to order_Information_4 table
select o.order_ID,o.order_total from order_Information_1 o where o.order_ID in
(select o1.order_ID from order_Information_4 o1 where o.order_ID=o1.order_ID
);

Subquery to display the costumer information that have


order
select *from Patient_Information_1 p where p.costumer_ID in(select
o.costumer_ID from order_Information_3 o where
o.costumer_ID=p.costumer_ID);
subquery to display the medicine information
select *from order_Information_2 o where o.medicine_ID
in (select m.medicine_ID from Medicine_Information m where
o.medicine_ID=m.medicine_ID);
select subquery to display the oder information with
match of medicine ID
select *from order_Information_1 o where o.order_ID in (select o1.order_ID from
order_Information_2 o1 where o1.medicine_ID
in( select m.medicine_ID from Medicine_Information m where
m.medicine_ID=o1.medicine_ID));
Select Subquery to Dislay Medicine that have in orders
select *from Medicine_Information m where m.medicine_ID in (select
o1.medicine_ID from order_Information_2 o1 where
m.medicine_ID=o1.medicine_ID);

Subquery to Display Order ID along with Medicine_ID


and medicine quantity
select *from order_Information_2 o where o.medicine_ID in (select
m.medicine_ID from Medicine_Information m where
m.medicine_ID=o.medicine_ID);
Subquery to Display the Company_ID along with
Medicine
select *from Company_Medicine c where c.medicine_ID in (select m.medicine_ID
from Medicine_Information m where m.medicine_ID=c.medicine_ID);
SubQuery to display the Patient ID and medicine_ID
bought by that Patient
select *from Costumer_Medicine c where c.medicine_ID in (select m.medicine_ID
from Medicine_Information m where m.medicine_ID=c.medicine_ID);
Indexes
Indexes are used to speed up the sorting and searching process.
We create Different Indexes in our project these are as follow.

Medicine Index on Medicine_Information_Table


create index Medicine_Index on
Medicine_Information(medicine_ID,medicine_name);

Medicine Index on Medicine_Information_2 Table.


create index Medicine_Index_1 on Medicine_Information_2(medicine_ID);

Company Index on Company_Information_1 Table.


create index company_index_1 on
company_Information_1(company_ID,company_name);

Company Index on Company_Information_2 Table.


create Index company_Index on company_Information_2(company_ID);
Company Index on Company_Medicine Table.
create Index company_medicine_index on
company_Medicine(company_ID,medicine_ID);

company index on Company_Information_2 table.


create index Company_Index on
company_Information_2(company_ID,company_country,company_city);

Patient Index on Patient_Information_1 Table.


create index Patient_Index_1 on
Patient_Information_1(costumer_ID,costumer_name);

Patient Index on Patient_Information_2 table.


create index Patient_Index on Patient_Information_2(costumer_ID);

Patient Index on Costumer_Medicine Table.


create index Patient_Medicine_Index on
costumer_Medicine(costumer_ID,medicine_ID);

Employee Index on Employee_Information_1 Table.


create index Employee_Index_1 on
Employee_Information_1(emp_ID,emp_country,emp_city,salary);

Employee Index on Employee_Information_2 Table.


create index Employee_Index on
Employee_Information_2(employee_ID,emp_user_name);

Employee Index on Employee_Information_3 Table.


create index Employee_Index_2 on
Employee_Information_3(emp_ID,emp_name,emp_status);

Order Index on Order_Information_1 Table


create index order_Index on
order_Information_1(order_ID,order_date,order_time);

Order Index on Order_Information_2 Table


create index order_Index_2 on order_Information_2(order_ID,medicine_ID);

Order Index on Order_Information_3 Table


create index order_Index_3 on order_Information_3(order_ID,costumer_ID);

Order Index on Order_Information_4 Table


create index order_Index_4 on order_Information_4(order_ID,order_discription);

Functions
Sum function to calculate Total Orders Price.
select sum(o.order_total) as total_Order_Price from order_Information_1 o;

Average function to Find the Average order price.


select avg(o.order_total) as Avg_Order_Price from order_Information_1 o;
Like function to display the medicine name start with l.
select *from Medicine_Information m where m.medicine_name like'%l';

Like function to display the medicine name start with M.


select *from Patient_Information_1 c where c.costumer_name like 'M%';
Not Like function to display the medicine name start that are
not start with M.
select distinct *from Patient_Information_1 c where c.costumer_name NOT like
'M%';
Find the Maximum order Price.
select max(o.order_total) as Max_price_order from order_Information_1 o;

Find the Minimum order amount


select min(o.order_total) as Min_price_order from order_Information_1 o;
Count the Number of Medicines.
select count(*) as Total_Num_of_Medicine from Medicine_Information;

Count the Number of Costumers


select count(*) as Total_Num_of_Costumer from Patient_Information_1;

Count the number of Orders.


select count(*) as Total_Num_of_Orders from order_Information_1;
Count the Number of Employees.
select count(*) as Total_Num_of_Employee from Employee_Information_1;

Count the number of Companies.


select count(*) as Total_Num_of_Companies from company_Information_1;
Display The order name With Sterike left.
select order_ID,left(order_discription + '******' ,10) from order_Information_4;
Display the Medicine name with sterike right.
select medicine_ID,right(Replicate('*',5) + medicine_name,10),wietage from
Medicine_Information;

Find the length of the company Variables.


select company_ID,len(company_ID) as
company_ID_Lenght,company_name,len(company_name) as
company_name_lenght from company_Information_1;
Display the Employee Character with 1 to 5.
select emp_ID,emp_name,substring(emp_name,1,5) as name_1_to_5 from
Employee_Information_3;
Find the a character in company name.
select c.company_ID,c.company_name,CHARINDEX('a',c.company_name) as
a_Index_Location from company_Information_1 c;
select *from Order_Information_1;

Find the Maximum salary of the employee.


select max(e.salary) as Max_Employee_Salary from Employee_Information_1 e;

Find the Minimum Salary of the employee.


select min(e.salary) as Min_Employee_Salary from Employee_Information_1 e;

Find the Average Salary of the Employee.


select avg(e.salary) as Avg_Employee_Salary from Employee_Information_1 e;
Count the Number of Employee.
select count(e.emp_ID) as Total_employee from Employee_Information_1 e;

Find the Total Salary of all employee.


select sum(e.salary) as All_Employee_Salary from Employee_Information_1 e;
Find the Employee that salary in the range 200 to 30000 using
between operator
select
e1.emp_ID,e.emp_name,e1.emp_contact,e1.emp_contact,e1.DOJ,e1.num_hour_
work,
e1.emp_country,e1.emp_city,e1.salary
from Employee_Information_1 e1,Employee_Information_3 e
where e1.salary between 200 and 30000 and e1.emp_ID=e.emp_ID;
Triggers
Triggers is used to display the message about the function performed by the user.
We use triggers in our all tables. Triggers on one table Medicine_Information are
as follow.

Delete Trigger
create trigger Medicine_Deleted_Trigger
on Medicine_Information
after delete
as begin
print 'One Medicine Record has been deleted from Medicine table'
end;
Insert Trigger
create trigger Medicine_insert_trigger
on Medicine_Information
after insert
as begin
print 'New Medicine has been inserted in Medicine Table'
end;

Update Trigger.
create trigger Medicine_Update_trigger
on Medicine_Information
after update
as begin
print'Record is updated in Medicine Table'
end;

You might also like