Professional Documents
Culture Documents
The purpose of this database is to manage information for a drug store chain. It
includes data on patients, doctors, drugs, contracts, pharmaceutical companies, and the
drug store's stock. The database is broken into 10 tables with interlocking foreign keys
to link relationships.
We chose to break the overall database into ten tables with interlocking foreign
keys in order to link relationships.
For the tables on patients, doctors, pharmacies, and companies, the main information
needed is basic identifying information such as social security number, name, age,
address, and primary doctor for patients, social security number, name, specialty, and
length of practice for doctors, name, phone number, and address for pharmacies, and
name and contact information for companies.
For data relating to patients, doctors, pharmacies, and companies – the project
description mainly required basic identifying information such as: a patient's social
security number, name, age, address, and primary doctor; a doctor's social security
number, name, specialty, and length of practice; a pharmacies name, phone number,
and address; and finally a companies name and contact information. These
requirements are met easily through our choice of attributes in each of the tables listed
above, as seen in the diagram and explanation below.
The prescription and drug tables have more complex requirements. For
prescriptions, the doctor who prescribed it, the target patient, the drug and its quantity,
and prescription date must be recorded. For drugs, it can have a trade name, generic
name, or both. A prescription can be filled by a specific trade or generic drug or an
alternative. The drug used to fill a prescription must be carried by a specific pharmacy
with its own price and manufacturer recorded. These requirements are fulfilled using the
drug, fill, pharmacyDrug, and companyMakesDrug tables. The drug table keeps track of
each drug in the system, the pharmacyDrug table tracks a specific pharmacy's stock
and prices, the companyMakesDrug table keeps track of which company makes which
drug, and the fill table keeps track of each filled prescription, which pharmacy filled it,
and what drug was used.
In the second part of the project, we were tasked with adding code to handle user
input data. The main requirements were to collect specific data from the user and
ensure that it meets certain constraints. For example, social security numbers had to be
9 digits long and the middle two digits had to be between 01 and 99 (excluding 00), zip
codes had to be either 5 or 9 digits, and the year had to be between 1900 and 2022. To
fulfill these requirements, we used regular expression patterns to validate the user's
input. This helped to prevent any faulty or malicious data from being entered into the
database and protected against potential SQL injection attacks.
ER Model
ER Model Explanation
● contract:
○ Summary: This table contains information for the contract(s) between the
pharmaceutical companies and the pharmacies that sell the drugs that the
company manufactures. It has a Many to One relationship with both the
pharmacy table and the company table.
○ Attributes:
■ contractId: Holds the id of the contract.
● Primary key.
● Auto-generated.
■ pharmacyId: Holds the id of the pharmacy.
● Foreign key into the pharmacy table.
■ companyId: Holds the id of the company.
● Foreign key into the company table.
■ startDate: Contains the contract initialization date.
■ endDate: Contains the contract expiration date.
■ supervisorName: Holds the name of the supervisor of the
pharmacy.
● Assumed to be 45 characters or less.
■ contents: Contains the description of the contract between the
pharmaceutical company and the pharmacy that sells the drugs
that the company manufactures.
● Assumed to be 200 characters or less.
● prescription:
○ Summary: This table represents a single prescription prescribed by a
doctor to a patient. Has a Many to One relationship with the patient,
doctor, and drug tables. Has a One to Many relationship with the fill table.
○ Attributes:
■ rxNum: Holds the id of the prescription.
● Primary key.
● Auto-generated.
■ doctorId: Holds the id for the prescribing doctor.
● Foreign key into the doctor table.
■ patientId: Holds the id for the prescription’s patient.
● Foreign key into the patient table.
■ drugId: Holds the id for the drug being prescribed.
● Foreign key into the drug table.
■ prescribeDate: Holds the date the prescription was written.
■ quantity: Holds the quantity of the drug prescribed.
● fill:
○ Summary: This table represents the fill records for prescriptions. It has a
Many to One relationship to the prescription, drug, company, and
pharmacy tables. It exists separately from the prescription table in order to
allow multiple fills to be completed on a single prescription.
○ Attributes:
■ fillId: Holds the id for the prescription fill.
● Primary key.
● Auto-generated.
■ rxNum: Holds the id for the related prescription.
● Foreign key into the prescription table.
■ pharmacyId: Holds the id for the pharmacy that filled this
prescription.
● Foreign key into the pharmacy table.
■ fillDrugId: Holds the id of the drug that the prescription was filled
with. This is needed when the prescription was for a generic, and
was filled with a trade drug.
● Foreign key into the drug table.
■ fillGenericCompanyId: Holds the company that produced the
generic drug.
● Foreign key into the company table.
● Can be null. In this case, fillDrugId is a tradeDrug.
■ fillDate:
● Keeps track of a prescription’s fill date.
● company:
○ Summary: This table represents the company that manufactures the
pharmaceutical drugs. It has a One to Many relationship with
companyMakesDrug, contract, and fill.
○ Attributes:
■ companyId: Holds the id of the company.
● Primary key.
● Auto-generated.
■ companyName: Holds the name of the company.
● Assumed to be 45 characters or less.
■ companyPhone: Holds the phone number of the company.
● Assumed to be 17 characters or less.
● companyMakesDrug:
○ Summary: This table keeps track of the drugs any company makes. It has
a Many to One relationship with drug and company tables.
○ Attributes:
■ companyId: Holds the id of the related company.
● Primary Key
● Foreign key into the company table.
■ drugId: Holds the id of the related drug.
● Primary Key
● Foreign key into the drug table.
● pharmacy:
○ Summary: This table represents a pharmacy at a physical location. It has
a One to Many relationship with pharmacyDrug, contract, and fill tables.
○ Attributes:
■ pharmacyId: Holds the id of the pharmacy.
● Primary key.
● Auto-generated.
■ pharmacyName: Holds the name of the pharmacy.
● Assumed to be 45 characters or less.
■ pharmacyPhone: Holds the phone number of the pharmacy.
● Assumed to be 17 characters or less.
■ pharmacyZip: Holds the pharmacy’s zip.
● Assumed to be 9 characters or less.
■ pharmacyCity: Holds the pharmacy’s city.
● Assumed to be 45 characters or less.
■ pharmacyStreet: Holds the pharmacy’s street.
● Assumed to be 45 characters or less.
● pharmacyDrug:
○ Summary: This table represents the stock of a particular drug at a specific
pharmacy. It has a Many to One relationship with the pharmacy and drug
tables.
○ Attributes:
■ pharmacyId: Holds the id of the pharmacy.
● Primary key.
● Foreign key into the pharmacy table.
■ drugId: Holds the id of the drug.
● Primary key.
● Foreign key into the drug table.
■ price: Holds the price for one unit of the drug as an integer value.
● doctor:
○ Summary: This table contains information about the doctors in the
database. Allowing data such as prescriptions to be linked to an individual
doctor. It has a One to Many relationship with the patient and prescription
tables.
○ Attributes:
■ doctorId: Holds the id of the doctor
● Primary key
● Auto generated
■ doctorSSN: Holds the Social Security Number of the doctor.
● Assumed to be 9 characters.
■ doctorFirstName: Holds the name of the doctor.
● Assumed to be 45 characters or less.
■ doctorLastName: Holds the name of the doctor.
● Assumed to be 45 characters or less.
■ specialty: Holds the medical practice specialty of the doctor.
● Assumed to be 45 characters or less.
■ practiceSinceYear: Holds the year this doctor began their practice.
● patient:
○ Summary: This table contains information about the patients in the
database. Allowing tables such as prescription to link data to an individual
patient. It has a One to Many relationship with the prescription table and
has a Many to One relationship with the doctor table.
○ Attributes:
■ patientId: Holds the id for this patient.
● Primary key.
● Auto generated.
■ primaryDoctorId: Holds the id for this patient’s primary doctor.
■ patientSSN: Holds the Social Security Number of the patient.
● Assumed to be 9 characters.
■ patientFirstName: Holds the first name of the patient.
● Assumed to be 45 characters or less.
■ patientLastName: Holds the last name of the patient.
● Assumed to be 45 characters or less.
■ patientBirthdate: Holds the birthdate of the patient.
■ patientState: Holds the patient’s state.
● Assumed to be 45 characters or less.
■ patientZip: Holds the patient’s zip.
● Assumed to be 9 characters or less.
■ patientCity: Holds the patient’s city.
● Assumed to be 45 characters or less.
■ patientStreet: Holds the patient’s street.
● Assumed to be 45 characters or less.
● drug:
○ Summary: This table contains information about the drugs in the
database, including any trade or generic name that may be associated
with it.
○ Attributes:
■ drugId: Holds the id of the drug.
● Primary key.
● Auto-generated.
■ tradeName: Holds the trade-name of the drug.
● Can be null. In this case, the drug is generic, and
genericName holds the drug’s formula name.
● Assumed to be 45 characters or less.
■ genericName: Holds the generic-name of the drug.
● Assumed to be 45 characters or less.
Relational Schema
-- -----------------------------------------------------
-- Schema cst363
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema cst363
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `cst363`;
CREATE SCHEMA IF NOT EXISTS `cst363` DEFAULT CHARACTER SET utf8 ;
USE `cst363` ;
-- -----------------------------------------------------
-- Table `doctor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `doctor` (
`doctorId` INT NOT NULL AUTO_INCREMENT,
`doctorSSN` CHAR(9) NOT NULL,
`doctorFirstName` VARCHAR(45) NOT NULL,
`doctorLastName` VARCHAR(45) NOT NULL,
`specialty` VARCHAR(45) NOT NULL,
`practiceSinceYear` INT NOT NULL,
PRIMARY KEY (`doctorId`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `patient` (
`patientId` INT NOT NULL AUTO_INCREMENT,
`primaryDoctorId` INT NOT NULL,
`patientSSN` CHAR(9) NOT NULL,
`patientFirstName` VARCHAR(45) NOT NULL,
`patientLastName` VARCHAR(45) NOT NULL,
`patientBirthdate` DATE NOT NULL,
`patientState` VARCHAR(45) NOT NULL,
`patientZip` VARCHAR(9) NOT NULL,
`patientCity` VARCHAR(45) NOT NULL,
`patientStreet` VARCHAR(45) NOT NULL,
PRIMARY KEY (`patientId`),
INDEX `fk_patient_doctor1_idx` (`primaryDoctorId` ASC) VISIBLE,
CONSTRAINT `fk_patient_doctor1`
FOREIGN KEY (`primaryDoctorId`)
REFERENCES `doctor` (`doctorId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `company` (
`companyId` INT NOT NULL AUTO_INCREMENT,
`companyName` VARCHAR(45) NOT NULL,
`companyPhone` VARCHAR(17) NOT NULL,
PRIMARY KEY (`companyId`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pharmacy` (
`pharmacyId` INT NOT NULL AUTO_INCREMENT,
`pharmacyName` VARCHAR(45) NOT NULL,
`pharmacyPhone` VARCHAR(17) NOT NULL,
`pharmacyZip` VARCHAR(9) NOT NULL,
`pharmacyCity` VARCHAR(45) NOT NULL,
`pharmacyStreet` VARCHAR(45) NOT NULL,
PRIMARY KEY (`pharmacyId`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `drug` (
`drugId` INT NOT NULL AUTO_INCREMENT,
`tradeName` VARCHAR(45) NULL,
`genericName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`drugId`),
UNIQUE INDEX `tradeName_UNIQUE` (`tradeName` ASC) VISIBLE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `prescription`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `prescription` (
`rxNum` INT NOT NULL AUTO_INCREMENT,
`doctorId` INT NOT NULL,
`patientId` INT NOT NULL,
`drugId` INT NOT NULL,
`prescribeDate` DATE NOT NULL,
`quantity` INT NOT NULL,
PRIMARY KEY (`rxNum`),
INDEX `fk_prescription_drug1_idx` (`drugId` ASC) VISIBLE,
INDEX `fk_prescription_patient1_idx` (`patientId` ASC) VISIBLE,
INDEX `fk_prescription_doctor1_idx` (`doctorId` ASC) VISIBLE,
CONSTRAINT `fk_prescription_drug1`
FOREIGN KEY (`drugId`)
REFERENCES `drug` (`drugId`)
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,
CONSTRAINT `fk_prescription_doctor1`
FOREIGN KEY (`doctorId`)
REFERENCES `doctor` (`doctorId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `pharmacyDrug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pharmacyDrug` (
`pharmacyId` INT NOT NULL,
`drugId` INT NOT NULL,
`price` INT NOT NULL,
PRIMARY KEY (`pharmacyId`, `drugId`),
INDEX `fk_pharmacyDrug_pharmacy1_idx` (`pharmacyId` ASC) VISIBLE,
INDEX `fk_pharmacyDrug_drug1_idx` (`drugId` ASC) VISIBLE,
CONSTRAINT `fk_pharmacyDrug_pharmacy1`
FOREIGN KEY (`pharmacyId`)
REFERENCES `pharmacy` (`pharmacyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_pharmacyDrug_drug1`
FOREIGN KEY (`drugId`)
REFERENCES `drug` (`drugId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `contract` (
`contractId` INT NOT NULL AUTO_INCREMENT,
`pharmacyId` INT NOT NULL,
`companyId` INT NOT NULL,
`startDate` DATE NOT NULL,
`endDate` DATE NOT NULL,
`supervisorName` VARCHAR(45) NOT NULL,
`contents` VARCHAR(200) NOT NULL,
PRIMARY KEY (`contractId`),
INDEX `fk_contract_pharmacy1_idx` (`pharmacyId` ASC) VISIBLE,
INDEX `fk_contract_company1_idx` (`companyId` ASC) VISIBLE,
CONSTRAINT `fk_contract_pharmacy1`
FOREIGN KEY (`pharmacyId`)
REFERENCES `pharmacy` (`pharmacyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_contract_company1`
FOREIGN KEY (`companyId`)
REFERENCES `company` (`companyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `fill`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `fill` (
`fillId` INT NOT NULL AUTO_INCREMENT,
`rxNum` INT NOT NULL,
`pharmacyId` INT NOT NULL,
`fillDrugId` INT NOT NULL,
`fillGenericCompanyId` INT NULL,
`fillDate` DATE NOT NULL,
PRIMARY KEY (`fillId`),
INDEX `fk_fill_prescription1_idx` (`rxNum` ASC) VISIBLE,
INDEX `fk_fill_pharmacy1_idx` (`pharmacyId` ASC) VISIBLE,
INDEX `fk_fill_drug1_idx` (`fillDrugId` ASC) VISIBLE,
INDEX `fk_fill_company1_idx` (`fillGenericCompanyId` ASC) VISIBLE,
CONSTRAINT `fk_fill_prescription1`
FOREIGN KEY (`rxNum`)
REFERENCES `prescription` (`rxNum`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_fill_pharmacy1`
FOREIGN KEY (`pharmacyId`)
REFERENCES `pharmacy` (`pharmacyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_fill_drug1`
FOREIGN KEY (`fillDrugId`)
REFERENCES `drug` (`drugId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_fill_company1`
FOREIGN KEY (`fillGenericCompanyId`)
REFERENCES `company` (`companyId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `companyMakesDrug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `companyMakesDrug` (
`companyId` INT NOT NULL,
`drugId` INT NOT NULL,
PRIMARY KEY (`companyId`, `drugId`),
INDEX `fk_drugToCompany_drug1_idx` (`drugId` ASC) VISIBLE,
INDEX `fk_drugToCompany_company1_idx` (`companyId` ASC) VISIBLE,
CONSTRAINT `fk_drugToCompany_drug1`
FOREIGN KEY (`drugId`)
REFERENCES `drug` (`drugId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_drugToCompany_company1`
FOREIGN KEY (`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;
Conceptual Select Statements
1. Select all of the expired contract ids that a pharmacy had with any pharmaceutical
company.
2. Select all of the distinct drugs by tradeName that a specific doctor (by SSN) has
written prescriptions for.
3. Select all of the distinct generic drug names that at least one patient has been
prescribed.
4. Select the pharmacy name(s) that sell a specific drug by generic name with the
lowest price.
5. Select the average age of patients that have prescriptions filled through a specific
pharmacy
Application Requirement 1:
A program to generate 100 random patients, 10 random doctors and 100 random prescriptions.
Output
Application Requirement 2
A pharmacy manager requests a report of the quantity of drugs that have been used to fill
prescriptions by the pharmacy. The report will contain the names of drugs used and the quantity
of each drug used. Input is pharmacy id and a start and end date range.
Input / Output
An FDA government official is looking for the quantity of drugs that each doctor has prescribed.
The report shows the doctor’s name and quantity prescribed. Input is drug name (may be
partial name) and a start and end date range.
Input / Output
A doctor creates a new prescription and the patient gets the prescription filled at a pharmacy.
Complete the methods marked with //TODO in PrescriptionCreate and PrescriptionFill classes.
Form Input
Output Page
Example Invalid Input
Web app Requirement 2
A patient registers themselves to the system and chooses a primary care doctor. The patient
enters their name, social security number, address, birthdate and chooses a primary care
physician. The program validates the information for reasonableness. For example a primary
care physician should be a doctor who specializes in Family Medicine, Internal Medicine or, if
the patient is a child, Pediatrics and not surgery or oncology. Complete the newPatient method
marked with // TODO in PatientController class.
Form Input
Output Page
A patient updates their profile. Complete the methods getPatientForm and updatePatientForm
marked with //TODO in PatientController class.
Form Display
Form Input
Output Page
The primary goal of the application code was to manage user input, prevent any
incorrect or malicious input, and efficiently take that user input and pull, add, or edit data
from our database. To achieve this, we used regular expression patterns to verify user
input and ensure that it meets specific constraints, such as length, format, and range of
values. Simple SQL queries were then used to allow for user-determined searches and
other tasks.
In addition to the graphical user interface, our application code includes three
additional files: DataGenerate, PrescriptionReport, and FDADrugReport. The
DataGenerate file is used to generate random data for patients, doctors, and
prescriptions. The PrescriptionReport file is used to retrieve information about all the
drugs used to fill prescriptions at a specific pharmacy. Finally, the FDADrugReport file
retrieves information about the doctors and the quantity of a specific drug they have
prescribed.