Professional Documents
Culture Documents
Juan Duarte
Pharmaceutical Database
By
Bit By Bit Database Development
PHARMACEUTICAL DATABASE REQUIREMENTS & RELATION TO ERD:
-- -----------------------------------------------------
-- 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;
- -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
- -What Pharmacies and Pharmaceutical Companies have contracts together? And what are the contract IDs, and
supervisor names appointed to each contract?
-- What pharmacy has the cheapest generic drug? The following SQL statement could answer this
question:
-- 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:
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.
Successful prescription:
PRESCRIPTION FILL FORM:
RX number does not match Script name:
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.
-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.