Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

-- 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 ProjectPharmacy
-- -----------------------------------------------------

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

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Doctor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Doctor` (
`idDoctor` INT NOT NULL AUTO_INCREMENT,
`doctorFirstName` VARCHAR(45) NOT NULL,
`doctorLastName` VARCHAR(45) NOT NULL,
`specialty` VARCHAR(45) NOT NULL,
`practice_since` INT NOT NULL,
`doctorSSN` VARCHAR(11) NOT NULL,
PRIMARY KEY (`idDoctor`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Patient` (
`idPatient` INT NOT NULL AUTO_INCREMENT,
`patientFirstName` VARCHAR(45) NOT NULL,
`patientLastName` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
`patientSSN` VARCHAR(11) NOT NULL,
`patientStreet` VARCHAR(50) NOT NULL,
`patientCity` VARCHAR(25) NOT NULL,
`patientState` VARCHAR(2) NOT NULL,
`patientZip` VARCHAR(25) NOT NULL,
`idPrimaryDoctor` INT NOT NULL,
PRIMARY KEY (`idPatient`),
INDEX `fk_Patient_Doctor1_idx` (`idPrimaryDoctor` ASC) VISIBLE,
CONSTRAINT `fk_Patient_Doctor1`
FOREIGN KEY (`idPrimaryDoctor`)
REFERENCES `ProjectPharmacy`.`Doctor` (`idDoctor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Pharmacy` (
`idPharmacy` INT NOT NULL AUTO_INCREMENT,
`pharmName` VARCHAR(45) NOT NULL,
`pharmAddress` VARCHAR(100) NOT NULL,
`pharmPhoneNumber` VARCHAR(13) NOT NULL,
PRIMARY KEY (`idPharmacy`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Drug` (
`idDrug` INT(11) NOT NULL,
`tradeName` VARCHAR(100) NOT NULL,
`genericFormula` VARCHAR(200) NOT NULL,
PRIMARY KEY (`idDrug`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Prescription`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Prescription` (
`idPrescription` INT NOT NULL AUTO_INCREMENT,
`quantity` INT NOT NULL,
`prescriptionDate` DATE NOT NULL,
`dateFilled` DATE NULL,
`idDoctor` INT NOT NULL,
`idPatient` INT NOT NULL,
`idPharmacy` INT NULL,
`idDrug` INT NOT NULL,
PRIMARY KEY (`idPrescription`, `idPatient`, `idDrug`),
INDEX `fk_Prescription_Doctor1_idx` (`idDoctor` ASC) VISIBLE,
INDEX `fk_Prescription_Patient1_idx` (`idPatient` ASC) VISIBLE,
INDEX `fk_Prescription_Pharmacy1_idx` (`idPharmacy` ASC) VISIBLE,
INDEX `fk_Prescription_Drug1_idx` (`idDrug` ASC) VISIBLE,
CONSTRAINT `fk_Prescription_Doctor1`
FOREIGN KEY (`idDoctor`)
REFERENCES `ProjectPharmacy`.`Doctor` (`idDoctor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Patient1`
FOREIGN KEY (`idPatient`)
REFERENCES `ProjectPharmacy`.`Patient` (`idPatient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Pharmacy1`
FOREIGN KEY (`idPharmacy`)
REFERENCES `ProjectPharmacy`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Drug1`
FOREIGN KEY (`idDrug`)
REFERENCES `ProjectPharmacy`.`Drug` (`idDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`PharmaCompany`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`PharmaCompany` (
`idPharmaCompany` INT NOT NULL AUTO_INCREMENT,
`pharmaCompanyName` VARCHAR(45) NOT NULL,
`pharmaCompanyPhone` VARCHAR(13) NOT NULL,
PRIMARY KEY (`idPharmaCompany`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Supervisor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Supervisor` (
`idSupervisor` INT NOT NULL AUTO_INCREMENT,
`supervisorFirstName` VARCHAR(45) NOT NULL,
`supervisorLastName` VARCHAR(45) NOT NULL,
`supervisorPhone` VARCHAR(13) NOT NULL,
PRIMARY KEY (`idSupervisor`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Contract` (
`idContract` INT NOT NULL AUTO_INCREMENT,
`startDate` DATE NOT NULL,
`endDate` DATE NOT NULL,
`contractText` VARCHAR(45) NOT NULL,
`idPharmaCompany` INT NOT NULL,
`idSupervisor` INT NOT NULL,
`idPharmacy` INT NOT NULL,
PRIMARY KEY (`idContract`),
INDEX `fk_Contract_PharmaCompany1_idx` (`idPharmaCompany` ASC) VISIBLE,
INDEX `fk_Contract_Supervisor1_idx` (`idSupervisor` ASC) VISIBLE,
INDEX `fk_Contract_Pharmacy1_idx` (`idPharmacy` ASC) VISIBLE,
CONSTRAINT `fk_Contract_PharmaCompany1`
FOREIGN KEY (`idPharmaCompany`)
REFERENCES `ProjectPharmacy`.`PharmaCompany` (`idPharmaCompany`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_Supervisor1`
FOREIGN KEY (`idSupervisor`)
REFERENCES `ProjectPharmacy`.`Supervisor` (`idSupervisor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_Pharmacy1`
FOREIGN KEY (`idPharmacy`)
REFERENCES `ProjectPharmacy`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Sells`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Sells` (
`idDrug` INT NOT NULL,
`idPharmacy` INT NOT NULL,
`price` DECIMAL(7,2) NOT NULL,
PRIMARY KEY (`idDrug`, `idPharmacy`),
INDEX `fk_Drug_has_Pharmacy_Pharmacy1_idx` (`idPharmacy` ASC) VISIBLE,
INDEX `fk_Drug_has_Pharmacy_Drug1_idx` (`idDrug` ASC) VISIBLE,
CONSTRAINT `fk_Drug_has_Pharmacy_Drug1`
FOREIGN KEY (`idDrug`)
REFERENCES `ProjectPharmacy`.`Drug` (`idDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Drug_has_Pharmacy_Pharmacy1`
FOREIGN KEY (`idPharmacy`)
REFERENCES `ProjectPharmacy`.`Pharmacy` (`idPharmacy`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ProjectPharmacy`.`Drug_has_PharmaCompany`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ProjectPharmacy`.`Drug_has_PharmaCompany` (
`Drug_idDrug` INT(11) NOT NULL,
`PharmaCompany_idPharmaCompany` INT NOT NULL,
PRIMARY KEY (`Drug_idDrug`, `PharmaCompany_idPharmaCompany`),
INDEX `fk_Drug_has_PharmaCompany_PharmaCompany1_idx`
(`PharmaCompany_idPharmaCompany` ASC) VISIBLE,
INDEX `fk_Drug_has_PharmaCompany_Drug1_idx` (`Drug_idDrug` ASC) VISIBLE,
CONSTRAINT `fk_Drug_has_PharmaCompany_Drug1`
FOREIGN KEY (`Drug_idDrug`)
REFERENCES `ProjectPharmacy`.`Drug` (`idDrug`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Drug_has_PharmaCompany_PharmaCompany1`
FOREIGN KEY (`PharmaCompany_idPharmaCompany`)
REFERENCES `ProjectPharmacy`.`PharmaCompany` (`idPharmaCompany`)
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;

--
-- Table structure for table `drug`
--
-- DROP TABLE IF EXISTS `drug`;
-- CREATE TABLE `drug` (
-- `idDrug` int(11) NOT NULL,
-- `tradeName` varchar(100) NOT NULL,
-- `genericformula` varchar(200) NOT NULL,
-- PRIMARY KEY (`idDrug`)
-- );
INSERT INTO `drug` VALUES
(1,'Tylenol with Codeine','acetaminophen and codeine'),
(2,'Proair Proventil','albuterol aerosol'),
(3,'Accuneb','albuterol HFA'),
(4,'Fosamax','alendronate'),
(5,'Zyloprim','allopurinol'),
(6,'Xanax','alprazolam'),
(7,'Elavil','amitriptyline'),
(8,'Augmentin','amoxicillin and clavulanate K+'),
(9,'Amoxil','amoxicillin'),
(10,'Adderall XR','amphetamine and dextroamphetamine XR'),
(11,'Tenormin','atenolol'),
(12,'Lipitor','atorvastatin'),
(13,'Zithromax','azithromycin'),
(14,'Lotrel','benazepril and amlodipine'),
(15,'Soma','carisoprodol'),
(16,'Coreg','carvedilol'),
(17,'Omnicef','cefdinir'),
(18,'Celebrex','celecoxib'),
(19,'Keflex','cephalexin'),
(20,'Cipro','ciprofloxacin'),
(21,'Celexa','citalopram'),
(22,'Klonopin','clonazepam'),
(23,'Catapres','clonidine HCl'),
(24,'Plavix','clopidogrel'),
(25,'Premarin','conjugated estrogens'),
(26,'Flexeril','cyclobenzaprine'),
(27,'Valium','diazepam'),
(28,'Voltaren','diclofenac sodium'),
(29,'Yaz','drospirenone and ethinyl estradiol'),
(30,'Cymbalta','Duloxetine'),
(31,'Vibramycin','doxycycline hyclate'),
(32,'Vasotec','enalapril'),
(33,'Lexapro','escitalopram'),
(34,'Nexium','esomeprazole'),
(35,'Zetia','ezetimibe'),
(36,'Tricor','fenofibrate'),
(37,'Allegra','fexofenadine'),
(38,'Diflucan','fluconozole'),
(39,'Prozac','fluoxetine HCl'),
(40,'Advair','fluticasone and salmeterol inhaler'),
(41,'Flonase','fluticasone nasal spray'),
(42,'Folic Acid','folic acid'),
(43,'Lasix','furosemide'),
(44,'Neurontin','gabapentin'),
(45,'Amaryl','glimepiride'),
(46,'Diabeta','glyburide'),
(47,'Glucotrol','glipizide'),
(48,'Microzide','hydrochlorothiazide'),
(49,'Lortab','hydrocodone and acetaminophen'),
(50,'Motrin','ibuprophen'),
(51,'Lantus','insulin glargine'),
(52,'Imdur','isosorbide mononitrate'),
(53,'Prevacid','lansoprazole'),
(54,'Levaquin','levofloxacin'),
(55,'Levoxl','levothyroxine sodium'),
(56,'Zestoretic','lisinopril and hydrochlorothiazide'),
(57,'Prinivil','lisinopril'),
(58,'Ativan','lorazepam'),
(59,'Cozaar','losartan'),
(60,'Mevacor','lovastatin'),
(61,'Mobic','meloxicam'),
(62,'Glucophage','metformin HCl'),
(63,'Medrol','methylprednisone'),
(64,'Toprol','metoprolol succinate XL'),
(65,'Lopressor','metoprolol tartrate'),
(66,'Nasonex','mometasone'),
(67,'Singulair','montelukast'),
(68,'Naprosyn','naproxen'),
(69,'Prilosec','omeprazole'),
(70,'Percocet','oxycodone and acetaminophen'),
(71,'Protonix','pantoprazole'),
(72,'Paxil','paroxetine'),
(73,'Actos','pioglitazone'),
(74,'Klor-Con','potassium Chloride'),
(75,'Pravachol','pravastatin'),
(76,'Deltasone','prednisone'),
(77,'Lyrica','pregabalin'),
(78,'Phenergan','promethazine'),
(79,'Seroquel','quetiapine'),
(80,'Zantac','ranitidine'),
(81,'Crestor','rosuvastatin'),
(82,'Zoloft','sertraline HCl'),
(83,'Viagra','sildenafil HCl'),
(84,'Vytorin','simvastatin and ezetimibe'),
(85,'Zocor','simvastatin'),
(86,'Aldactone','spironolactone'),
(87,'Bactrim DS','sulfamethoxazole and trimethoprim DS'),
(88,'Flomax','tamsulosin'),
(89,'Restoril','temezepam'),
(90,'Topamax','topiramate'),
(91,'Ultram','tramadol'),
(92,'Aristocort','triamcinolone Ace topical'),
(93,'Desyrel','trazodone HCl'),
(94,'Dyazide','triamterene and hydrochlorothiazide'),
(95,'Valtrex','valaciclovir'),
(96,'Diovan','valsartan'),
(97,'Effexor XR','venlafaxine XR'),
(98,'Calan SR','verapamil SR'),
(99,'Ambien','zolpidem');

--
-- Table structure for table `pharmacy`
--
-- DROP TABLE IF EXISTS `pharmacy`;
-- CREATE TABLE `pharmacy` (
-- `idPharmacy` INT NOT NULL AUTO_INCREMENT,
-- `pharmName` VARCHAR(45) NOT NULL,
-- `pharmAddress` VARCHAR(100) NOT NULL,
-- `pharmPhoneNumber` VARCHAR(13) NOT NULL,
-- PRIMARY KEY (`idPharmacy`)
-- );

INSERT INTO `pharmacy` VALUES


(1,'CVS #12','83483 Reseda Blvd, Reseda, CA, 91406', '823-321-3234'),
(2,'CVS #7','77212 Vanowen St, Canoga Park, CA, 91335', '772-323-4542'),
(3,'Rite Aid #8','4332 Sherman St, Encino, CA, 93245', '818-365-9293'),
(4,'Med Zone Pharmacy','9283 Willow Ave, West Hills, CA, 92834', '818-546-7712'),
(5,'Walgreens #22','9238 Hollywood St, Hollywood, CA, 92832', '878-856-9182'),
(6,'Curtex Pharmacy','77123 Wayne Ave, Studio City, CA, 91822', '823-321-3234'),
(7,'Reseda Pharmacy','93843 Smile Ave, Reseda, CA, 91406', '373-323-545'),
(8,'Century Discount Pharmacy','8827 Louise Ave, Van Nuys, CA, 82832', '213-332-
9912'),
(9,'Albertsons Pharmacy','1112 Nordhoff, Northridge, CA, 91233', '818-444-5432'),
(10,'Savon Pharmacy','13651 Willard St, Panorama City, CA, 91402', '833-574-2273');

-- -- Project 1 and 2: DB Design queries


-- -- CST363: Intro to Database Systems
-- -- May 2022
-- -- By: Christian Vargas and Laurand Osmeni
-- -- This file is made up of five queries we used to validate the integrity of our
pharmacy database.

-- -- #1 Display the idPharmacy and corresponding count as "RxCount" for the


pharmacy that receives the most prescriptions orders excluding null values.
SELECT idPharmacy, MAX(presOrders) AS 'RxCount'
FROM (SELECT idPharmacy, COUNT(p.idPrescription) AS 'presOrders'
FROM Prescription p WHERE idPharmacy != "NULL" GROUP BY idPharmacy) AS
pharmacyCount;
-- -- This query let's pharmacy personnel know which specific pharmacy receives
-- -- the most prescriptions orders, which can mean this store needs to have more
staff to meet demand.

-- -- #2 Display patientfirstname, patientlastname, idPharmacy, and dateFilled


(prescription filled date)
-- -- for all patients that have filled their prescriptions at branch CVS #12.
SELECT p.patientfirstname, p.patientlastname, ph.idPharmacy, pres.dateFilled
FROM Patient p
INNER JOIN Prescription pres ON p.idPatient = pres.idPatient
INNER JOIN Pharmacy ph ON pres.idPharmacy = ph.idPharmacy
WHERE ph.pharmName IN ('CVS #12');
-- -- This query provides valuable store data in terms of prescriptions and can let
company owners
-- -- and providers know which stores are performing well and which stores are
underperforming.

-- -- #3 Display doctorFirstName, doctorLastName, and "NumOfPatients" for the


doctor with the most patients
-- -- born after a specific date, in this case, 2009-04-21. Order by
"NumOfPatients".
SELECT d.doctorFirstName, d.doctorLastName, COUNT(p.idPrimaryDoctor) AS
'NumOfPatients'
FROM Patient p, Doctor d
WHERE p.idPrimaryDoctor = d.idDoctor
AND p.birthdate > "2009-04-21" GROUP BY d.doctorFirstName
ORDER BY NumOfPatients DESC LIMIT 1;
-- -- This query could serve to crosscheck information on doctor specialties,
-- -- such as a doctor specializing in pediatrics should be prescribing more to
patients ages 18 and below.
-- -- Also, a pharmacy might be interested in knowing which doctor deals with
which population.

-- -- #4 Display the idDoctor, doctorFirstName, doctorLastName and


doctorYearsOfExperience,
-- -- and a count of prescription given as 'RxCount' if the doctor gave out
prescriptions in the year 2021.
-- -- Order by idDoctor.
SELECT pres.idDoctor, d.doctorFirstName, d.doctorLastName, d.practice_since,
COUNT(*) AS 'PrescriptionsGiven'
FROM prescription pres INNER JOIN doctor d ON pres.idDoctor = d.idDoctor
WHERE pres.prescriptionDate LIKE '2021%' GROUP BY pres.idDoctor;
-- -- This query can be useful to see if the number of prescriptions given is
correlated to the years of experience a doctor has.
-- -- Also, this query can help identify busy or prescription-happy doctors.

-- -- #5 Display patientZip as the zip code where the highest number of patients
reside as well
-- -- as the corresponding number of patients as ‘highestNumOfPatients’.
SELECT patientZip, MAX(patients) AS 'highestNumOfPatients'
FROM (SELECT patientZip, COUNT(p.idPatient) AS 'patients'
FROM Patient p GROUP BY patientZip) AS zipCount;
-- -- This query can be used by public health and pharmacy professionals to
understand
-- -- which areas have the highest need and therefore invest more resources in
those areas.

You might also like