You are on page 1of 9

Chris Weilacker

Kirk Kosinski
Patrick Cao
OJ Alcaraz
CST 363
GEO Elves
2/18/2020

Project Part 1
After reviewing the project requirements, the GEO Elves have come up with the
database schema listed on the following pages to store the data necessary for the new
confidential online drug store chain being developed by a FAANG company. This database
schema should meet all the requirements given to us except for the following:
● We did not force recently added doctors to have at least one patient, though the design
does connect a prescription in such a way that a patient and a doctor must be connected
in order for a prescription to be listed.
● When a pharmaceutical company is deleted from the database the current schema does
not delete the drugs from the list. We have decided that either we will implement this
functionality with a Cascade Delete or programmatically in the web interface.

Here are some other notes we had:


● While the SSN may be identifying we don't think it is proper security to use it as a
Primary Key and have it listed in tables that doctors, pharmacists, and patients have
access to. As such we have created patientId and doctorId columns that are Auto
Incrementing int as primary keys for data security and combining the two in a
Patient_Doctor_Relationship table.
● In the case of the patients table, we have included a birthDate column that would be
used to calculate age. This would be more pertinent information and also is regularly
asked as a security question when dispensing prescriptions.
ER Diagram
DB Schema Create Script

-- MySQL Script generated by MySQL Workbench


-- Sun Feb 16 21:42:04 2020
-- 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,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema drugstore_db
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `drugstore_db` ;

-- -----------------------------------------------------
-- Schema drugstore_db
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `drugstore_db` ;
USE `drugstore_db` ;

-- -----------------------------------------------------
-- Table `drugstore_db`.`pharmaceutical_companies`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`pharmaceutical_companies` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`pharmaceutical_companies` (


`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`phone` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`specialties`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`specialties` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`specialties` (


`id` INT NOT NULL,
`name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`doctors`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`doctors` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`doctors` (


`id` INT NOT NULL AUTO_INCREMENT,
`ssn` CHAR(9) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`specialty_id` INT NOT NULL,
`xp` INT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ssn_UNIQUE` (`ssn` ASC) VISIBLE,
INDEX `fk_doctors_1_idx` (`specialty_id` ASC) VISIBLE,
CONSTRAINT `fk_doctors_1`
FOREIGN KEY (`specialty_id`)
REFERENCES `drugstore_db`.`specialties` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`patients`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`patients` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`patients` (


`id` INT NOT NULL AUTO_INCREMENT,
`ssn` CHAR(9) NOT NULL,
`name` VARCHAR(100) NOT NULL,
`dob` DATE NOT NULL,
`address1` VARCHAR(50) NOT NULL,
`city` VARCHAR(40) NOT NULL,
`state` CHAR(2) NOT NULL,
`zip` CHAR(5) NOT NULL,
`primary_physician` INT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ssn_UNIQUE` (`ssn` ASC) VISIBLE,
INDEX `fk_patients_1_idx` (`primary_physician` ASC) VISIBLE,
CONSTRAINT `fk_patients_1`
FOREIGN KEY (`primary_physician`)
REFERENCES `drugstore_db`.`doctors` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`patdoc_relationship`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`patdoc_relationship` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`patdoc_relationship` (


`id` INT NOT NULL AUTO_INCREMENT,
`patient_id` INT NOT NULL,
`doctor_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_patdoc_match_patients_idx` (`patient_id` ASC) VISIBLE,
INDEX `fk_patdoc_match_doctors_idx` (`doctor_id` ASC) VISIBLE,
CONSTRAINT `fk_patdoc_match_patients`
FOREIGN KEY (`patient_id`)
REFERENCES `drugstore_db`.`patients` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_patdoc_match_doctors`
FOREIGN KEY (`doctor_id`)
REFERENCES `drugstore_db`.`doctors` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`prescriptions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`prescriptions` ;
CREATE TABLE IF NOT EXISTS `drugstore_db`.`prescriptions` (
`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`quantity` INT NOT NULL,
`drug_id` INT NOT NULL,
`refills_allowed` INT GENERATED ALWAYS AS (),
`patdoc_id` INT NOT NULL,
INDEX `fk_prescriptions_patdoc_idx` (`patdoc_id` ASC) VISIBLE,
PRIMARY KEY (`ID`),
UNIQUE INDEX `uk_drug_patdoc` (`drug_id` ASC, `patdoc_id` ASC) VISIBLE,
CONSTRAINT `fk_prescriptions_patdoc`
FOREIGN KEY (`patdoc_id`)
REFERENCES `drugstore_db`.`patdoc_relationship` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`drugs`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`drugs` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`drugs` (


`ID` INT NOT NULL,
`pharmaceutical_company_id` INT NOT NULL,
`trade_name` VARCHAR(255) NOT NULL,
`generic_name` VARCHAR(255) NOT NULL,
`prescriptions_ID` INT UNSIGNED NOT NULL,
PRIMARY KEY (`ID`, `pharmaceutical_company_id`),
UNIQUE INDEX `trade_name_UNIQUE` (`trade_name` ASC) VISIBLE,
INDEX `fk_drugs_pharmaceutical_companies_idx` (`pharmaceutical_company_id` ASC)
VISIBLE,
INDEX `fk_drugs_prescriptions1_idx` (`prescriptions_ID` ASC) VISIBLE,
CONSTRAINT `fk_drugs_pharmaceutical_companies`
FOREIGN KEY (`pharmaceutical_company_id`)
REFERENCES `drugstore_db`.`pharmaceutical_companies` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_drugs_prescriptions1`
FOREIGN KEY (`prescriptions_ID`)
REFERENCES `drugstore_db`.`prescriptions` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`pharmacies`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`pharmacies` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`pharmacies` (


`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`address1` VARCHAR(50) NOT NULL,
`city` VARCHAR(40) NOT NULL,
`state` CHAR(2) NOT NULL,
`zip` CHAR(5) NOT NULL,
`phone` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`pharmacy_drugs`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`pharmacy_drugs` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`pharmacy_drugs` (


`pharmacy_id` INT NOT NULL,
`drug_id` INT NOT NULL,
`price` DECIMAL(7,2) NOT NULL,
INDEX `fk_pharmacy_drugs_pharmacies1_idx` (`pharmacy_id` ASC) INVISIBLE,
INDEX `fk_pharmacy_drugs_drugs1_idx` (`drug_id` ASC) VISIBLE,
PRIMARY KEY (`pharmacy_id`, `drug_id`),
CONSTRAINT `fk_pharmacy_drugs_pharmacies1`
FOREIGN KEY (`pharmacy_id`)
REFERENCES `drugstore_db`.`pharmacies` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_pharmacy_drugs_drugs1`
FOREIGN KEY (`drug_id`)
REFERENCES `drugstore_db`.`drugs` (`ID`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`supervisors`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`supervisors` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`supervisors` (


`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`pharmacy_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_supervisors_pharmacies_idx` (`pharmacy_id` ASC) VISIBLE,
CONSTRAINT `fk_supervisors_pharmacies`
FOREIGN KEY (`pharmacy_id`)
REFERENCES `drugstore_db`.`pharmacies` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `drugstore_db`.`pharmacy_contracts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore_db`.`pharmacy_contracts` ;

CREATE TABLE IF NOT EXISTS `drugstore_db`.`pharmacy_contracts` (


`id` INT NOT NULL,
`company_id` INT NOT NULL,
`start_date` DATE NOT NULL,
`end_date` DATE NOT NULL,
`contract_text` TEXT NOT NULL,
`supervisor_id` INT NOT NULL,
INDEX `fk_pharmacy_contracts_pharmaceutical_companies1_idx` (`company_id` ASC)
VISIBLE,
INDEX `fk_pharmacy_contracts_pharmacies1_idx` (`id` ASC) VISIBLE,
INDEX `fk_pharmacy_contracts_supervisors1_idx` (`supervisor_id` ASC) VISIBLE,
CONSTRAINT `fk_pharmacy_contracts_pharmaceutical_companies1`
FOREIGN KEY (`company_id`)
REFERENCES `drugstore_db`.`pharmaceutical_companies` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_pharmacy_contracts_pharmacies1`
FOREIGN KEY (`id`)
REFERENCES `drugstore_db`.`pharmacies` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `fk_pharmacy_contracts_supervisors1`
FOREIGN KEY (`supervisor_id`)
REFERENCES `drugstore_db`.`supervisors` (`id`)
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