Professional Documents
Culture Documents
Team 03
Team 03
The purpose of this database is to manage all of the patient, doctor, pharmacy, and drug
information as they relate to each other. For patients, we will create a separate table to collect
their information such as: date of birth, social security number, address, and contact information.
Similarly, a separate table is created for doctors to collect their information. Pharmacy, drug
prices, and generic information of the drug will also have their own tables. We will be creating
tables for each entity as required and those tables are detailed below. Certain tables like the
Price table are entirely dependent on other tables and will be created out of necessity.
The benefits of designing a database for the purposes outlined below are that it is an easier way
to keep track of each patient and doctor’s information. With this database, it will be fairly simple
to answer complex questions by sorting and filtering important information based on the
database records.
The requirements of the database are listed below. The requirements are used to determine
which tables are created and how they are linked to other tables in the database.
1. Patients have uniquely identifying information which are social security number, date of
birth, full name, and address. This information is used to collect and record necessary
data about a patient. Medical professionals and insurance companies use this
information to help their patients and update their records.
2. Doctors have similar identifying information to patients such as: social security numbers,
expertise in specific fields, length of experience, and education. Most doctors have
biographical information on the clinic or hospital websites that they work with for public
and patient information.
3. Pharmaceutical companies have identifying information such as: address and contact
information. These companies have contracts with local pharmacies to provide them with
drugs, which their customers need and use.
4. Prescription drugs are only obtainable by prescription from a certified doctor. Prescription
drugs are manufactured and sold by pharmaceutical companies to retail pharmacies.
These drugs have a generic formula name and may also have a trade name. Trade
names must be unique, if a drug is to have one.
5. Pharmacies have name and contact information such as: website, email address, phone
number, and mailing address. These pharmacies must be registered with the local
governments, they must also be certified to practice and distribute drugs to customers
with a valid prescription.
6. Most patients have a primary physician that they go to for their primary care. These
physicians provide their patients with certified treatment in the field that they are certified
in and if they are not certified in a certain treatment, patients are referred to another
expert or certified physician that is an expert in that field.
7. Physicians can write prescriptions for any patient, this is a many to many relationship in
the SQL model. Of course, there are restrictions for physicians to only write prescriptions
in their own field and there are some government restrictions on what drug that can be
prescribed.
8. Each pharmacy sells several drugs and has a price for each. A drug could be sold at
several pharmacies and the price could vary from one pharmacy to another. Also, the
same drug can be manufactured by different pharmaceutical companies and may have
different prices at various pharmacies.
9. Doctors prescribe drugs to many patients. A prescription has a unique Rx number. This
number pertains to one drug, one patient, and is written by one doctor. A patient can
have multiple prescriptions from multiple doctors for multiple drugs.
10. Each prescription has a date and quantity associated with it. If the prescription has a
trade name, then the prescription is for a specific drug from a specific pharmaceutical
company. If the prescription is for a generic name, then any drug with that formula name
from any pharmaceutical company can be used.
11. When a prescription is filled, we want to track the pharmacy that filled it and the date that
it was filled. If it was for a generic name, we want the pharmaceutical company that
supplied the drug used to fill the prescription.
13. Pharmacies appoint a supervisor for each contract. Every contract has a supervisor that
can change over time. A supervisor may be a supervisor for multiple contracts. These
pharmacies must compete with its competitors to provide better quality and prices. Also,
pharmacies must comply with government regulations.
Database Structure
The database consists of 8 tables as shown below. More detailed descriptions of each table
will follow.
Table Description
Table explanations:
Doctor
The Doctor table includes docid as the primary key and the docid will auto increment. It also
includes the doctor’s name, specialty, license start date, and SSN. The Doctor table is
connected to both the Patient and Prescription tables. In this database, it will be possible for a
doctor to have zero to many patients. In the same way, it will be possible for a doctor to have
zero to many prescriptions written. However, it is only possible for a patient to have one
doctor. Similarly, it is only possible for a prescription to have one doctor.
Patient
The Patient table includes the patientid as the primary key which will auto increment. The
table will also include the patient’s name, dob, address, and SSN. The foreign key linking the
Patient table to the Doctor table will be the docid. As stated, a patient can only have one
doctor. The Patient table is also connected to the Prescription table as the patient will have
one to many prescriptions.
Prescription
The Prescription table will use the Rx number as its primary key to identify the prescription.
The database will automatically assign an integer value to a prescription record when it is
created. The table will also consist of the quantity, date, drug_id. The drug_id is the first
foreign key of this table and comes from the Drug table. The next foreign keys this table uses
are the doctor and patient table’s primary keys. This is because a prescription is required to
have one doctor and one patient. When a prescription is filled, the pharmacyid and fill_date,
which begin with null values, will automatically populate.
Drug
This table’s job is to contain all of the drugs in the database. The drug_id will act as the
auto-incremented primary key. The table will also include the trade name and generic name.
The Drug table is connected to the PharmCompany table and uses the company’s name as
the foreign key which is not required. The table is also connected to the Price table because
pharmacies are able to have prices that differ from pharmacy to pharmacy.
PharmCompany
The PharmCompany table contains the least amount of columns of all the tables. The primary
key which serves for identification is the pharmaceutical company’s name. The table will also
include a column for a phone number. As stated above, the PharmCompany table is
connected to the PrescriptionDrug table and the pharmaceutical company is allowed to have
one to many prescription drugs. A Drug can have zero to one PharmaceuticalCompanies.
The PharmCompany table is also connected to the Contract table which serves as a way to
manage all of the contracts between pharmaceutical companies and pharmacies.
Contract
As stated above, this table will keep track of all the contracts between the pharmaceutical
companies and pharmacies. A pharmaceutical company can have one to many contracts.
This is the same case for the pharmacies. The Contract table consists of a contract number
which will serve as the primary key. It also houses the contract start date, end date (if the
contract is over), text of the contract, and the name of the supervisor assigned to the contract.
Since the Contract table relies on the pharmaceutical companies and pharmacies, the foreign
keys include the pharmaceutical company’s name and the pharmacy’s name which are the
primary keys of the two tables respectively.
Pharmacy
The Pharmacy table consists of the pharmacy’s name as the primary key. The table also
contains the address and phone number of the pharmacy. As mentioned above, the
Pharmacy table is connected to both the Contract and Price tables. A pharmacy is allowed to
have zero to many contracts just in case a pharmacy is new and does not have any contracts
yet. It is possible for the pharmacy to have one to many prices, as it is expected that the
pharmacy will be selling at least one prescription drug.
Price
The price table is entirely dependent upon the PrescriptionDrug and Pharmacy tables. In fact,
the Price table uses the primary keys of both tables as its own primary key. Those columns
include the pharmacy name from the pharmacy and the generic name of the prescription
drug. In addition to the primary key, the price table also includes the price for the drug. As
stated above, it is possible for a pharmacy to have one to many prices. Similarly, it is possible
for a prescription drug to have one to many prices, because prices may differ among
pharmacies.
NOTE: All names have a maximum character count of 35. Address components have varying
maximum character counts for street, city, state, and zip code. Phone numbers have a
maximum of 10, and social security numbers (SSN) have a maximum of 9. Therefore, dashes
should not be included in phone numbers and SSN numbers.
Create File
-- MySQL Script generated by MySQL Workbench
-- Sat May 21 08:38:03 2022
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
-- Table `Doctor`
CREATE TABLE IF NOT EXISTS `Doctor` (
`docid` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(35) NOT NULL,
`last_name` VARCHAR(35) NOT NULL,
`specialty` VARCHAR(35) NOT NULL,
`firstyear` CHAR(4) NOT NULL,
`doctorssn` CHAR(9) NOT NULL,
PRIMARY KEY (`docid`));
-- Table `Patient`
CREATE TABLE IF NOT EXISTS `Patient` (
`patientid` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(35) NOT NULL,
`last_name` VARCHAR(35) NOT NULL,
`dob` DATE NOT NULL,
`street` VARCHAR(50) NOT NULL,
`city` VARCHAR(45) NOT NULL,
`state` CHAR(2) NOT NULL,
`zipcode` VARCHAR(9) NOT NULL,
`patientssn` CHAR(9) NOT NULL,
`docid` INT NOT NULL,
PRIMARY KEY (`patientid`),
INDEX `fk_Patient_Doctor_idx` (`docid` ASC) VISIBLE,
CONSTRAINT `fk_Patient_Doctor`
FOREIGN KEY (`docid`)
REFERENCES `Doctor` (`docid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
-- Table `PharmCompany`
CREATE TABLE IF NOT EXISTS `PharmCompany` (
`companyname` VARCHAR(35) NOT NULL,
`phonenumber` CHAR(10) NOT NULL,
PRIMARY KEY (`companyname`));
-- Table `Drug`
CREATE TABLE IF NOT EXISTS `Drug` (
`drug_id` INT NOT NULL AUTO_INCREMENT,
`tradename` VARCHAR(100) NULL,
`genericname` VARCHAR(200) NULL,
`companyname` VARCHAR(35) NULL,
PRIMARY KEY (`drug_id`),
INDEX `fk_PrescriptionDrug_PharmCompany1_idx` (`companyname` ASC) VISIBLE,
CONSTRAINT `fk_PrescriptionDrug_PharmCompany1`
FOREIGN KEY (`companyname`)
REFERENCES `PharmCompany` (`companyname`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
-- Table `Pharmacy`
CREATE TABLE IF NOT EXISTS `Pharmacy` (
`pharmacyid` INT NOT NULL AUTO_INCREMENT,
`pharmacyname` VARCHAR(35) NOT NULL,
`address` VARCHAR(100) NOT NULL,
`phonenumber` CHAR(10) NULL,
PRIMARY KEY (`pharmacyid`));
-- Table `Price`
CREATE TABLE IF NOT EXISTS `Price` (
`pharmacyid` INT NOT NULL,
`drug_id` INT NOT NULL,
`price` DECIMAL(5,2) NOT NULL,
PRIMARY KEY (`pharmacyid`, `drug_id`),
INDEX `fk_PrescriptionDrug_has_Pharmacy_Pharmacy1_idx` (`pharmacyid` ASC) VISIBLE,
INDEX `fk_PrescriptionDrug_has_Pharmacy_PrescriptionDrug1_idx` (`drug_id` ASC)
VISIBLE,
CONSTRAINT `fk_PrescriptionDrug_has_Pharmacy_PrescriptionDrug1`
FOREIGN KEY (`drug_id`)
REFERENCES `Drug` (`drug_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PrescriptionDrug_has_Pharmacy_Pharmacy1`
FOREIGN KEY (`pharmacyid`)
REFERENCES `Pharmacy` (`pharmacyid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
-- Table `Prescription`
CREATE TABLE IF NOT EXISTS `Prescription` (
`rx` INT NOT NULL AUTO_INCREMENT,
`quantity` INT NOT NULL,
`date` DATE NOT NULL,
`drug_id` INT NOT NULL,
`docid` INT NOT NULL,
`patientid` INT NOT NULL,
`pharmacyid` INT NULL,
`fill_date` DATE NULL,
PRIMARY KEY (`rx`),
INDEX `fk_Prescription_PrescriptionDrug1_idx` (`drug_id` ASC) VISIBLE,
INDEX `fk_Prescription_Doctor1_idx` (`docid` ASC) VISIBLE,
INDEX `fk_Prescription_Patient1_idx` (`patientid` ASC) VISIBLE,
INDEX `fk_Prescription_Pharmacy1_idx` (`pharmacyid` ASC) VISIBLE,
CONSTRAINT `fk_Prescription_PrescriptionDrug1`
FOREIGN KEY (`drug_id`)
REFERENCES `Drug` (`drug_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Doctor1`
FOREIGN KEY (`docid`)
REFERENCES `Doctor` (`docid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Patient1`
FOREIGN KEY (`patientid`)
REFERENCES `Patient` (`patientid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Prescription_Pharmacy1`
FOREIGN KEY (`pharmacyid`)
REFERENCES `Pharmacy` (`pharmacyid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
-- Table `Contract`
CREATE TABLE IF NOT EXISTS `Contract` (
`number` INT NOT NULL AUTO_INCREMENT,
`startdate` DATE NOT NULL,
`enddate` DATE NULL,
`text` VARCHAR(100) NOT NULL,
`supervisor` VARCHAR(35) NOT NULL,
`companyname` VARCHAR(35) NOT NULL,
`pharmacyid` INT NOT NULL,
PRIMARY KEY (`number`),
INDEX `fk_Contract_PharmCompany1_idx` (`companyname` ASC) VISIBLE,
INDEX `fk_Contract_Pharmacy1_idx` (`pharmacyid` ASC) VISIBLE,
CONSTRAINT `fk_Contract_PharmCompany1`
FOREIGN KEY (`companyname`)
REFERENCES `PharmCompany` (`companyname`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Contract_Pharmacy1`
FOREIGN KEY (`pharmacyid`)
REFERENCES `Pharmacy` (`pharmacyid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
2. Display the pharmacy name, generic name, and price of all drugs from pharmacies
with the phone number beginning with ‘949’.
select ph.pharmacyname, pd.genericname, price
from prescriptiondrug pd join price p on pd.genericname = p.genericname
join pharmacy ph on ph.pharmacyname = p.pharmacyname
where phonenumber like '949%';
3. Display the doctor SSN, specialty, and prescriptions which have a quantity of more
than 10.
select d.doctorssn, specialty, rx
from doctor d join prescription p on d.doctorssn = p.doctorssn
where quantity > 10;
4. Display the ongoing contracts and the pharmaceutical company and pharmacy they
belong to.
select number, pc.companyname name, p.pharmacyname
from pharmcompany pc join contract c on pc.companyname = c.companyname
join pharmacy p on c.pharmacyname = p.pharmacyname
where enddate is null;
5. Display the maximum price of the generic name drug of any pharmacy and list the
pharmacy name.
select ph.pharmacyname, max(price)
from pharmacy ph join price p on ph.pharmacyname = p.pharmacyname
join prescriptiondrug pd on pd.genericname = p.genericname
group by pharmacyname;
Example of Errors:
The web application used to create a prescription will allow doctors to create a
prescription for patients using personally identifying information.
The screenshots below show the process of writing a new prescription as a doctor,
and some error messages that appear if the data is not input correctly:
The web application used to fill prescriptions verifies that the rx exists in the database
and matches the patient’s last name, adds a fill date, and pharmacy name to the
record:
The screenshots below displays the process of a patient filling a prescription and
some errors that can occur:
The pharmacyid and fill date are automatically added to the database:
Normalization
In order to reduce the duplication of data in this database project, our team used the third
normal form (3NF) for our normalization process. We have confirmed that none of the
columns in our tables are dependent on anything other than the primary keys. With this
approach in our design of the database, we avoided data anomalies and ensured data
integrity, all while simplifying the management of the data in the database.
Summary
This is a group assignment and part one and two of a two week project. We worked on this
assignment as a team. So far we have learned the creation of relational tables, models,
normalization, updating tables, adding data to the tables, and modifying tables. This
assignment allowed us to implement what we have learned in CST 363 Introduction to
Database Systems. We are looking forward to what more this class has to offer.