Assignment 3 Report
Assignment 3 Report
Additionally, The database will track pharmacies and contracts that pharmacies will
have with pharmaceutical companies from which drugs will be purcheds. Each
pharmacy will have a supervisor that will be responsible for the contracts that a
pharmacy has with the pharmaceutical company.
This database consists of 10 tables with 8 strong and 2 weak relations. The information
below explains how each table will relate to one another.
Relational Schema
-- MySQL Script generated by MySQL Workbench
-- Tue May 17 21:52:02 2022
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `physician`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `physician` (
`ssn` INT NOT NULL,
`name` VARCHAR(45) NULL,
`specialty` VARCHAR(45) NULL,
`exp_years` INT NULL,
PRIMARY KEY (`ssn`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `patient` (
`ssn` INT NOT NULL,
`name` VARCHAR(45) NULL,
`age` INT NULL,
`address` VARCHAR(45) NULL,
`pcp_ssn` INT NOT NULL,
PRIMARY KEY (`ssn`),
INDEX `fk_patient_physician1_idx` (`pcp_ssn` ASC) VISIBLE,
CONSTRAINT `fk_patient_physician1`
FOREIGN KEY (`pcp_ssn`)
REFERENCES `physician` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `pharm_co`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pharm_co` (
`name` VARCHAR(45) NOT NULL,
`phone` VARCHAR(45) NULL,
PRIMARY KEY (`name`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `drug`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `drug` (
`generic_name` VARCHAR(45) NOT NULL,
`trade_name` VARCHAR(45) NULL,
`pharm_co_name` VARCHAR(45) NOT NULL,
INDEX `fk_drug_pharm_co1_idx` (`pharm_co_name` ASC) VISIBLE,
UNIQUE INDEX `trade_name_UNIQUE` (`trade_name` ASC) VISIBLE,
PRIMARY KEY (`generic_name`),
CONSTRAINT `fk_drug_pharm_co1`
FOREIGN KEY (`pharm_co_name`)
REFERENCES `pharm_co` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `pharmacy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pharmacy` (
`name` VARCHAR(45) NOT NULL,
`address` VARCHAR(45) NULL,
`phone` VARCHAR(45) NULL,
PRIMARY KEY (`name`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `prescription`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `prescription` (
`physician_ssn` INT NOT NULL,
`patient_ssn` INT NOT NULL,
`drug` VARCHAR(45) NOT NULL,
`rx_number` INT NOT NULL AUTO_INCREMENT,
`date` DATE NULL,
`quantity` INT NULL,
`pharmacy_name` VARCHAR(45) NOT NULL,
`fill_date` DATE NULL,
PRIMARY KEY (`rx_number`),
INDEX `fk_physician_has_patient_patient1_idx` (`patient_ssn`
ASC) VISIBLE,
INDEX `fk_physician_has_patient_physician1_idx`
(`physician_ssn` ASC) VISIBLE,
INDEX `fk_prescription_drug1_idx` (`drug` ASC) VISIBLE,
INDEX `fk_prescription_pharmacy1_idx` (`pharmacy_name` ASC)
VISIBLE,
CONSTRAINT `fk_physician_has_patient_physician1`
FOREIGN KEY (`physician_ssn`)
REFERENCES `physician` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_physician_has_patient_patient1`
FOREIGN KEY (`patient_ssn`)
REFERENCES `patient` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_prescription_drug1`
FOREIGN KEY (`drug`)
REFERENCES `drug` (`generic_name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_prescription_pharmacy1`
FOREIGN KEY (`pharmacy_name`)
REFERENCES `pharmacy` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `retail_sale`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `retail_sale` (
`pharmacy_name` VARCHAR(45) NOT NULL,
`drug_generic_name` VARCHAR(45) NOT NULL,
`price` DECIMAL(10,2) NULL,
PRIMARY KEY (`pharmacy_name`, `drug_generic_name`),
INDEX `fk_pharmacy_has_drug_drug1_idx` (`drug_generic_name`
ASC) VISIBLE,
INDEX `fk_pharmacy_has_drug_pharmacy1_idx` (`pharmacy_name`
ASC) VISIBLE,
CONSTRAINT `fk_pharmacy_has_drug_pharmacy1`
FOREIGN KEY (`pharmacy_name`)
REFERENCES `pharmacy` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_pharmacy_has_drug_drug1`
FOREIGN KEY (`drug_generic_name`)
REFERENCES `drug` (`generic_name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `doctors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `doctors` (
`physician_ssn` INT NOT NULL,
`patient_ssn` INT NOT NULL,
PRIMARY KEY (`physician_ssn`, `patient_ssn`),
INDEX `fk_physician_has_patient_patient2_idx` (`patient_ssn`
ASC) VISIBLE,
INDEX `fk_physician_has_patient_physician2_idx`
(`physician_ssn` ASC) VISIBLE,
CONSTRAINT `fk_physician_has_patient_physician2`
FOREIGN KEY (`physician_ssn`)
REFERENCES `physician` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_physician_has_patient_patient2`
FOREIGN KEY (`patient_ssn`)
REFERENCES `patient` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `supervisor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `supervisor` (
`pharmacy_name` VARCHAR(45) NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`pharmacy_name`),
INDEX `fk_pharmacy_has_contracts_pharmacy1_idx`
(`pharmacy_name` ASC) VISIBLE,
CONSTRAINT `fk_pharmacy_has_contracts_pharmacy1`
FOREIGN KEY (`pharmacy_name`)
REFERENCES `pharmacy` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `contract` (
`pharm_co_name` VARCHAR(45) NOT NULL,
`start_date` DATE NULL,
`end_date` DATE NULL,
`terms` VARCHAR(255) NULL,
`supervisor_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`pharm_co_name`),
INDEX `fk_pharm_co_has_pharmacy_pharm_co1_idx`
(`pharm_co_name` ASC) VISIBLE,
INDEX `fk_contracts_supervisor1_idx` (`supervisor_name` ASC)
VISIBLE,
CONSTRAINT `fk_pharm_co_has_pharmacy_pharm_co1`
FOREIGN KEY (`pharm_co_name`)
REFERENCES `pharm_co` (`name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_contracts_supervisor1`
FOREIGN KEY (`supervisor_name`)
REFERENCES `supervisor` (`pharmacy_name`)
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;
Additional constraints:
● A physician cannot prescribe a medication to a patient that he is not a doctor for.
● Two physicians must not be able to prescribe the same drug to a single patient.
The concept of normalization is addressed with the top down design and rows of each
table are unique. One exception was made to the patient's primary PCP. This has the
information of physician ssn and would be a duplicate in the doctor table.
Question of Interest
1. Find out the name of the primary physician for all patients in the order of patients’
names.
select patient.name
from patient, doctors d
where patient.name = d.name
union
select physician.name
from physician, doctors d
where physician.name = d.name
order by name;
2. Based on the generic name, list drug price from low to high.
select distinct generic_name, price
from drug, retail_sale rs
where drug.generic_name = rs.generic_name
order by price;
3. List out all the drugs each pharmacy is selling in the order by the name of the
pharmacy.
select name
from pharmacy, retail_sale rs
where pharmacy.name = rs.name
union
select trade_name
from drug, retail_sale rs
where drug.trade_name = rs.trade_name
order by name;
4. List all the prescriptions that cost equal or more than 10 dollars.
select generic_name as name, price as price
from drug
group by generic_name
having count(distinct (price) ) >=10
order by generic_name;
5. Find out the name of the physician who prescribed a medication for a patient.
select ssn, rx_number
from physician, prescription p
where physician.ssn = p.ssn
union
select ssn, rx_number
from patient, prescription p
where patient.ssn = p.ssn
order by ssn;
Conclusion
In this project, we are basing the specifications off of the prompt that was given in the
class. The process was fairly simple. We read over the specifications on the relationship
between patient, doctor, physician, pharmacy, company, prescription, drug, supervisor,
and contracts. While we figure out the relationship between each party, we started to
think about questions of interest on what would be interesting to bring up for this design.
One thing that we learned from this project is that the database is not as simple and
straightforward as it looks like. Some of the business-related databases in the real world
could be very complicated depending on what kind of information is included.
There might be some open questions that we will face in the part 2 of this assignment,
including some of the loopholes that might come up in the alternatives that we design
during part 1. At the time that we started the insert statements, there could be some
other questions too depending on how we design it. We’re excited to see part 2 coming
in the following week and how we can create this project together.