0% found this document useful (0 votes)
213 views31 pages

Blood Bank Management System

The document describes a proposed blood bank management system that would allow users to register as donors, request and receive blood, and manage inventory. Key features would include allowing online donor registration, organizing blood donation camps, managing donor data and medical records, facilitating blood requests between branches, and generating expiration reports. The system aims to more effectively connect blood donors with those in need of donations and save lives by providing this information online in a timely manner. It outlines entity relationships like donors, patients, registration teams, analysts, hospitals, and blood/blood components to model the data.

Uploaded by

Pep Jorghino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views31 pages

Blood Bank Management System

The document describes a proposed blood bank management system that would allow users to register as donors, request and receive blood, and manage inventory. Key features would include allowing online donor registration, organizing blood donation camps, managing donor data and medical records, facilitating blood requests between branches, and generating expiration reports. The system aims to more effectively connect blood donors with those in need of donations and save lives by providing this information online in a timely manner. It outlines entity relationships like donors, patients, registration teams, analysts, hospitals, and blood/blood components to model the data.

Uploaded by

Pep Jorghino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

lOMoARcPSD|18807539

Blood Bank Management System

Database Management Systems (Lovely Professional University)

NAME:- Akanksha Thakur

SECTION:- K21YK

ROLL NO:- A28

REG NO:- 12112361

DATE:- 08-11-2022
lOMoARcPSD|18807539
lOMoARcPSD|18807539

Blood Bank Management System

Contents
1.Introduction…………………………………………………………………………….1
1.1 Objectives…………………………………………………………………………...2
1.2 Features……………………………………………………………………………...2
1.3 Problem Statements…………………………………………………………………2
2. Problem Description....................................................................................................3
3. Conceptual Model........................................................................................................3
3.1. Relational Entity Diagram.....................................................................................4
4. Logic Model.................................................................................................................5
5. Normalization..............................................................................................................5
6. Data Dictionary............................................................................................................7
7. List of Queries...........................................................................................................13
7.1. Relational Algebra...............................................................................................14
7.2. SQL Queries.........................................................................................................15
8. Triggers and Stored Procedures.................................................................................16
9. Database Creation.......................................................................................................25

1
lOMoARcPSD|18807539

1. Introduction
Blood Bank Management System is a browser based solution that is designed to
store, process, retrieve and analyze information concerned with the administrative,
inventory management and clinical aspects of providing services within a blood
bank.
Donor Registration: The application allows the users to store the donor details in
the application itself. As soon as a donor is registered, a unique identification
number is assigned to him/her automatically which remains same throughout the
life time of donor. Any subsequent donations made by the same donor
automatically got linked up with the same identification number.

Donor physical and medical details: System allows the user to store physical and
medical details of the donor at the time of the registration which could later on be
edited during the time of subsequent donations. Application also allows the users to
check the eligibility criteria of the donors for making donations, depending on which
a track of the deferred donors can be kept for the future referrals.

TTI screening and Inventory management: The Application provides its users to
organize and manage the inventory (Blood bags, blood component etc.) for their
blood bank. User can store the records for blood bags present in the bank, the
blood components created and imported in the bank. Every single unit/component
will be assigned with a unique ID for which Bar code can be generated and can be
pasted on the bags itself for later on identification. The end user is able to store the
blood in quarantine till the time it has passed the TTI screening as per specified
standards issued by NACO. After which the blood which has passed the tests will be
moved to inventory with their unique identification numbers and the rejected blood
will be processed to the discard samples for which the reports can be generated any
time later on.

Blood requisition and Cross matching: The Application allows the user to request for
blood and blood transfusion for which the cross matching using the appropriate
technique can be carried out and the results can further be processed and analyzed
by the experts to issue the blood. Also various reports for blood requisition and the
cross matching can be generated at run time.

Secured environment and login facility: System provides the users a secured
environment to work inside the application by accepting the log in ID’s and
passwords from the users.

Report generation: Various comprehensive reports can be generated any time by the
end user to measure the performance parameters in the blood bank and also to
analyses the inventory and other aspects in blood bank. Blood is an important factor that
is very essential in the life of every organisms . Nowadays, we see that people lose their
precious life without getting the blood at the right time due to improper contact of the blood
donors .The software system is an online blood bank management system that helps in
managing various blood bank operations effectively.

2
lOMoARcPSD|18807539

1.1. OBJECTIVES
To create awareness and solve all these problems this online blood bank app can be of great
help as it provides required info in no time and also helps in quicker decision making .This
also helps to motivate people for Blood Donation easily.

1.2. FEATURES
 User can send blood request and they can check donor’s list.
 Management can organize Blood Donation Camps.
 Blood donors can register through online.
 Management can manage donor’s database by recording their physical and medical
statistics.
 Medically qualified management in blood bank for storage and issuance of blood.
 Blood request from one branch to another.
 Displays information about expired blood.

1.3. PROBLEM STATEMENT


The problem definition of the system is to launch an online interaction medium for the
blood donation management.
The main aim of this project is to help the people who needs blood in emergency and to
associate some donors who are willing to donate their blood to needy people and save their
lives.
The user will be able to :
 Search donors of suitable blood groups and contact them if needed.
 Donate blood by registering themselves with our system and can also become donors.
 Will be able to see the stock of various blood groups.
 Send request for blood via “contact us”.
 Get information about all the blood campaigns.

The proposed system will be designed to provide functionalities as mentioned below:

3
lOMoARcPSD|18807539

2. Problem Description

A Blood Bank is a place where blood is collected and stored for use by others who need
it due to health emergencies or a lack of blood. So, it is necessary to develop a data
management system to minimize the cost and time of manual labour to access blood
bank information.

• The donor will be the person who donates the blood; it will contain information
such as: name, age, gender, contact, blood group, address, disease;
• The patient will be the person who will receive the blood; this will have the
following attributes: ID, name, gender, blood group, contact, address;
• The blood bank is managed by a manager, who has a name, an ID and location;
• The donor can only make the donation once a month; at each donation, the
donated blood will be analysed by a clinical analyst, to find out if he suffers
from any blood-borne disease or not. If you have a disease and you are younger
than 18 or older than 60, will not be able to donate;
• An ID and blood group will be tagged (to the blood);
• Donors and patients are registered by a registration team. A registration team
can register multiple donors and patients, however, a donor or patient can only
be registered by one team member;
• Both registry team and clinical analyst have with attributes: ID and name;
• Hospitals and patients order blood from the blood bank through the manager, as
this is the one who handles the blood bags;
• Hospital will have the following attributes: ID, name, location;
• The date of registration for each patient and donor is important to the context.

3. Conceptual Model
3.1. Relational Entity Diagram

4
lOMoARcPSD|18807539

4. Logic Model

Manager (IdManager, nameM, IdBloodBank, nameBloodBank, locationBloodBank)


Registration_Team(IdRT, nameRT, idBloodBank)
Technical_Analyst (IdTA, nameTA, idBloodBank)

TechnicalAnalyst_Blood ( IdTA, IdBlood, bloodGroupBlood )

Blood (IdBlood, bloodGroupBlood, IdManager)

Blood_Patient ( IdBlood, bloodGroupBlood, IdPatient )

Patient (IdPatient, nameP, gender, bloodGroup, contact, address, IdManager, idRT,


dateRegisters, bloodGroupRequests, amountOfBloodRequests)
Donor (IdDonor, nameD, gender, bloodGroup, contact, address, age, disease, IdRT,
IdBlood, bloodGroupBlood, dateRegisters)
Hospital (IdHospital, nameH, location, IdManager,
bloodGroupRequests, amountOfBloodRequests)

5. Normalization
 Manager Table
Manager (IdManager, nameM, IdBloodBank, nameBloodBank, locationBloodBank)
1NF
Manager (IdManager, nameM, surnameG)
BloodBank (IdManager, IdBloodBank, name, avenue_street, number, neighborhood)

2NF / 3NF
Manager (IdManager, nameM, surnameG)
Manager_BloodBank(IdManager, IdBloodBank)
BloodBank (IdBloodBank, nameBB, avenue_street, number, neighborhood)

5
lOMoARcPSD|18807539

 Registration_Team Table
Registration_Team(IdRT, nameRT, IdBloodBank)
1NF / 2NF / 3NF
Registration_Team(IdRT, nameRT, IdBloodBank)

 Technical_Analyst Table
Technical_Analyst (IdTA, nameTA, IdBloodBank)

1NF / 2NF / 3NF


Technical_Analyst (IdTA, nameTA, IdBloodBank)

 TechnicalAnalyst_Blood Table

TechnicalAnalyst_Blood ( IdTA, IdBlood, bloodGroupBlood )

1NF / 2NF / 3NF

TechnicalAnalyst_Blood ( IdTA, IdBlood, bloodGroupBlood )

 Blood Table
Blood (IdBlood, bloodGroupBlood, IdManager)
1NF / 2NF / 3NF
Blood (IdBlood, bloodGroupBlood, IdManager)

 Blood_Patient Table

Blood_Patient ( IdBlood, bloodGroupBlood, IdPatient )

1NF / 2NF / 3NF

Blood_Patient ( IdBlood, bloodGroupBlood, IdPatient )

 Patient Table

6
lOMoARcPSD|18807539

Patient (IdPatient, nameP, gender, bloodGroup, contact, address, IdManager, IdRT,


dateRegisters, bloodGroupRequests, amountOfBloodRequests)
1NF
Patient (IdPatient, nameP, surnameP, gender, bloodGroup, contact, avenue_street,
number, neighborhood, IdManager, IdRT, dateRegisters)
Requests (IdPatient, bloodGroupRequests, amountOfBloodRequests)

2NF / 3NF
Patient (IdPatient, nameP, surnameP, gender, bloodGroup, contact, avenue_street,
number, neighborhood, IdManager, IdRT, dateRegisters)
Patient_Requests (IdPatient, IdRequests)
Requests (IdRequests, bloodGroupRequests, amountOfBloodRequests)

 Donor Table
Donor (IdDonor, nameD, gender, bloodGroup, contact, address, age, disease, IdRT,
IdBlood, bloodGroupBlood, dateRegisters)
1NF / 2NF / 3NF
Donor (IdDonor, nameD, gender, bloodGroup, contact, avenue_street, number,
neighborhood, age, disease, IdRT, idBlood, bloodGroupBlood, dateRegisters)

 Hospital Table
Hospital (IdHospital, nameH, location, idManager,
bloodGroupRequests, amountOfBloodRequests)
1NF
Hospital (IdHospital, nameH, avenue_street, number, neighborhood, idManager)
Requests (IdHospital, bloodGroupRequests, amountOfBloodRequests)

2NF / 3NF
Hospital (IdHospital, nomeH, avenue_street, number, neighborhood, IdManager)
Hospital_Requests (IdHospital, IdRequests)
Requests (IdRequests, bloodGroupRequests, amountOfBloodRequests)

6. Data Dictionary

7
lOMoARcPSD|18807539

Table Manager
Description Will contain manager information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdManager Manager Code SMALLINT 4 PK
nameM Manager’s First Name VARCHAR 30 NOT NULL
surnameM Manager’s Surname VARCHAR 30 NOT NULL

Table Manager_BloodBank
Description Will contain Blood Bank and Manager ID information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdManager Manager Code SMALLINT 4 PK
IdBloodBank Blood Bank Code SMALLINT 4 PK

Table BloodBank
Description Will contain Blood Bank information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdBloodBank Blood Bank Code SMALLINT 4 PK
nameBB Blood Bank Name VARCHAR 30 NOT NULL
Avenue or Street
avenue_street where the Blood VARCHAR 30 NOT NULL
Bank is located
Blood Bank building
number INTEGER 4 NOT NULL
number
Neighborhood where
neighborhood the Blood Bank is VARCHAR 30
located

Table Registration_Team

8
lOMoARcPSD|18807539

Description Will contain Registration Team information


Comments
Fields
Comments
Name Description Type Size
(PK / FK )
Registration Team
IdRT INTEGER 10 PK
member code
Team Member’s First
nameRT VARCHAR 30 NOT NULL
Name
Team Member’s
surnameRT VARCHAR 30 NOT NULL
Surname
IdBloodBank Blood Bank Code SMALLINT 4 FK

Table Technical_Analyst
Description Will contain Technical Analyst information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
Technical Analyst
IdTA INTEGER 10 PK
Code
Technical Analyst
nameTA VARCHAR 30 NOT NULL
First Name
Technical Analyst
surnameTA VARCHAR 30 NOT NULL
Surname
IdBloodBank Blood Bank Code SMALLINT 4 FK

Table Technical Analyst_ Blood


Will contain information of the Blood Code and Blood Group of
Description
the blood analyzed by the Technical Analyst
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
Technical Analyst
IdTA INTEGER 10 PK
Code
IdBlood Blood Code INTEGER 10 PK
Blood Group of
bloodGroup VARCHAR 3 PK
blood analyzed

9
lOMoARcPSD|18807539

Table Blood
Description Will contain information on donated blood
Comments
Type
Comments
Name Description Tipo Size
(PK / FK )
Donated Blood
IdBlood INTEGER 10 PK
Code
Blood Group of
bloodGroup VARCHAR 3 PK
donated blood
IdManager Manager Code SMALLINT 4 FK

Table Blood_Patient
Description Will contain information on the blood received by a patient
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
Donated Blood
IdBlood INTEGER 10 PK
Code
Blood Group of
bloodGroup blood received by a VARCHAR 3 PK
Patient
Code of the Patient
IdPatient who reciveid the INTEGER 10 PK
blood

Table Patient
Description Will contain Patient information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdPatient Patient Code INTEGER 10 PK
nameP Patient’s First Name VARCHAR 30 NOT NULL
surnameP Patient’s Surname VARCHAR 30 NOT NULL
gender Patient’s Gender CHAR 1 DEFAULT

10
lOMoARcPSD|18807539

‘M’
Patient’s Blood
bloodGroup VARCHAR 3 NOT NULL
Group
contacto Patient Contact VARCHAR 9 NOT NULL
Patient’s Avenue or
avenue_street VARCHAR 30 NOT NULL
Street
number House Number INTEGER 4 NOT NULL
Neighborhood
neighborhood where the Patient VARCHAR 30 NOT NULL
lives
Registration Team
IdRT INTEGER 10 FK
member code
IdManager Manager Code SMALLINT 4 FK
Date the patient was
dateRegisters DATE NOT NULL
registered

Table Patient_Requests
Description Will contain information on patients who requested blood
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdPatient Patient Code INTEGER 10 PK
IdRequests Blood Request Code INTEGER 10 PK

Table Donor
Description Will contain Donor information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdDonor Donor Code INTEGER 10 PK
nameD Donor First Name VARCHAR 30 NOT NULL
surnameD Donor Surname VARCHAR 30 NOT NULL
DEFAULT
gender Donor Gender CHAR 1
‘M’
Donor Blood Group
bloodGroup VARCHAR 3 NOT NULL
contacto Donor Contact VARCHAR 9 NOT NULL
avenue_street Donor Avenue or VARCHAR 30 NOT NULL

11
lOMoARcPSD|18807539

Street
number House Number INTEGER 4 NOT NULL
Neighborhood
neighborhood where the Donor VARCHAR 30 NOT NULL
lives
age Donor Age INTEGER 3 NOT NULL
Indicates whether
the donor has any
disease or not.
disease Have disease = 1 INTEGER 1 DEFAULT 0
There is no disease
=0
Donated Blood
IdBlood INTEGER 10 NULL
Code
Blood Group of
bloodGroupBlood VARCHAR 3 NULL
donated blood
Registration Team
IdRT INTEGER 10 FK
member code
Date the donor was
dateRegisters DATE NOT NULL
registered

Table Hospital
Description Will contain Hospital information
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdHospital Hospital Code INTEGER 10 PK
nameH Hospital Nome VARCHAR 30 NOT NULL
Avenue or Street
avenue_street where the Hospital is VARCHAR 30 NOT NULL
located
Hospital building
number INTEGER 4 NOT NULL
number
Neighborhood where
neighborhood VARCHAR 30 NOT NULL
the hospital is located
IdManager Manager Code SMALLINT 4 FK

Table Hospital_Requests
Description Will contain information from hospitals that requested blood
Comments

12
lOMoARcPSD|18807539

Fields
Comments
Name Description Type Size
(PK / FK )
IdHospital Hospital Code INTEGER 10 PK
IdRequests Blood Request Code INTEGER 10 PK

Table Requests
Description Will contain information on blood requests made
Comments
Fields
Comments
Name Description Type Size
(PK / FK )
IdRequests Request Code INTEGER 10 PK
Required Blood
bloodGroup VARCHAR 3 NOT NULL
Group
Amount of Blood
amountBlood requested (number INTEGER 10 NOT NULL
of bags)

7. List of Queries
1. List the names of the donors next to their address, who belong to the AB+ blood
group and are female.
2. List the names of tegistered donors who donated blood aged 18-28 years, and older
male and female donors who also donated blood.
3. List the registered donors who donated blood and who belong to the blood group
considered universal donor, as well as the patients who belong to the blood group
considered universal recipient.
4. List the names of workers who work in the same blood bank as technical analyst
‘Junaid’.
5. List registered donors who cannot donate (who have some illness).
6. The percentage of male donors without any disease, where the percentage of donors
without any disease is approximately 69.23%.
7. List the hospital and patient with the highest number of requested bags.

13
lOMoARcPSD|18807539

7.1. Relational Algebra


1.
Result < - πIdDonor, nameD, surnameD, gender, bloodGroup, number, neighborhood, avenue_streets

(σgender='F' (σbloodGroup ='AB+'(σidBlood !=NULL(σbloodGroupBlood !=NULL (donor)))))

2.
A < - πIdDonor, nameD, surnameD, gender, age(σage ≥ 18 (σage ≤ 28 (σdisease=0 (donor))))

B < - πIdDonor, nameD, surnameD, gender, age(σgender = 'M' (σdisease=0 (donor)))

C < - πIdDonor, nameD, surnameD, gender, age(σgender = 'F' (σdisease=0 (donor)))

Result < - A U B U C

3.
A < - πIdDonor, nameD, surnameD, bloodGroup(σbloodGroup='O-' (σdisease=0 (donor)))

B < - πIdPatient, nameP, surnameP, bloodGroup(σbloodGroup ='AB+' (patient)) Result < -


AUB

4.

A <- πIdBloodBank name ='Junaid'(technical_analyst)) E <- πidManager

(manager_bloodBank))
B <- πnameM, surnameM, IdBloodBank (πIdManager(Manager) |X|IdManager = IdManager
E
C <- πnameRT, surnameRT (πIdBloodBank |X| IdBloodBank = IdBloodBank A)

D <- πnameTA, surnameTA (πIdBloodBank |X| IdBloodBank = IdBloodBank A)


E <- πnameM, surnameM (B |X|IdBancoSangue =Id_BancoSangue A)
Result <- C U D U E

5.
Result < - πIdDonor, nameD, surnameD (σdisease != 0 (donor))

6.

14
lOMoARcPSD|18807539

A < - countname (σdisease = 0 (donor))


B < - countname (σdisease = 0 (σgender ='M' (donor)))
Result < - (B * 100) / A

7.
A <- πIdHospital, nameH, IdRequisita(hospital h

|X|h.IdHospital = hr.IdHospital hospital_requests hr)


B < - πIdHospital, nameH, amountBlood (A |X|A.idRequests = r.IdRequests requests r)

AB < - πIdHospital, nameH, amountBlood(𝜎amountBlood = max(amountBlood)(B))

C < - πIdPatient, nameP, IdRequests (patient p |X|p.IdPatient = pr.IdPatient patient_requests pr)

D < - πIdPatient, nameP, amountBlood (C |X|C.IdRequests = r.IdRequests requests r)

CD < -πIdPatient, nameP, amountBlood(𝜎amountBlood =max(amountBlood)(𝐷))


Result <- AB U CD

7.2. SQL Queries


1.
SELECT IdDonor, nameD, surnameD, gender, d.bloodGroup, avenue_street, number,
neighborhood FROM donor d, blood s
WHERE d.IdBlood = s.IdBlood
AND bloodGroupBlood = s.bloodGroup
AND gender='F'
AND d.bloodGroup = 'AB+';

2.
SELECT IdDonor, nameD, surnameD, gender, age
FROM donor d, blood s
WHERE d. IdBlood = s. IdBlood AND bloodGroupBlood = s.bloodGroup
AND age >=18 AND age <=28
UNION
SELECT IdDonor, nameD, surnameD, gender, max(age)
FROM donor d, blood s
WHERE d.IdBlood = s. IdBlood AND bloodGroupBlood = s. bloodGroup
AND gender = 'M'
UNION
SELECT IdDonor, nameD, surnameD, gender, max(age)
FROM donor d, blood s

15
lOMoARcPSD|18807539

WHERE d. IdBlood = s. idBlood AND bloodGroupBlood = s. bloodGroup AND


gender = 'F';

3.
SELECT IdDonor AS 'iD', nameD, surnameD, d. bloodGroup
FROM donor d, blood s
WHERE d. IdBlood = s. IdBlood
AND bloodGroupBlood = s. bloodGroup
AND d. bloodGroup = 'O-'
UNION
SELECT IdPatient, nameP, surnameP, bloodGroup
FROM patient
WHERE bloodGroup = 'AB+';

4.
SELECT nameM AS 'Name', surnameM AS 'Surname'
FROM manager m, manager_bloodbank mb
WHERE g.IdManager = gb.IdManager
AND gb.IdBloodBank = (SELECT IdBloodBank
FROM technical_analyst
WHERE nameTA = 'Junaid')
UNION
SELECT nameRT, surnameRT
FROM registration_team RT
WHERE RT. IdBloodBank = (SELECT IdBloodBank
FROM technical_analyst
WHERE nameAT = 'Junaid')
UNION
SELECT nameTA, surnameTA
FROM technical_analyst TA
WHERE TA.IdBloodBank = (SELECT IdBloodBank
FROM technical_analyst
WHERE nameTA = 'Junaid');

5.
SELECT IdDonor, nameD, surnameD
FROM donor
WHERE disease != 0;

6.
SELECT ((count(nameD)*100)/18) AS ‘Disease Free Male Donors (%)’
FROM donor
WHERE gender = 'M'

16
lOMoARcPSD|18807539

AND disease = 0;

7.
SELECT h.IdHospital AS ‘ID’, CONCAT(nameH, ' (hospital)') AS 'Name',
r.amountBlood AS ‘Amount of Blood’
FROM hospital h, requests r, hospital_requests hr
WHERE h.IdHospital = hr.IdHospital
AND hr.IdRequests = r.IdRequests
AND r.amountBlood = (SELECT max(amountBlood) FROM
requests re, hospital_requests hre
WHERE hre.IdRequests = re.IdRequests)
UNION ALL
SELECT p.IdPatient, CONCAT(nameP,' ',surnameP, ' (patient)'), r.amountBlood
FROM patient p, requests r, patient_requests pr
WHERE p.IdPatient = pr.IdPatient
AND pr.IdRequests = r.IdRequests
AND r.amountBlood = (SELECT max(amountBlood) FROM
requests re, patient_requests pre
WHERE pre.IdRequests = re.IdRequests);

8. Triggers and Stored Procedures


 Triggers
1. Trigger for donor data entry, where it checks if the donor has any disease and if the
donor has an age betweern 18-60. If it does not contai it and has the age in the range
above, the donor proceeeds with the donation, and the record of the donated blood is
inserted in the blood table; if not, no record will be inserted in the blood table, and in the
donor table, the id and blood group of the donated blood will be canceled.
DELIMITER //
CREATE TRIGGER tr_donor BEFORE INSERT
ON donor FOR EACH ROW
BEGIN
DECLARE Id INT;
IF ( ((NEW.age >= 18) AND (NEW. age <= 60)) AND (NEW.disease = 0) ) THEN
INSERT INTO blood ( bloodGroup, IdManager) VALUES ( NEW. bloodGroup,
(SELECT mb.IdManager FROM manager_bloodbank mb, registration_team rt
WHERE rt.IdRT = NEW.IdRT AND rt.IdBloodBank = mb. IdBloodBank));

SET Id = (SELECT max(IdBloodBank) FROM blood);


SET NEW. IdBloodBank = Id;
SET NEW.bloodGroupBlood = NEW. bloodGroup;
ELSE
SET NEW.IdBlood = NULL;
SET NEW. bloodGroupBlood = NULL;

17
lOMoARcPSD|18807539

END IF;
END//
DELIMITER ;

2. Trigger for donor data entry, where it checks if the donor has any disease and if the
donor has an age between 18-60. If it does not contain it and has the age in the range
above, the donor proceeds with the donation, and the record od the donated blood is
inserted in the blood table; if not, no record will be inserted in the blood table, and in the
donor table, the id and blood group of the donated blood will be canceled.
DELIMITER //
CREATE TRIGGER tr_donor_update BEFORE UPDATE
ON donor FOR EACH ROW
BEGIN
DECLARE id INT;
IF ( ((NEW.age >= 18) AND (NEW.age <= 60)) AND (NEW.disease = 0) ) THEN
INSERT INTO blood ( bloodGroup, IdManager) VALUES ( NEW.bloodGroup,
(SELECT mb.IdManager FROM manager_bloodbank mb, registration_team rt
WHERE rt.IdRT = NEW.IdRT AND rt. IdBloodBank = mb. IdBloodBank));

SET Id = (SELECT max(IdBlood) FROM blood);


SET NEW. IdBlood = Id;
SET NEW.bloodGroupBlood = NEW. bloodGroup;
ELSE
SET NEW.IdBlood = NULL;
SET NEW. bloodGroupBlood = NULL;
END IF; END//
DELIMITER ;

3. Trigger for entering patinent data, where the manager’s ID (in the patient table)
will match the manager’s ID that generates the Blood Bank, on which the registration
team member, who registered the patient works.

DELIMITER //
CREATE TRIGGER tr_patient BEFORE INSERT
ON patient FOR EACH ROW
BEGIN
SET new.IdManager = (SELECT mb.IdManager
FROM manager_bloodbank gb, registration_team rt
WHERE rt.IdRT = new.IdRT
AND rt.IdBloodBank = mb.IdBloodBank);
END//
DELIMITER ;

18
lOMoARcPSD|18807539

4. It counts the donors, after insertion, by their gender and by the fact that they
contain or not any disease.
DELIMITER //
CREATE TRIGGER count_donor BEFORE INSERT
ON donor FOR EACH ROW
BEGIN
DECLARE total_D, cont INT;

IF (NEW.gender = 'M' AND NEW.disease = 0) THEN


SET cont = (SELECT Donor_Male FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_doador SET Donor_Male = (cont + 1), Total_Donors = (total_D + 1)
WHERE IdCont = 1;
END IF;

IF (NEW.gender = 'M' AND NEW.disease = 1) THEN


SET cont = (SELECT Doador_Male_Sick from cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors from cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Male_Sick = (cont + 1) , Total_Donors = (total_D +
1) WHERE IdCont = 1;
END IF;

IF (NEW.gender = 'F' AND NEW disease = 1) THEN


SET cont = (SELECT Donor_Female_Sick FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female_Sick = (cont + 1) , Total_Doadores =
(total_D
+ 1) WHERE IdCont = 1;
END IF;

IF (NEW.gender = 'F' AND NEW.disease = 0) THEN


SET cont = (SELECT Donor_Female FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female = (cont + 1) , Total_Donors = (total_D + 1)
WHERE IdCont = 1;
END IF;

END//
DELIMITER ;

5. It counts the donors, after the removal of any donor record, by their gender and by the
fact that they contain or not any disease.
DELIMITER //
CREATE TRIGGER contar_donor_delete BEFORE DELETE

19
lOMoARcPSD|18807539

ON donor FOR EACH ROW


BEGIN
DECLARE total_D, cont INT;

IF (OLD.gender = 'M' AND OLD.disease = 0) THEN


SET cont = (SELECT Donor_Male FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Male = (cont - 1), Total_Donors = (total_D - 1)
WHERE IdCont = 1;
END IF;

IF (OLD.gender = 'M' AND OLD.disease = 1) THEN


SET cont = (SELECT Donor_Male_Sick from cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Doadores from cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Male_Sick = (cont - 1) , Total_Donors = (total_D -
1)
WHERE IdCont = 1;
END IF;

IF (OLD.gender = 'F' AND OLD.disease = 1) THEN


SET cont = (SELECT Donor_Female_Sick FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female_Sick = (cont - 1) , Total_Donors = (total_D -
1) WHERE IdCont = 1;
END IF;

IF (OLD.gender = 'F' AND OLD.disease = 0) THEN


SET cont = (SELECT Donor_Female FROM cont_donor WHERE IdCont = 1);
SET total_D = (SELECT Total_Donors FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female = (cont - 1) , Total_Donors = (total_D - 1)
WHERE IdCont = 1;
END IF;

END//
DELIMITER ;

6. It counts the donors, after updating a donor’s record, by their gender and by whether
or not they contain any disease.

DELIMITER //
CREATE TRIGGER contar_donor_update AFTER UPDATE
ON donor FOR EACH ROW
BEGIN
DECLARE total_D, cont INT;

20
lOMoARcPSD|18807539

IF (NEW.gender = 'M' AND NEW.disease = 0) THEN


SET cont = (SELECT count(*) FROM donor WHERE gender='M' AND disease=0);
SET total_D = (SELECT count(*) FROM donor);
UPDATE cont_donor SET Donor_Male = cont , Total_Donors = total_D WHERE
idCont
= 1;
END IF;

IF (NEW.gender = 'M' AND NEW.disease = 1) THEN


SET cont = (SELECT count(*) from donor WHERE gender ='M' AND doenca=1);
SET total_D = (SELECT count(*) from donor); disease
UPDATE cont_donor SET Donor_Male_Sick = cont , Total_Donors = total_D WHERE
idCont = 1;
END IF;

IF (NEW.gender = 'F' AND NEW.disease = 1) THEN


SET cont = (SELECT count(*) FROM donor WHERE gender ='F' AND disease =1);
SET total_D = (SELECT count(*) FROM donor);
UPDATE cont_donor SET Donor_Female_Sick = cont , Total_Donors = total_D
WHERE IdCont = 1;
END IF;

IF (NEW.gender = 'F' AND NEW.disease = 0) THEN


SET cont = (SELECT count(*) FROM donor WHERE gender ='F' AND disease =0);
SET total_D = (SELECT count(*) FROM donor);
UPDATE cont_donor SET Donor_Female = cont , Total_Donors = total_D WHERE
IdCont = 1;
END IF;

END//
DELIMITER ;

7. It counts the donors, after changing a donor’s register, by their gender and by the fact
that they contain or not any disease.

DELIMITER //
CREATE TRIGGER contar_donor_update2 BEFORE UPDATE
ON donor FOR EACH ROW
BEGIN
DECLARE cont INT;

IF (OLD.gender = 'M' AND OLD.disease = 0) THEN


SET cont = (SELECT Donor_Male FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Male = (cont - 1) WHERE IdCont = 1;
END IF;

21
lOMoARcPSD|18807539

IF (OLD.gender = 'M' AND OLD.disease = 1) THEN


SET cont = (SELECT Donor_Male_Sick from cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Male_Sick = (cont - 1) WHERE IdCont = 1;
END IF;

IF (OLD.gender = 'F' AND OLD.disease = 1) THEN


SET cont = (SELECT Donor_Female_Sick FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female_Sick = (cont - 1) WHERE IdCont = 1;
END IF;

IF (OLD.gender = 'F' AND OLD.disease = 0) THEN


SET cont = (SELECT Donor_Female FROM cont_donor WHERE IdCont = 1);
UPDATE cont_donor SET Donor_Female = (cont - 1) WHERE IdCont = 1;
END IF;

END//
DELIMITER ;

 Stored Procedures
1. Stored Procedure that prints the groups to which a certain blood type (passed by
parameter), you can donate.

DELIMITER ||
CREATE PROCEDURE bloodType (v_type varchar(3)) BEGIN
DECLARE info varchar(45);
IF (v_type = 'A+') THEN SET
info = 'A+, AB+';
SELECT info AS 'Groups to which you can donate';
END IF;

IF (v_type = 'A-') THEN


SET info = 'A+, A-, AB+, AB-';
SELECT info AS 'Groups to which you can donate';
END IF;

IF (v_type = 'B+') THEN SET


info = 'B+, AB+';
SELECT info AS 'Groups to which you can donate';
END IF;

IF (v_type = 'B-') THEN


SET info = 'B+, B-, AB+, AB-';

22
lOMoARcPSD|18807539

SELECT info AS 'Groups to which you can donate';


END IF;

IF (v_type = 'AB+') THEN


SET info = 'AB+';
SELECT info AS 'Groups to which you can donate'; END
IF;

IF (v_tipo = 'AB-') THEN SET


info = 'A+, AB+';
SELECT info AS 'Groups to which you can donate';
END IF;

IF (v_type = 'O+') THEN


SET info = 'A+, B+, AB+, O+';
SELECT info AS 'Groups to which you can donate';
END IF;

IF (v_type = 'O-') THEN


SET info = 'Todos os grupos sanguineos';
SELECT info AS 'Groups to which you can donate';
END IF;
END ||
DELIMITER;

2. Stored Procedure that lists donor belonging to a blood group and who can donate
blood.

DELIMITER //
CREEATE PROCEDURE donors (v_type varchar (3)) BEGIN
SELECT IdDonor, nameD, surnameD, gender, bloodGroup FROM donor WHERE
v_type = bloodGroup AND disease = 0;
END//
DELIMITER;

3. Stored Procedure for entering donor data.

DELIMITER ||
CREATE PROCEDURE insert_donor (name varchar(30), surname varchar(30), gender
char(1), group varchar(3), contact varchar(9), avenue varchar(30), nr int,
hood varchar(30), age int, disease int(1), RT int, dateRegisters date)
BEGIN
INSERT INSERT donor (nameD, surnameD, gender, bloodGroup, contact, avenida_rua,
number, neighborhood, age, disease, IdRT, dateRegisters) VALUES (name, surname,
gender, group, contact, avenue, nr, hood, age, disease, RT, dateRegisters);

23
lOMoARcPSD|18807539

END||
DELIMITER;

4. Stored Procedure for entering patient data.

DELIMITER ||
CREATE PROCEDURE insert_patient (name varchar(30), surname varchar(30), gender
char(1), group varchar(3), contact varchar(9), avenue varchar(30), nr
int, hood varchar(30), RT int, dateRegisters date)

BEGIN
INSERT INSERT patient (nameP, surnameP, gender, bloodGroup, contact,
avenida_rua, number, neighborhood, dateRegisters, idRT) VALUES
(name, surname, gender, group, contact, avenue, nr, hood, RT, dateRegisters);
END||
DELIMITER;

5. Stored Procedure for making a new donation from an already registered donor
(through your ID), if it has the necessary requirements for this.

DELIMITER //
CREATE PROCEDURE donation (ID int)
BEGIN
DECLARE group varchar(3);
DECLARE age, disease_d, rt_resgistration int;
IF (EXISTS (SELECT IdDonor FROM donor where IdDonor = ID)) THEN
SET group = (SELECT bloodGroup FROM donor WHERE IdDonor = ID);
SET age_a = (SELECT age FROM donor WHERE IdDonor = ID);
SET disease_d = (SELECT disease FROM donor WHERE IdDonor = ID);
SET rt_resgistration = (SELECT IdRT FROM donor WHERE IdDonor = ID);

IF ( ((age >= 18) AND (age <= 60)) AND (disease = 0) ) THEN
INSERT INTO blood ( bloodGroup, IdManager) VALUES (group, (SELECT
mb.IdManager FROM manager_bloodbank mb, registration_team rt
WHERE rt.IdRT = rt_resgistration
AND rt.IdBloodBank = mb.IdBloodBank));
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'The donor doesn't have the minimum requirements to make
the donation';
END IF;
ELSE
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'There's no registered donor with the referenced ID';
END IF;

24
lOMoARcPSD|18807539

END//
DELIMITER ;

6. Stored Procedure for viewing the stock of donated blood.

DELIMITER //
CREATE PROCEDURE stock()
BEGIN
CREATE TEMPORARY TABLE stock_blood ( ID
int PRIMARY KEY AUTO_INCREMENT, quantity
int,
group varchar(3));

INSERT INTO stock_blood (quantity,) VALUES


((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'A+'), 'A+'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'A-'), 'A-'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'B+'), 'B+'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'B-'), 'B-'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'AB+'), 'AB+'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'AB-'), 'AB-'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'O+'), 'O+'),
((SELECT COUNT(*) FROM blood WHERE bloodGroup = 'O-'), 'O-');
SELECT quantity AS ‘Quantity in Stock (bags)’ group AS ‘Blood Group’
FROM stock_blood;
DROP TEMPORARY TABLE stock_blood;
END//
DELIMITER ;

9. Database Creation
CREATE DATABASE blood_bank;

CREATE TABLE manager( IdManager


smallint(4) PRIMARY KEY, nameM
varchar(30) NOT NULL, surnameM
varchar(30) NOT NULL);

CREATE TABLE bloodbank ( IdBloodBank


smallint(4) PRIMARY KEY, nameBB
varchar(30) NOT NULL, avenue_street
varchar(30) NOT NULL, number integer(4)

25
lOMoARcPSD|18807539

NOT NULL, neighborhood varchar(30)


DEFAULT NULL);

CREATE TABLE manger_bloodbank


( IdManager smallint(4) NOT NULL,
IdBloodBank smallint(4) NOT NULL,
CONSTRAINT PK_manager_bloodBank PRIMARY KEY (IdManager, IdBloodBank),
CONSTRAINT FK_manager_bloodBank2 FOREIGN KEY (IdManager)
REFERENCES manager (IdManager),
CONSTRAINT FK_manager_bloodBank2 FOREIGN KEY (IdBloodBank)
REFERENCES bloodbank (IdBloodBank) ON DELETE CASCADE ON UPDATE
CASCADE);

CREATE TABLE registration_team ( IdRT


integer(10) PRIMARY KEY, nameER
varchar(30) NOT NULL, surnameER
varchar(30) NOT NULL, IdBloodBank
smallint(4) NOT NULL,
FOREIGN KEY (IdBloodBank) REFERENCES bloodbank (IdBloodBank) ON
DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE technical_analyst( IdTA


integer(10) PRIMARY KEY, nameTA
varchar(30) NOT NULL, surnameAT
varchar(30) NOT NULL, IdBloodBank
smallint(4) NOT NULL,
FOREIGN KEY (idBloodBank) REFERENCES bloodbank (IdBloodBank) ON
DELETE CASCADE ON UPDATE CASCADE);
CREATE TABLE blood ( IdBlood
integer(10) NOT NULL,
bloodGroup varchar(3) NOT NULL,
idManager smallint(4) NOT NULL,
CONSTRAINT PK_blood PRIMARY KEY (IdBlood, bloodGroup),
CONSTRAINT FK_blood2 FOREIGN KEY (IdManager) REFERENCES manager
(IdManager) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE patient ( IdPatient


integer(10) PRIMARY KEY, nameP
varchar(30) NOT NULL, surnameP
varchar(30) NOT NULL, gender
char(1) DEFAULT 'M', bloodGroup
varchar(30) NOT NULL, contact

26
lOMoARcPSD|18807539

varchar(9) NOT NULL, avenue_street


varchar(30) NOT NULL, number
integer(4) NOT NULL, bairro
varchar(30) NOT NULL, dateRegisers
date NOT NULL, IdRT integer(10)
NOT NULL, IdManager smallint(4)
NOT NULL,
FOREIGN KEY (IdRT) REFERENCES registration_team (IdRT),
FOREIGN KEY (IdManager) REFERENCES manager (IdManager) ON DELETE
CASCADE ON UPDATE CASCADE);

CREATE TABLE requests ( idRequests


integer(10) PRIMARY KEY,
bloodGroup varchar(3) NOT NULL,
amountBlood integer(10) NOT NULL);

CREATE TABLE patient_requests


( IdPatient integer(10) NOT NULL,
IdRequests integer(10) NOT NULL,
CONSTRAINT PK_patient_requests PRIMARY KEY (IdPatient, idRequests),
CONSTRAINT FK_patient_requests2 FOREIGN KEY (IdPatient) REFERENCES
patient (IdPatient),
CONSTRAINT FK_patient_requests3 FOREIGN KEY (IdRequests) REFERENCES
requests (IdRequests) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE hospital ( IdHospital


integer(10) PRIMARY KEY, nameH
varchar(30) NOT NULL, avenue_street
varchar(30) NOT NULL, number
integer(4) NOT NULL, neighborhood
varchar(30) NOT NULL,
IdManager smallint(4) NOT NULL,
FOREIGN KEY (IdManager) REFERENCES manager (IdManager) ON DELETE
CASCADE ON UPDATE CASCADE);

CREATE TABLE hospital_requests


( IdHospital integer(10) NOT NULL,
IdRequests integer(10) NOT NULL,
CONSTRAINT PK_hospital_request PRIMARY KEY (IdHospital, idRequests),
CONSTRAINT FK_hospital_requests FOREIGN KEY (IdHospital) REFERENCES
hospital (IdHospital),

27
lOMoARcPSD|18807539

CONSTRAINT FK_hospital_requisita3 FOREIGN KEY (IdRequests) REFERENCES


requests (IdRequests) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE blood_patient ( IdBlood


integer(10) NOT NULL, bloodGroup
varchar(3) NOT NULL, IdPatient
integer(10) NOT NULL,
CONSTRAINT PK_blood_patient PRIMARY KEY (IdBlood, bloodGroup, IdPaciente),
CONSTRAINT FK_blood_patient1 FOREIGN KEY (IdBlood, bloodGroup)
REFERENCES blood (IdBlood, bloodGroup),
CONSTRAINT FK_blood_patient2 FOREIGN KEY (IdPatient) REFERENCES patient
(IdPatient) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE technicalAnalyst_blood(


IdTA integer(10) NOT NULL, IdBlood
integer(10) NOT NULL, bloodGroup
varchar(3) NOT NULL,
CONSTRAINT PK_techinalAnalyst_Sangue PRIMARY KEY (IdTA, IdBlood,
bloodGroup),
CONSTRAINT FK_techinalAnalyst_blood1 FOREIGN KEY (idTA) REFERENCES
technical_analyst (IdTA),
CONSTRAINT FK_techinalAnalyst_blood2 FOREIGN KEY (IdBlood, bloodGroup)
REFERENCES blood (IdBlood, bloodGroup) ON DELETE CASCADE ON UPDATE
CASCADE);

CREATE TABLE donor( IdDonor


integer(10) PRIMARY KEY, nameD
varchar(30) NOT NULL, surnameD
varchar(30) NOT NULL, gender
char(1) DEFAULT 'M', bloodGroup
varchar(3) NOT NULL, contact
varchar(9) NOT NULL, avenue_street
varchar(30) NOT NULL, number
integer(4) NOT NULL, neighborhood
varchar(30) NOT NULL, age
integer(3) NOT NULL, disease
integer(1) DEFAULT 0, IdBlood
integer(10) NULL, bloodGroupBlood
varchar(3) NULL, IdRT integer(10)
NOT NULL, dateRegisters date NOT
NULL,
CONSTRAINT FK_donor FOREIGN KEY (IdBlood, bloodGroupBlood)
REFERENCES blood (IdBlood, bloodGrou),
CONSTRAINT FK_donor1 FOREIGN KEY (IdRT) REFERENCES registration_team

28
lOMoARcPSD|18807539

(IdRT) ON DELETE CASCADE ON UPDATE CASCADE);

CREATE TABLE cont_donor (


Donor_Male_Sick int not null default 0,
Donor_Male int not null null default 0,
Donor_Female_Sick int not null null default 0,
Donor_Female int not null null default 0,
Total_Donors int not null null default 0);

29

You might also like