cst363 Schema
cst363 Schema
CREATE DATABASE IF NOT EXISTS `cst363` /*!40100 DEFAULT CHARACTER SET utf8mb3
*/ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `cst363`;
-- MySQL dump 10.13 Distrib 8.0.31, for Win64 (x86_64)
--
-- Host: localhost Database: cst363
-- ------------------------------------------------------
-- Server version 8.0.31
--
-- Table structure for table `company`
--
--
-- Dumping data for table `company`
--
--
-- Table structure for table `companymakesdrug`
--
--
-- Dumping data for table `companymakesdrug`
--
--
-- Table structure for table `contract`
--
--
-- Dumping data for table `contract`
--
--
-- Table structure for table `doctor`
--
--
-- Dumping data for table `doctor`
--
LOCK TABLES `doctor` WRITE;
/*!40000 ALTER TABLE `doctor` DISABLE KEYS */;
INSERT INTO `doctor` VALUES (1,'839935448','Liam','Johnson','Family
Medicine',2019),(2,'464536858','Olivia','Williams','Pediatrics',2004),(3,'792675159','Noah','Jones'
,'Orthpedics',2012),(4,'803870099','Ava','Brown','Dermatology',2002),(5,'294934823','Isabella','D
avis','Cardiology',2005),(6,'622605140','Sophia','Garcia','Gynecology',2002),(7,'636196541','Jac
kson','Rodriguez','Gastroenterology',2005),(8,'318095042','Mia','Martinez','Psychiatry',2001),(9,'
202398369','Aiden','Hernandez','Oncology',2007),(10,'211962163','Charlotte','Lopez','Internal
Medicine',2019);
/*!40000 ALTER TABLE `doctor` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `drug`
--
--
-- Dumping data for table `drug`
--
--
-- Table structure for table `fill`
--
--
-- Dumping data for table `fill`
--
--
-- Table structure for table `patient`
--
--
-- Dumping data for table `patient`
--
--
-- Table structure for table `pharmacy`
--
--
-- Dumping data for table `pharmacy`
--
--
-- Table structure for table `pharmacydrug`
--
--
-- Dumping data for table `pharmacydrug`
--
--
-- Table structure for table `prescription`
--
--
-- Dumping data for table `prescription`
--
-- Select all of the expired contract ids that a pharmacy had with any pharmaceutical company.
SELECT contractId FROM contract c, pharmacy p
WHERE c.endDate < CURDATE()
AND c.pharmacyId = p.pharmacyId AND p.pharmacyName = 'targetPharmacyName';
-- Select all of the distinct drugs by tradeName that a specific doctor (by SSN) has written
prescriptions for.
SELECT DISTINCT tradeName
FROM prescription p, doctor doc, drug d
WHERE p.doctorId = doc.doctorId
AND p.drugId = d.drugId
AND doc.doctorSSN = 'targetSSN'
AND d.tradeName IS NOT NULL;
-- Select all of the distinct generic drug names that at least one patient has been prescribed.
SELECT DISTINCT genericName
FROM drug
WHERE drugId IN
(SELECT drugId
FROM prescription
WHERE rxNum IS NOT NULL);
-- Select the pharmacy name(s) that sell a specific drug by generic name with the lowest price.
SELECT DISTINCT p.pharmacyName
FROM pharmacy p
JOIN pharmacyDrug pd on p.pharmacyId = pd.pharmacyId
JOIN drug d ON pd.drugId = d.drugId
WHERE genericName = 'targetDrugGenericName'
AND price = (
SELECT MIN(price)
FROM pharmacyDrug pd, drug d
WHERE pd.drugId = d.drugId
AND genericName = 'targetDrugGenericName'
);
-- Select the average age of patients that have prescriptions filled through a specific pharmacy
SELECT AVG(YEAR(CURDATE())-YEAR(p.patientBirthdate)) as "Average Age"
FROM prescription AS pr
JOIN patient AS p ON pr.patientId = p.patientId
JOIN fill AS f ON f.rxNum = pr.rxNum
JOIN pharmacy AS ph ON f.pharmacyID = ph.pharmacyId
WHERE ph.pharmacyName = 'targetPharmacyName';