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

R2T Consulting Company

Assignment 3 Project Report

CST363: Intro to Database Systems


May 2022

Angela Cheng, Denis Mulalic


Introduction:
This is a database that was designed to track patient prescriptions. It is capable of
tracking which physician prescribes the prescription, the quantity of the drug, when the
prescription was filled, at what pharmacy the prescription was filled, how much the drug
costs, and what pharmaceutical company manufactured the drug. It is designed to allow
for patients to have multiple physicians and for physicians to have multiple patients.

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.

● The database will keep track of physicians, patents, prescriptions, pharmacies,


pharmacy contract supervisors, contracts, drugs, and pharmaceutical companies.
These will be strong relationships.
● Each physician will have ssn (Social Security Number), name, specialty and
years of experience.
● Each prescription will have a physician social security number, patient social
security number, drug, Rx number, date, quantity, pharmacy name, and fill date.
● Each drug will have a generic name, trade name, and pharmaceutical company
name.
● Each patient will have one PCP (primary care physician), SSN (social security
number), name (full name), age, and address.
● Each pharmacy will have a name, address, and phone number.
● Each supervisor will have a pharmacy name and name.
● Each pharmaceutical company will have a name and a phone number.
● Each patient can have multiple doctors and a doctor can only have one patient,
but a physician can be a doctor to many patients. Doctor table will hold all the
physicians for each patient. One of the doctors for a patient will be a patient's
PCP and will also be in the patients table.
● Each contract will have only one supervisor, but a supervisor can have multiple
contracts. A pharmaceutical company can have multiple contracts.
● Each pharmacy will have multiple drugs it sells. The table retail_sale will hold the
price of each drug.
ER Model

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

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

Check your design for normalization:


Normalization is the process of organizing data in a database, which includes creating
tables and setting up the relationships between those tables.

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.

You might also like