Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

The Pill Store Database

Project Report

CST363: Intro to Database System


Spring 2023

Stacy Kirchner, Steven Casey


1

Introduction

Purpose:
The Pill Store is a large drug store chain where customers can get their prescriptions
filled. When a customer drops off a prescription to be fulfilled a new record is created in the Pill
Store database. The pharmacist filling the prescription is able to use the RXNumber and see the
name of the person the prescription is for, along with the name of the doctor that prescribed the
prescription. The pharmacist can also find the date the prescription was created, what drug it is
for, and the quantity. The price of the drugs are not uniform across stores. The pharmacist will be
able to look up the price of the drug for the prescription at their specific pharmacy.
If a prescription has a generic name, it can be filled from any pharmaceutical company
that has the formula name of the drug. However, if there is a trade name on the prescription, then
the prescription needs to be filled using a specific pharmaceutical company. Pharmaceutical
companies have contracts with pharmacies and the database will allow for these contracts to be
found. The contracts will show the start and end date, along with the text of the contract. There is
a specific supervisor for each contract that the pharmacy appoints. If a prescription has a trade
name, the database should be able to show the contract for the pharmaceutical company that is
needed for the prescription along with the supervisor, in case there are any issues with filling the
prescription.
Once a prescription is filled a customer will be given a receipt of the prescription being
filled. There will also be a receipt saved in the database. The receipt in the database will be
identified by the RXNumber so it will be linked with the original prescription. The receipt will
also show what date it was fulfilled and what pharmacy it was fulfilled at. It will show what is
the pharmaceutical company for the drug that was used to fulfill the prescription, assuming it
was a generic name and did not need a specific pharmaceutical company.
Our goal as a consulting company asked by the pill store is to develop this database that
will allow the store to be able to record the prescriptions that will be filled at their stores. The
database will allow the pharmacy to be able to monitor what pharmaceutical companies they are
supplying for the most. The Pill Store fulfills a large amount of prescriptions, and this database
will allow them to store and organize the prescriptions in a way that will make it easier for the
pharmacists to view this data.
2

Database Structure

The database consists of 10 tables. The main tables are Prescription, Drug, and Pharmacy. The
next page shows the entity-relationship diagram.

Database Table Description


Patient Contains the patient’s information

Doctor Contains the doctor’s work information

Prescription Contains the patient, the drug, date, quantity,


and doctor who wrote it

Drug Contains the generic and trade name along

Drug_has_Company Contains the Company that manufacturers a


drug

Company Contains the company contact information

Pharmacy Contains the pharmacy contact information

Price Contains the price a pharmacy has for a drug

Contract Contains the date the contract is valid, the


company it is for, text of the contract, and
who is the supervisor

Supervisor Contains the supervisor information and what


contract they are responsible for
3

ER Model
4

Relational Schema

See Appendix A

Questions

These five questions demonstrate how the data in this database can be managed.
1. What pharmaceutical company sells the most drugs at each pharmacy?
select PharmacyID, company.CompanyID, COUNT(prescription.RXNumber) as
TotalSales
from company
join drug_has_company on drug_has_company.company_companyid =
company.companyid
join drug on drug.drugid = drug_has_company.drug_drugid
join prescription on drug.drugid = prescription.drug_drugID
join pharmacy p on prescription.Pharmacy_PharmacyID = p.PharmacyID
group by company.CompanyID, PharmacyID;

2. What is the most prescribed drug by year?


select Date, drug_drugid as DrugID
from prescription
where drug_drugid = (select MAX(drug_drugid) from prescription);

3. When is the next expiring contract and what are the supervisor’s details?
select ContractID, s.*, PhoneNumber, EndDate
from contract
join supervisor s on contract.Supervisor_SupervisorID = s.SupervisorID
join pharmacy p on s.Pharmacy_PharmacyID = p.PharmacyID
order by EndDate asc
LIMIT 1;

4. What is the patientSSN, patientName, and what are their prescriptions?

select PatientSSN, FirstName, RXNumber


from patient
5

join prescription p on patient.doctor_doctorId = p.doctor_doctorId


order by firstname;

5. What is the revenue created by each drug?


select d.DrugID, SUM(p.DrugPrice * (select COUNT(Drug_DrugID) from
prescription))
from drug d
join price p on d.DrugID = p.Drug_DrugID
join prescription p2 on d.DrugID = p2.Drug_DrugID
group by d.DrugID;

Database Design Process

The first step we took in designing the database was to look at what was being asked
from us. We examine the requirements that the database would need to meet. Once we
understood the requirements, we started thinking about what tables would need to be included.
After a table was created, there was thought into what the table should consist of, along what
would be the primary keys and foreign keys. The foreign keys were based on what tables would
need to be joined together. The variables in each table are also requirements the user will need to
look up all the information they need in the table.
After working with our database, we realized our tables had a lot of extra information that
was not necessary and made it more difficult to work with.To address normalization, we made
tables with attributes specific to the primary key, and then used MySQL Workbench to connect
them, ensuring nothing is unnecessarily reused.
We eliminated some foreign keys and attributes that were not needed and slightly
restructured our diagram. The biggest change to our database was we added the table
Drug_has_Company that allows a drug to have a relationship with the company that makes the
drug.
Conclusion

The project allowed us to work as a team to use SQL in a real world setting. It helped us
gain a deeper understanding of primary keys, foreign keys, and constraints. It also allowed us to
practice planning out a database using an ER Diagram. The project allowed us to understand how
having a solid database design can help ease the process of creating a database. The
Entity-Relationship diagram was more useful than we both thought it would be. We were able to
successfully create a database that allows a drug store to effortlessly record and monitor their
prescriptions. This database is a great starting point for any drug store chain that wants to
organize their data.
6

WebApp Demo

Write a new prescription (for Doctors only)

Write a new prescription (for Doctors only) INVALID INPUT


7

Request a prescription to be filled (for Patients only)

Request a prescription to be filled (for Patients only) INVALID INPUT


8

Register a Patient

JDBC Demo

DataGenerate.java
9

Manager_load_data.java

Appendix

Appendix A

-- 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,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema pill_store_db
10

-- -----------------------------------------------------

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

-- -----------------------------------------------------
-- Table `Doctor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Doctor` (
`DoctorID` INT NOT NULL AUTO_INCREMENT,
`DoctorSSN` VARCHAR(9) NOT NULL,
`FirstName` VARCHAR(255) NOT NULL,
`LastName` VARCHAR(255) NOT NULL,
`Specialty` VARCHAR(255) NOT NULL,
`YearStarted` INT NOT NULL,
PRIMARY KEY (`DoctorID`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Patient` (
`PatientID` INT NOT NULL AUTO_INCREMENT,
`PatientSSN` VARCHAR(9) NOT NULL,
`FirstName` VARCHAR(255) NOT NULL,
`LastName` VARCHAR(255) NOT NULL,
`BirthDate` DATE NOT NULL,
`Street` VARCHAR(255) NOT NULL,
`City` VARCHAR(255) NOT NULL,
`State` VARCHAR(255) NOT NULL,
`Zip` VARCHAR(255) NOT NULL,
`Doctor_DoctorID` INT NOT NULL,
PRIMARY KEY (`PatientID`, `Doctor_DoctorID`),
INDEX `fk_Patient_Doctor1_idx` (`Doctor_DoctorID` ASC) VISIBLE,
CONSTRAINT `fk_Patient_Doctor1`
FOREIGN KEY (`Doctor_DoctorID`)
REFERENCES `Doctor` (`DoctorID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
11

-- -----------------------------------------------------
-- Table `Company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Company` (
`CompanyID` INT NOT NULL,
`CompanyName` VARCHAR(255) NOT NULL,
`PhoneNumber` VARCHAR(10) NOT NULL,
PRIMARY KEY (`CompanyID`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Drug` (
`DrugID` INT NOT NULL,
`TradeName` VARCHAR(100) NOT NULL,
`GenericName` VARCHAR(100) NOT NULL,
PRIMARY KEY (`DrugID`),
UNIQUE INDEX `TradeName_UNIQUE` (`TradeName` ASC) VISIBLE)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Pharmacy` (
`PharmacyID` INT NOT NULL,
`PharmacyName` VARCHAR(255) NOT NULL,
`Address` VARCHAR(255) NOT NULL,
`PhoneNumber` VARCHAR(10) NOT NULL,
PRIMARY KEY (`PharmacyID`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Doctor_has_Drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Doctor_has_Drug` (
`Doctor_DoctorID` INT NOT NULL,
`Drug_DrugID` INT NOT NULL,
PRIMARY KEY (`Doctor_DoctorID`, `Drug_DrugID`),
12

INDEX `fk_Doctor_has_Drug_Drug1_idx` (`Drug_DrugID` ASC)


VISIBLE,
CONSTRAINT `fk_Doctor_has_Drug_Drug1`
FOREIGN KEY (`Drug_DrugID`)
REFERENCES `Drug` (`DrugID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Price`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Price` (
`DrugPrice` DECIMAL(6,2) NOT NULL,
`Pharmacy_PharmacyID` INT NOT NULL,
`Drug_DrugID` INT NOT NULL,
PRIMARY KEY (`Pharmacy_PharmacyID`, `Drug_DrugID`),
INDEX `fk_Price_Drug1_idx` (`Drug_DrugID` ASC) VISIBLE,
CONSTRAINT `fk_Price_Pharmacy1`
FOREIGN KEY (`Pharmacy_PharmacyID`)
REFERENCES `Pharmacy` (`PharmacyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Price_Drug1`
FOREIGN KEY (`Drug_DrugID`)
REFERENCES `Drug` (`DrugID`)
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` DATE NOT NULL,
`Quantity` INT NOT NULL,
`DateFilled` DATE NULL,
`Drug_DrugID` INT NOT NULL,
`Patient_PatientID` INT NOT NULL,
`Doctor_DoctorID` INT NOT NULL,
`Pharmacy_PharmacyID` INT NULL,
13

PRIMARY KEY (`RXNumber`, `Drug_DrugID`, `Patient_PatientID`, `Doctor_DoctorID`),


UNIQUE INDEX `RXNumber_UNIQUE` (`RXNumber` ASC) VISIBLE,
INDEX `fk_Prescription_Drug1_idx` (`Drug_DrugID` ASC) VISIBLE,
INDEX `fk_Prescription_Patient1_idx` (`Patient_PatientID` ASC) VISIBLE,
INDEX `fk_Prescription_Doctor1_idx` (`Doctor_DoctorID` ASC) VISIBLE,
INDEX `fk_Prescription_Pharmacy1_idx` (`Pharmacy_PharmacyID` ASC) VISIBLE,
CONSTRAINT `fk_Prescription_Drug1`
FOREIGN KEY (`Drug_DrugID`)
REFERENCES `Drug` (`DrugID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Patient1`
FOREIGN KEY (`Patient_PatientID`)
REFERENCES `Patient` (`PatientID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Doctor1`
FOREIGN KEY (`Doctor_DoctorID`)
REFERENCES `Doctor` (`DoctorID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Pharmacy1`
FOREIGN KEY (`Pharmacy_PharmacyID`)
REFERENCES `Pharmacy` (`PharmacyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Supervisor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Supervisor` (
`SupervisorID` INT NOT NULL,
`SupervisorName` VARCHAR(255) NOT NULL,
`Pharmacy_PharmacyID` INT NOT NULL,
PRIMARY KEY (`SupervisorID`, `Pharmacy_PharmacyID`),
INDEX `fk_Supervisor_Pharmacy1_idx` (`Pharmacy_PharmacyID` ASC) VISIBLE,
CONSTRAINT `fk_Supervisor_Pharmacy1`
FOREIGN KEY (`Pharmacy_PharmacyID`)
REFERENCES `Pharmacy` (`PharmacyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
14

-- -----------------------------------------------------
-- Table `Contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Contract` (
`ContractID` INT NOT NULL,
`PharmacyName` VARCHAR(255) NOT NULL,
`StartDate` DATE NOT NULL,
`EndDate` DATE NOT NULL,
`Text` LONGTEXT NULL,
`Company_CompanyID` INT NOT NULL,
`Pharmacy_PharmacyID` INT NOT NULL,
`Supervisor_SupervisorID` INT NOT NULL,
PRIMARY KEY (`ContractID`, `Company_CompanyID`, `Pharmacy_PharmacyID`,
`Supervisor_SupervisorID`),
INDEX `fk_Contract_Company1_idx` (`Company_CompanyID` ASC) VISIBLE,
INDEX `fk_Contract_Pharmacy1_idx` (`Pharmacy_PharmacyID` ASC) VISIBLE,
INDEX `fk_Contract_Supervisor1_idx` (`Supervisor_SupervisorID` ASC) VISIBLE,
CONSTRAINT `fk_Contract_Company1`
FOREIGN KEY (`Company_CompanyID`)
REFERENCES `Company` (`CompanyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_Pharmacy1`
FOREIGN KEY (`Pharmacy_PharmacyID`)
REFERENCES `Pharmacy` (`PharmacyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_Supervisor1`
FOREIGN KEY (`Supervisor_SupervisorID`)
REFERENCES `Supervisor` (`SupervisorID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Drug_has_Company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Drug_has_Company` (
`Drug_DrugID` INT NOT NULL,
`Company_CompanyID` INT NOT NULL,
PRIMARY KEY (`Drug_DrugID`, `Company_CompanyID`),
15

INDEX `fk_Drug_has_Company_Company1_idx` (`Company_CompanyID` ASC) VISIBLE,


INDEX `fk_Drug_has_Company_Drug1_idx` (`Drug_DrugID` ASC) VISIBLE,
CONSTRAINT `fk_Drug_has_Company_Drug1`
FOREIGN KEY (`Drug_DrugID`)
REFERENCES `Drug` (`DrugID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Drug_has_Company_Company1`
FOREIGN KEY (`Company_CompanyID`)
REFERENCES `Company` (`CompanyID`)
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;

You might also like