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

Drug Store Database

Cerberus Security Concepts

CST 363 Project 1

May 17, 2022

Richard Rivera, Steve Forgacs

1
General Information
This is a proposal by Cerberus Security Concepts to develop a 3NF compliant relational database
system for the drug store chain XYZ.

Database Requirements
XYZ has requested a relational database system that will gather and store data to support
operations across the chain’s pharmacies. The data to be gathered includes patient, prescription,
physician, pharmaceutical manufacturer, pharmacy/pharmaceutical contracts, pharmacy
supervisor date for contracts, and drug information.

The patient data will include an identifier, the social security number, name, address, phone
number, and birthdate.

The physician data will include an identifier, the physician’s name, address, phone number,
specialty, and start of practice.

The pharmaceutical company data will include an identifier, name, address, and phone number.

Pharmacy date will include an identifier, the name, address, phone number, and a link to the drug
inventory of the pharmacy.

Prescription data will include an identifier, the prescription date, the patient identification number,
the physician prescriber, an identifier where the prescription is to be filled, the quantity and drug
prescribed, the sell price, a field on whether the drug is ‘Dispense as Written’, and a date for when
the prescription is filled.

Drug data will include an identifier, the name of the drug, the cost, manufacturer, and trade name.

There will be an inventory table that will include an identifier, the amount of the drug in stock, the
drug identifier, the cost, and the selling price.

Supervisor data will include an identifier, the name of the supervisor, the pharmacy identifier the
supervisor is responsible for, and a phone number.

Contract date will include an identifier, the pharmacy, and pharmaceutical companies the contract
references, the start and end dates of the contract, the supervisor in charge of the contract, and the
text of the contract.

2
Database Structure
The database will consist of nine tables. The tables consist of Patient, Physician, Prescription,
Drug, Pharmaceutical Company, Pharmacy, Inventory, PharmacySupervisor, and Contract.

Patient Contains patient data

Physician Contains prescribing physician’s data

Prescription Contains prescription data

PharmaCompany Contains pharmaceutical company data

Drug Contains information about prescribed drugs

Pharmacy Contains information for each pharmacy

Inventory Contains drug stock and cost data

PharmacySupervisor Contains information about contract supervisors

Contract Contains contract information

Functionality
The database will allow the storage and retrieval of data relating to the fulfillment of prescriptions,
including the insertion, update, and deletion of data pertaining to all aspects related to
prescriptions.

Database Design Process


Our database design process was to take the provided requirements and design a simple prototype
database to meet those requirements. We proceeded to research information on drugs and privacy
and modified the tables to better fit the requirements and general database usage.

3
Changes to the requirements request
A phone number field has been added to the patient and physician tables. The phone number is a
variable length string up to fifteen (15) characters to accommodate international number and will
need to be formatted as a numeric string without any special characters before insertion.

All address fields will be a variable length string of up to 100 characters which should include all
relevant data.

Patient identification will be generated at the time of data entry. There will be a field for social
security number, but due to privacy it will not be part of the database identification record. The
patient age requirement was changed to use a birthdate so that it does not need to be updated
constantly.

Physician social security number requirement removed due to privacy considerations. The
requirement for years of experience was changed to the date the physicians practice start to
eliminate constant updates.

Database EER Diagram

4
Five Sample Questions and Queries
1. Determine the drug with the maximum number of prescriptions.
select drugprescribed, COUNT(drugprescribed) as Times_Prescribed
from prescription
group by drugprescribed
order by Times_Prescribed DESC
limit 1;
2. List patients that had prescriptions not given by their primary physician.
select pat.name as Non_Primary_Prescriptions from patient pat, prescription pres
where pat.primaryphysician != pres.prescriber;
3. Determine which doctor prescribed the most/least number of prescriptions.

4. Determine the pharmacy that filled the greatest number of prescriptions.

5. Determine the cost of drugs for all filled prescriptions.

Conclusions
We have learned that keeping the names of foreign keys in mind when creating new foreign keys
is necessary, and the next time a little more typing initially will save aggravation later as key names
need to be changed.

Still to do is to create the five sample questions and queries.

Database SQL Schema

-- MySQL Workbench Forward Engineering


-- Project 1 CST363
-- Richard Rivera, Steve Forgacs

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
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `drugstore` ;

-- -----------------------------------------------------
-- Schema drugstore
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `drugstore` DEFAULT CHARACTER SET utf8 ;
SHOW WARNINGS;
USE `drugstore` ;

-- -----------------------------------------------------
-- Table `drugstore`.`Physician`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Physician` ;

SHOW WARNINGS;

5
CREATE TABLE IF NOT EXISTS `drugstore`.`Physician` (
`idPhysician` VARCHAR(16) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`address` VARCHAR(100) NULL,
`phone` VARCHAR(15) NULL,
`specialty` VARCHAR(20) NULL,
`startofpractice` DATE NULL,
PRIMARY KEY (`idPhysician`))
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idPatient_UNIQUE` ON `drugstore`.`Physician` (`idPhysician` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Patient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Patient` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Patient` (
`idPatient` VARCHAR(16) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`ssn` VARCHAR(9) NOT NULL,
`birthdate` DATETIME NOT NULL,
`address` VARCHAR(100) NULL,
`phone` VARCHAR(15) NULL,
`primaryphysician` VARCHAR(16) NULL,
PRIMARY KEY (`idPatient`),
CONSTRAINT `primaryphysician`
FOREIGN KEY (`primaryphysician`)
REFERENCES `drugstore`.`Physician` (`idPhysician`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idPatient_UNIQUE` ON `drugstore`.`Patient` (`idPatient` ASC) VISIBLE;

SHOW WARNINGS;
CREATE UNIQUE INDEX `ssn_UNIQUE` ON `drugstore`.`Patient` (`ssn` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `primaryphysician_idx` ON `drugstore`.`Patient` (`primaryphysician` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`PharmaCompany`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`PharmaCompany` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`PharmaCompany` (
`idPharmaCompany` VARCHAR(45) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`address` VARCHAR(45) NULL,
`phone` VARCHAR(15) NULL,
PRIMARY KEY (`idPharmaCompany`))
ENGINE = InnoDB;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Drug`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Drug` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Drug` (

6
`idDrug` VARCHAR(16) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`cost` DECIMAL(20,10) UNSIGNED NULL,
`manufacturer` VARCHAR(45) NULL,
`tradename` VARCHAR(45) NULL,
PRIMARY KEY (`idDrug`),
CONSTRAINT `manufacturer`
FOREIGN KEY (`manufacturer`)
REFERENCES `drugstore`.`PharmaCompany` (`idPharmaCompany`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idDrug_UNIQUE` ON `drugstore`.`Drug` (`idDrug` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `manufacturer_idx` ON `drugstore`.`Drug` (`manufacturer` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Inventory`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Inventory` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Inventory` (
`idInventory` VARCHAR(45) NOT NULL,
`stock` INT NULL,
`drugid` VARCHAR(16) NOT NULL,
`cost` DECIMAL(20,10) UNSIGNED NOT NULL,
`sellingprice` DECIMAL(20,10) UNSIGNED NOT NULL,
PRIMARY KEY (`idInventory`),
CONSTRAINT `drugName`
FOREIGN KEY (`drugid`)
REFERENCES `drugstore`.`Drug` (`idDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE INDEX `drug_idx` ON `drugstore`.`Inventory` (`drugid` ASC) VISIBLE;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idInventory_UNIQUE` ON `drugstore`.`Inventory` (`idInventory` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Pharmacy`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Pharmacy` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Pharmacy` (
`idPharmacy` VARCHAR(16) NOT NULL,
`name` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`phone` VARCHAR(15) NULL,
`inventory` VARCHAR(45) NULL,
PRIMARY KEY (`idPharmacy`),
CONSTRAINT `inventory`
FOREIGN KEY (`inventory`)
REFERENCES `drugstore`.`Inventory` (`idInventory`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idPharmacy_UNIQUE` ON `drugstore`.`Pharmacy` (`idPharmacy` ASC) VISIBLE;

7
SHOW WARNINGS;
CREATE INDEX `inventory_idx` ON `drugstore`.`Pharmacy` (`inventory` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Prescription`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Prescription` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Prescription` (
`idPrescription` VARCHAR(16) NOT NULL,
`dateprescribed` DATE NOT NULL,
`patient` VARCHAR(16) NULL,
`prescriber` VARCHAR(16) NULL,
`fillpharmacy` VARCHAR(16) NULL,
`quantity` INT NOT NULL DEFAULT 0,
`price` DECIMAL(20,2) NULL,
`daw` BINARY NOT NULL DEFAULT TRUE COMMENT 'dispense as written (no substitutions, medically
necessary, do not interchange, etc)',
`datefilled` DATE NULL,
`drugprescribed` VARCHAR(16) NOT NULL,
PRIMARY KEY (`idPrescription`),
CONSTRAINT `patient`
FOREIGN KEY (`patient`)
REFERENCES `drugstore`.`Patient` (`idPatient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `prescriber`
FOREIGN KEY (`prescriber`)
REFERENCES `drugstore`.`Physician` (`idPhysician`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fillpharmacy`
FOREIGN KEY (`fillpharmacy`)
REFERENCES `drugstore`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `drugprescribed`
FOREIGN KEY (`drugprescribed`)
REFERENCES `drugstore`.`Drug` (`idDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idPrescription_UNIQUE` ON `drugstore`.`Prescription` (`idPrescription` ASC)
VISIBLE;

SHOW WARNINGS;
CREATE INDEX `patient_idx` ON `drugstore`.`Prescription` (`patient` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `prescriber_idx` ON `drugstore`.`Prescription` (`prescriber` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `pharmacy_idx` ON `drugstore`.`Prescription` (`fillpharmacy` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`PharmacySupervisor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`PharmacySupervisor` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`PharmacySupervisor` (
`idSupervisor` VARCHAR(16) NOT NULL,
`name` VARCHAR(45) NOT NULL,

8
`pharmacy` VARCHAR(16) NOT NULL,
`phone` VARCHAR(15) NULL,
PRIMARY KEY (`idSupervisor`),
CONSTRAINT `pharmacy`
FOREIGN KEY (`pharmacy`)
REFERENCES `drugstore`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE INDEX `pharmacy_idx` ON `drugstore`.`PharmacySupervisor` (`pharmacy` ASC) VISIBLE;

SHOW WARNINGS;

-- -----------------------------------------------------
-- Table `drugstore`.`Contract`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `drugstore`.`Contract` ;

SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `drugstore`.`Contract` (
`idContract` VARCHAR(45) NOT NULL,
`pharma` VARCHAR(16) NOT NULL,
`pharmaco` VARCHAR(45) NOT NULL,
`startdate` DATE NOT NULL,
`enddate` DATE NULL,
`contract` LONGTEXT NULL,
`supervisor` VARCHAR(16) NULL,
PRIMARY KEY (`idContract`),
CONSTRAINT `pharma`
FOREIGN KEY (`pharma`)
REFERENCES `drugstore`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `pharmaco`
FOREIGN KEY (`pharmaco`)
REFERENCES `drugstore`.`PharmaCompany` (`idPharmaCompany`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `supervisor`
FOREIGN KEY (`supervisor`)
REFERENCES `drugstore`.`PharmacySupervisor` (`idSupervisor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

SHOW WARNINGS;
CREATE UNIQUE INDEX `idContract_UNIQUE` ON `drugstore`.`Contract` (`idContract` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `pharmacy_idx` ON `drugstore`.`Contract` (`pharma` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `pharmaco_idx` ON `drugstore`.`Contract` (`pharmaco` ASC) VISIBLE;

SHOW WARNINGS;
CREATE INDEX `supervisor_idx` ON `drugstore`.`Contract` (`supervisor` ASC) VISIBLE;

SHOW WARNINGS;

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

insert into physician values('410085733', 'House', 'New Jersey', '609-555-1234', 'Diagnostic


Medicine', '2004-11-16');
insert into physician values('530102289', 'Wilson', 'New Jersey', '609-555-1234', 'Oncology',
'2004-11-16');
insert into physician values('401703096', 'Chase', 'California', '609-555-1234', 'Allergies',
'2004-11-16');

9
insert into physician values('110093361', 'Cameron', 'New Mexico', '609-555-1234', 'Pediatrics',
'2004-11-16');
insert into physician values('993091199', 'Davis', 'London, England', '609-555-1234',
'Cardiology', '2004-11-16');

insert into patient values('11A0', 'David', '123579011', '2005-11-16', '1111 King Avenue, North
Hollywood, CA 91605', '3105551215', '530102289');
insert into patient values('22B0', '234003799', 'Michael', '2006-11-16', '5640 Woodstock St.,
South Pasadena, CA 90032', '6265551325', '410085733');
insert into patient values('33C0', '777036510', 'Jason', '2007-11-16', '3221 Altas Street, South
Pasadena, CA 91030', '626555456', '530102289');
insert into patient values('44D0', '566903221', 'Christian', '2008-11-16', '2105 Waverly Street,
Palo Alto, CA 94301', '6505558556', '993091199');
insert into patient values('55E0', '669017715', 'Sasha', '2009-11-16', '1004 North Bristol Ave.,
Glendale, CA 91020', '8185553982', '993091199');
insert into patient values('66F0', '899045691', 'Natalie', '2010-11-16', '477 S Oakland Ave,
Pasadena, CA 91106', '6265553333', '993091199');

insert into pharmacompany values('JJ', "Johnson & Johnson", 'Addy1', '1-866-565-2873');


insert into pharmacompany values('PFZ', "Pfizer", 'Addy2', '1-800-879-4377');
insert into pharmacompany values('MD', "Moderna", 'Addy3', '1-866-665-3953');
insert into pharmacompany values('MK', "Merck", 'Addy4', '1-800-545-2028');
insert into pharmacompany values('NV', "Novartis", 'Addy5', '1-888-669-6682');

insert into drug values('77G0', 'Dostinex', 24.35, 'JJ', 'Dostinex');


insert into drug values('88H0', 'Comirnaty', 39.50, 'PFZ', 'Comirnaty');
insert into drug values('99I0', 'Parlodel', 34.29, 'MD', 'Parlodel');
insert into drug values('100J0', 'Ibuprofen', 5.95, 'MK', 'Advil');
insert into drug values('110K0', 'Sectral', 6.25, 'NV', 'Sectral');
insert into drug values('120L0', 'Tenornmn', 14.35, 'JJ', 'Tenornmn');
insert into drug values('130M0', 'Allegra', 4.99, 'JJ', 'Allegra');
insert into drug values('140N0', 'Amoxicillin', 55.29, 'JJ', 'Amoxicillin');
insert into drug values('150O0', 'Ibuprofen', 10.50, 'JJ', 'Ibuprofen');
insert into drug values('160P0', 'Zyrtec', 25.99, 'JJ', 'Zyrtec');

insert into inventory values(2, 512, '130M0', 0.20, 4.99);


insert into inventory values(3, 512, '99I0', 28.00, 34.29);
insert into inventory values(4, 512, '140N0', 25.00, 55.29);
insert into inventory values('834', 512, '77G0', 25.00, 756.00);
insert into inventory values('8364', 512, '150O0', 15.00, 2356.00);

insert into pharmacy values('WALG', 'Walgreens', 'Hoolywood Blvd.', '111-233-4557', null);


insert into pharmacy values('RITE', 'Rite Aid', 'New York Ave.', '661-290-8961', '834');
insert into pharmacy values('CVS', 'CVS Pharmacy', 'Florida Street', '1033-659-1099', null);

insert into prescription values(1, '2019-11-19', '11A0', '410085733', 'WALG', 70 , 5, false,


null, '77G0');
insert into prescription values(2, '2020-01-10', '22B0', '530102289', 'RITE', 30 , 12, false,
null, '88H0');
insert into prescription values(3, '2021-06-13', '33C0', '410085733', 'CVS', 12 , 22, false,
null, '99I0');
insert into prescription values(4, '2022-05-08', '44D0', '110093361', 'WALG', 120 , 15, false,
null, '100J0');
insert into prescription values(5, '2022-05-08', '33C0', '110093361', 'CVS', 80 , 10, true,
'2202-05-08', '99I0');

insert into pharmacysupervisor values(11, 'Mike 1', 'WALG', '555-555-5555');


insert into pharmacysupervisor values(12, 'Jack 1', 'RITE', '555-123-1244');
insert into pharmacysupervisor values(13, 'Alan 1', 'CVS', '555-232-2122');
insert into pharmacysupervisor values(21, 'Mike 2', 'WALG', '555-555-5556');
insert into pharmacysupervisor values(22, 'Jack 2', 'RITE', '555-123-1245');
insert into pharmacysupervisor values(23, 'Alan 2', 'CVS', '555-232-2123');
insert into pharmacysupervisor values(31, 'Mike 3', 'WALG', '555-555-5557');
insert into pharmacysupervisor values(32, 'Jack 3', 'RITE', '555-123-1246');
insert into pharmacysupervisor values(33, 'Alan 3', 'CVS', '555-232-2124');

insert into contract values(1, 'CVS', 'JJ', '2021-01-01', '2023-01-01', 'CVS agrees to buy some
drugs at marginal cost & plan to sell at markup .20', 31);
insert into contract values(2, 'WALG', 'PFZ', '2021-01-02', '2023-01-02', 'WALG agrees to pay for
the drug at marginal cost & plan to sell at markup .20', 32);

10
insert into contract values(3, 'RITE', 'MK', '2021-01-03', '2024-01-03', 'RITE agrees to pay for
the drug at marginal cost & plan to sell at markup .20', 33);

-- Select from individual tables


select * from physician;
select * from patient;
select * from pharmacompany;
select * from drug;
select * from inventory;
select * from pharmacy;
select * from prescription;
select * from pharmacysupervisor;
select * from contract;

-- Select 5 interesting queries


-- 1
select drugprescribed, COUNT(drugprescribed) as Times_Prescribed
from prescription
group by drugprescribed
order by Times_Prescribed DESC
limit 1;

-- 2
select pat.name as Non_Primary_Prescriptions from patient pat, prescription pres
where pat.primaryphysician != pres.prescriber;

11

You might also like