You are on page 1of 24

Nick Anderson

Juan Duarte

Pharmaceutical Database
By
Bit By Bit Database Development
PHARMACEUTICAL DATABASE REQUIREMENTS & RELATION TO ERD:

We designed a Patient table where we can store patients' information in a relational


database. Information such as a unique identifying SSN that is used as a primary key,a name,
age assuming positive numbers only, contact phone number and primary physician.The primary
physician creates a 1:1 relationship with the Doctor table to facilitate retrieving information and
optimizing records with doctors and prescriptions.
Along with patients’ information, we created a Doctor table that includes a unique
identifying SSN,a full name with the assumption it won't be bigger than 45 char, their field of
specialty, and years of experience. The doctors SSN creates a 1:1 relation with the Prescription
table which provides our clients with detailed information not only about the patients receiving
pharmaceutical products but also information on the doctors writing these prescriptions for the
pharmaceutical drugs.
In addition to keeping records of patients and their doctors, the information of the
pharmaceutical companies that manufacture pharmaceutical drugs will be stored and identified
by a unique ID, name and phone number. The unique ID will create a 1:m relationship between
drugs and the pharmaceutical company, and help create a weak entity with contracts and
pharmaceutical companies.
Pharmaceutical companies are in charge of manufacturing the drugs that will eventually
be sold by retail pharmacies where they will carry a “generic” name and can also carry a unique
trade name given by individual pharmacies. The names will be stored in the PrecriptDrug table
which has a 1:1 relation with the Prescription table, 1:M with a weak entity with the Pharmacy
table and M:1 relation with the PharmaComp table.
Individual pharmacies that will be stored and identified with a unique ID, name, address,
and phone number. Pharmacies will create a weak 1:m entity between a contracts table and
another with the prescription drugs.
The patients in our database will be stored in our database along with their physicians
creating a 1:1 relationship with the “Doctor” entity, primary physicians will be stored as foreign
keys in the “Patients” entity.
Prescriptions can be written by all physicians to any patients,our database will help keep
track of all pharmaceutical drugs from the pharmaceutical company down to individual patients.
8-13:
To satisfy the requirements for requirement 8, we created a weak entity to combine the
pharmacy and the actual drug. This way we could create cardinality where each drug can come
from many pharmacies, and each pharmacy can have many drugs. Neither the drug or the
pharmacy entity houses the price since the price can vary. The weak entity has an attribute price
which allows this requirement to be satisfied.
Requirement number 9 involves the cardinality between the entities patient, doctor,
prescription, and drug. Each prescription will have a unique RX number that will be used as the
primary key. The prescription will link between the drug and the doctor, and the drug and the
patient. The cardinality between the prescription and the patient/doctor will be one way, but each
patient can have many prescriptions. Likewise each doctor can write many prescriptions. So this
will be a 1:m relationship.
Requirement 10 specifies detail for the prescription entity. With this information we
added columns for quantity as an int, and details about the drug’s name which is found in the
drug entity. We made the generic name not null because from this information we made the
assumption that all drugs have a generic name. And, if the drug has a trade name, which is
allowed to be null, the drug has to come from the specific pharma company. For this reason the
pharma company Id is stored as foreign key in the drug entity. This is all connected via
prescription -> drug -> pharma company.
The one attribute that is added to prescription from requirement 11 is the date the
prescription was filled. This takes the DateTime data type. The other requirements of number 11
are found in the drug entity. Each prescription is linked to the dug entity which is linked to the
pharma company entity. So using the foreign key idPrescriptDrug we can track if this was a
generic drug and the pharma company it came from by joining the tables.
Contract requirements begin in requirement 12. We created a contract entity with the
attributes startdate, enddate, and textOfContract. The date attributes have a DatTime data type,
and the contract text is a string. The contract needs to connect one pharmacy to a pharma
company. For this reason we need weak entities in between the contract and each company
respectively. This way we can keep the company to company information out of the contract
entity. The contract only connects from one company to another, but each company can have
many contracts.
Each contract has a supervisor, but a supervisor can have many contracts. For this
reason we need to connect the supervisor to the contract via the weak entity that connects the
pharma company to the contract. This way we can keep the unique identifiers as the contract id
and the pharma company id, and keep the supervisor name non distinct.
PHARMACEUTICAL ENTITY RELATIONSHIP DIAGRAM:
PHARMACEUTICAL DATABASE CREATE TABLES:
-- MySQL Script generated by MySQL Workbench
-- Mon Feb 6 21:14:56 2023
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;


SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE,
SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERRO
R_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `doctor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `doctor` (
`id` INT NOT NULL AUTO_INCREMENT,
`ssn` VARCHAR(10) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`specialty` VARCHAR(45) NOT NULL,
`practice_since` VARCHAR(4) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `patient` (
`patientId` INT NOT NULL AUTO_INCREMENT,
`ssn` VARCHAR(10) NOT NULL,
`firstName` VARCHAR(45) NOT NULL,
`lastName` VARCHAR(45) NOT NULL,
`age` DATETIME NOT NULL,
`street` VARCHAR(100) NOT NULL,
`city` VARCHAR(100) NOT NULL,
`state` VARCHAR(2) NOT NULL,
`zipcode` VARCHAR(5) NOT NULL,
`primaryDoc` INT NOT NULL,
INDEX `fk_Patient_Doctor1_idx` (`primaryDoc` ASC) VISIBLE,
PRIMARY KEY (`patientId`),
CONSTRAINT `fk_Patient_Doctor1`
FOREIGN KEY (`primaryDoc`)
REFERENCES `doctor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PharmaComp`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PharmaComp` (
`pharmaCompId` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`phone` VARCHAR(10) NOT NULL,
PRIMARY KEY (`pharmaCompId`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `drug` (
`idPrescriptDrug` INT NOT NULL,
`trade_name` VARCHAR(45) NOT NULL,
`formula` VARCHAR(100) NOT NULL,
`pharmaCompId` INT NULL,
PRIMARY KEY (`idPrescriptDrug`),
INDEX `fk_PrescriptDrug_PharmaComp_idx` (`pharmaCompId` ASC) VISIBLE,
CONSTRAINT `fk_PrescriptDrug_PharmaComp`
FOREIGN KEY (`pharmaCompId`)
REFERENCES `PharmaComp` (`pharmaCompId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pharmacy` (
`name` VARCHAR(50) NOT NULL,
`address` VARCHAR(45) NOT NULL,
`phone` VARCHAR(11) NOT NULL,
`pharmacyId` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`pharmacyId`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `PrescriptDrug_has_pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `PrescriptDrug_has_pharmacy` (
`idPrescriptDrug` INT NOT NULL,
`pharmacyId` INT NOT NULL,
`price` DECIMAL(7,2) NOT NULL,
PRIMARY KEY (`idPrescriptDrug`, `pharmacyId`),
INDEX `fk_PrescriptDrug_has_pharmacy_pharmacy1_idx` (`pharmacyId` ASC) VISIBLE,
INDEX `fk_PrescriptDrug_has_pharmacy_PrescriptDrug1_idx` (`idPrescriptDrug` ASC) VISIBLE,
CONSTRAINT `fk_PrescriptDrug_has_pharmacy_PrescriptDrug1`
FOREIGN KEY (`idPrescriptDrug`)
REFERENCES `drug` (`idPrescriptDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PrescriptDrug_has_pharmacy_pharmacy1`
FOREIGN KEY (`pharmacyId`)
REFERENCES `pharmacy` (`pharmacyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `prescription`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `prescription` (
`rxNumber` INT NOT NULL AUTO_INCREMENT,
`date` DATETIME NULL,
`quantity` INT NOT NULL,
`idPrescriptDrug` INT NOT NULL,
`doctorId` INT NOT NULL,
`patientId` INT NOT NULL,
`pharmacyId` INT NULL,
`cost` DECIMAL(5,2) NULL,
PRIMARY KEY (`rxNumber`),
INDEX `fk_Prescription_PrescriptDrug1_idx` (`idPrescriptDrug` ASC) VISIBLE,
INDEX `fk_Prescription_Doctor1_idx` (`doctorId` ASC) VISIBLE,
INDEX `fk_Prescription_Patient1_idx` (`patientId` ASC) VISIBLE,
CONSTRAINT `fk_Prescription_PrescriptDrug1`
FOREIGN KEY (`idPrescriptDrug`)
REFERENCES `drug` (`idPrescriptDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Doctor1`
FOREIGN KEY (`doctorId`)
REFERENCES `doctor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Patient1`
FOREIGN KEY (`patientId`)
REFERENCES `patient` (`patientId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Contract` (
`contractId` INT NOT NULL AUTO_INCREMENT,
`startDate` DATETIME NOT NULL,
`endDate` DATETIME NOT NULL,
`textOfContract` VARCHAR(255) NOT NULL,
PRIMARY KEY (`contractId`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Contract_has_pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Contract_has_pharmacy` (
`contractId` INT NOT NULL,
`pharmacyId` INT NOT NULL,
PRIMARY KEY (`contractId`, `pharmacyId`),
INDEX `fk_Contract_has_pharmacy_pharmacy1_idx` (`pharmacyId` ASC) VISIBLE,
INDEX `fk_Contract_has_pharmacy_Contract1_idx` (`contractId` ASC) VISIBLE,
CONSTRAINT `fk_Contract_has_pharmacy_Contract1`
FOREIGN KEY (`contractId`)
REFERENCES `Contract` (`contractId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_has_pharmacy_pharmacy1`
FOREIGN KEY (`pharmacyId`)
REFERENCES `pharmacy` (`pharmacyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Contract_has_PharmaComp`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Contract_has_PharmaComp` (
`contractId` INT NOT NULL,
`pharmaCompId` INT NOT NULL,
PRIMARY KEY (`contractId`, `pharmaCompId`),
INDEX `fk_Contract_has_PharmaComp_PharmaComp1_idx` (`pharmaCompId` ASC) VISIBLE,
CONSTRAINT `fk_Contract_has_PharmaComp_Contract1`
FOREIGN KEY (`contractId`)
REFERENCES `Contract` (`contractId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_has_PharmaComp_PharmaComp1`
FOREIGN KEY (`pharmaCompId`)
REFERENCES `PharmaComp` (`pharmaCompId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Supervisor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Supervisor` (
`supervisorName` VARCHAR(50) NOT NULL,
`contractId` INT NOT NULL,
`pharmId` INT NOT NULL,
INDEX `fk_Supervisor_Contract_has_PharmaComp1_idx` (`contractId` ASC, `pharmId` ASC) VISIBLE,
CONSTRAINT `fk_Supervisor_Contract_has_PharmaComp1`
FOREIGN KEY (`contractId` , `pharmId`)
REFERENCES `Contract_has_PharmaComp` (`contractId` , `pharmaCompId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

The SQL Query Statements:

- -What drugs were prescribed most by doctors with 8 years of experience or more.Tables list drug’s by generic
name, and drug quantity total

CREATE VIEW drug_pres_count AS


SELECT pd.trade_name, COUNT(p.quantity) AS drug_total, d.practice_since
FROM doctor d JOIN Prescription p ON d.id = p.doctorId
JOIN drug pd ON pd.idPrescriptDrug = p.idPrescriptDrug
GROUP BY pd.trade_name, d.practice_since;
SELECT * FROM drug_pres_count;

SELECT trade_name, drug_total


FROM drug_pres_count
WHERE practice_since >= 8
ORDER BY drug_total DESC;

- -What Pharmacies and Pharmaceutical Companies have contracts together? And what are the contract IDs, and
supervisor names appointed to each contract?

CREATE VIEW phar_contracts AS


SELECT p.name AS phar_name, c.contractId, s.supervisorName, pc.name AS pCompName
FROM pharmacy p JOIN contract_has_pharmacy chp ON p.pharmacyid = chp.pharmacyid
JOIN contract c ON c.contractid = chp.contractid
JOIN Contract_has_PharmaComp chpc ON chpc.contractId = chp.contractId
JOIN supervisor s ON s.contractid = chpc.contractid
JOIN pharmaComp pc ON pc.pharmaCompId = chpc.pharmaCompId
GROUP BY phar_name, c.contractId, s.supervisorName, pCompName;
SELECT phar_name, contractId, supervisorName, pCompName
FROM phar_contracts
ORDER BY phar_name DESC;

-- What pharmacy has the cheapest generic drug? The following SQL statement could answer this
question:

WITH temp AS (SELECT MIN(price) AS cheap, p.name, d.trade_name


FROM mydb.PrescriptDrug_has_pharmacy d2p
JOIN mydb.drug d ON d2p.idPrescriptDrug=d.idPrescriptDrug
JOIN mydb.pharmacy p ON d2p.pharmacyId=p.pharmacyId GROUP BY p.pharmacyId, d.trade_name)
SELECT MIN(cheap) AS cheapest, name FROM temp GROUP BY name LIMIT 1;

-- What doctor has written the most scripts for pain meds (or a generic type of med)? If the generic name
for pain meds was ‘painmeds’ we could answer this question with the following query:

SELECT COUNT(rx.doctorId) as maxScripts, d.last_name, drug.trade_name


FROM mydb.Prescription rx JOIN mydb.Patient p ON rx.patientId=p.patientId
JOIN mydb.Doctor d ON d.id=rx.doctorId
JOIN mydb.drug drug ON rx.idPrescriptDrug=drug.idPrescriptDrug
GROUP BY d.last_name, drug.trade_name HAVING drug.trade_name='painmeds'
ORDER BY maxScripts DESC LIMIT 1;

– What pharmacy has the most different (unique generic) drugs?

WITH temp1 AS (SELECT COUNT(d2p.idPrescriptDrug) as drugtypes, d.trade_name, p.name


FROM mydb.PrescriptDrug_has_pharmacy as d2p
JOIN mydb.drug d ON d2p.idPrescriptDrug=d.idPrescriptDrug
JOIN mydb.pharmacy p ON d2p.pharmacyId=p.pharmacyId
GROUP BY d.trade_name, p.name)
SELECT count(drugtypes) AS mosttype, name FROM temp1 GROUP BY name ORDER BY
mosttype DESC LIMIT 1;

3NF Analysis:
When we designed our diagram we tried to avoid any functional dependencies. By doing so each
entity does not record redundant data. With only storing necessary data in each entity the database will
not have delete, insert, and update anomalies.
USE CASES:
GenerateData.java output:

The outputs display first 10 random doctors being inserted, secondly, 100 random patients being
inserted, and finally 100 random prescriptions being inserted into the database.
EXTRA: made class GeneratePharamaDrug.java and executed in main of DataGenerate to
insert drugs into weak entity PrescriptionDrug_has_pharmacy that connects drug entity to
pharmacy entity and holds the drugs price.

NEW PRESCRIPTION FORM:


SSN is not a valid SSN number (formatting is wrong)
Doctors SSN does not match doctors name, or SSN/Name is not in the database:

Patient has invalid SSN format


Patient is not in the database or Patient/SSN do not match:

Drug name is invalid:


Invalid drug quantity:

Successful prescription:
PRESCRIPTION FILL FORM:
RX number does not match Script name:

Pharmacy has invalid address:


Successful Prescription Fill:

NEW PATIENT FORM:


INVALID SSN Number:
-The SSN is invalid because it starts with a 9. The SSN must not start with a 9 or 0, it also cant
have 00 as the middle integers or end with 0000.
INVALID First And Last Name:
-The first and last name must only contain letters, it can't contain numbers or symbols, that is
why this input is invalid.

INVALID Date:
-The date is invalid because the year must be between the range of 1900 and 2023.
INVALID Date:
-The street address is invalid because it must contain a full address with the number, name and
indication of St, Dr, Ave, Rd, etc.

INVALID City And State Name:


-This city name and State are invalid because they contain numbers and symbols, when they’re
only allowed to contain letters.
INVALID Zip Code Number:
-The input for Zipcode is invalid because it contains 6 numbers. Only 5 or 9 numbers are
allowed, letters in the input would also make for an invalid input.

INVALID Primary Physician Name:


-The input must match a doctor in the database by first and last name. In this case the doctor's
last name was misspelled as “Whit” instead of “White” which resulted in the doctor not being
found in the database.
SUCCESSFUL Output:
-This is an output for when all the criteria in the fields is met, it results in a “Registration
Successful” message along with the information that has been stored in the Patient database.

SUCCESSFUL Profile Update:


-When the edit link is pressed, the user is taken to a page where they can update certain
information on their profile. Here it's shown how patient “101” was able to update his Address,
City, Zip Code and Primary Physician
INVALID Patient Search:
-The search function fails in the first attempt because the last name “Dir” wasn’t found in the
database. In the second attempt the search failed because the Patient ID doesn’t match the last
name of the patient stored under that Patient ID.

SUCCESSFUL Patient Search:


-When both the Patient ID and Patient Last Name match the database, the user's profile
information is returned.

Java JDBC Program: Pharmacy Drug Quantity Report

INVALID Pharmacy ID:


-This shows an invalid input for PharmacyID, because it's not an existing ID for a
Pharmacy stored in the database.
INVALID Dates:

-The input dates are invalid because they are not in the range of 1900 and 2023.
This results in an error message.

SUCCESSFUL Output:
-These successful outputs show different outputs depending on the Pharmacy ID and Date
Range.
-The first and second picture share the same Pharmacy ID but have a different range of dates
which results in different reports.
-The third picture shows a report with the same date range as the first picture but a different
Pharmacy ID which results in a different report.
CONCLUSION:

One of the first things we learned while completing part 1 of this project, was interpreting
requirements and translating them into an Entity Relationship Diagram. We were able to take the
requirements given by a pharmaceutical company, and the information provided to develop a specialized
database that met the company’s demands. While creating the entity relationship diagram we learned
how to use MySQL to generate the ERD where we could take advantage of features like creating primary
keys, foreign keys, and labeling relationships accordingly. These features allowed us to use the forward
engineering tool to create an SQL script from the ERD we had created. This project demonstrates how
demand in the real world creates the need for databases in order to optimize and improve business
processes.

You might also like