Opensourcerers cst363 Project2 Report Final
Opensourcerers cst363 Project2 Report Final
Opensourcerers cst363 Project2 Report Final
Introduction
entity or business would utilize a database like the one we were creating. In our reasoning,
hospitals and medical groups would require databases that contained physician information,
patient information and other data such as prescriptions, procedures and insurance information.
A pharmaceutical company would utilize a database that contained supplies, drug formulas,
clinical trial information in their scientific development sectors, and databases of companies that
sell their products in their financial departments. Pharmacies, hospitals and medical groups who
sell or otherwise provide medications to patients would utilize databases that contain drug cost,
inventory levels, and patient health data including prescribing physician, dates of filled
contains blended information that could be useful to companies that track medication use or
health initiative to monitor the opioid epidemic, potentially tracking physicians who are
prescribing large quantities of harmful or addictive medication, or patients who could be filling
The database must contain patient and doctor information, both with an identifying social
security number and a name and more specific information for each. Each patient is assigned to
at least one doctor, and every doctor must have at least one patient. These doctors prescribe
prescriptions for their patients, with one or more drugs, with a date and quantity associated with
it. There must be tables to contain data for pharmacies, their name and location, the drugs they
3
sell and their cost, and patients who fill a prescription. When a prescription is filled, the date
should be tracked, and the pharmacy who filled it. The data for the pharmaceutical company
must contain a name, address and phone number, as well as formulas and a trade name as a
unique identifier. A long term contract binds the pharmaceutical companies with the pharmacies,
and information such as contract start and expiration dates, and the text of the contract. Each
contract must have a supervisor assigned by the pharmacy, but the supervisor may change over
time.
The patients table contains fields for a primary key social security number, and fields for
name, age and address, and we decided the name should be non nullable. The doctors table has a
primary key social security number and a non nullable name field. We allowed the specialty to be
a nullable field, with the understanding that this field would be null if the physician was a
general doctor and did not have a specialty. The years of experience was made nullable also, in
case this information was unknown at the time the physician was entered into the database. The
patients and doctors table are connected by the patients_has_doctors table, containing the
identifying social security number from each side in a many to many relationship, since patients
can have more than one doctor and doctors see many patients.
The pharmacies table contains an auto generated id number and fields for name, address
and phone number, and is related to the prescriptions table by the foreign key
filling_pharmacy_id, in a one to many relationship since a pharmacy will interact with multiple
prescriptions and a prescription will be filled by only one pharmacy. We made the name field non
nullable, but left the address and phone number nullable. The pharmacy table also relates to the
drugs table in a many to many relationship since one company manufactures many drugs and a
4
generic formula could be used by many companies to produce a drug with a specific trade name.
The drugs table has a trade name field which also serves as the primary key, as well as a foreign
key formula_id from the formulas table. We gave formulas its own table since a generic drug
could be manufactured by different companies using the same formula which may have the same
active ingredient but different concentrations.The pharmacies table and drugs table are related by
a pharmacies_has_drugs table which contains foreign keys pharmacies_id and drugs_id as well
The contracts table uses the id numbers from the pharma_companies and pharmacies
table as foreign keys, along with the auto generated primary key from the supervisors table,
supervisors_employee_id. The contracts table has a one to many relationship with the
pharma_companies since one company could have many contracts, but may have no current
pharmacies_has_supervisors tables which contains the identifier id number from both tables in a
one to many relationship since a supervisor will work for one pharmacy but a pharmacy may
Schema
Below is our SQL script for building the full schema, with constraints, keys and other
There was only one significant code constraint (outside of validation) to be implemented
in the front end application. As the database uses the RXID for the primary key for all
prescriptions, the application must check the database for existing prescriptions with the same
doctor, patient and drug combination. This is to enforce only one allowed prescription for each
drug per doctor/patient relation. If an existing relation is found, the application replaces the new
Normalization
There were a few steps to normalize our model, clearly all of our tables were in first
normalized form by default as we used the ER tool to design them. As it is possible for some of
our entities to share information and have partial dependencies, some steps were taken to
mitigate these issues. For example, different companies can produce the same drug formulas
under different trade names, so we seperated formulas from the drugs entity. This removes that
partial dependency and increases the normalization of the database. Although our tables are all in
second normal form, some are not in third normal form. This however is acceptable as there is
little to be gained from this additional normalization. Some transitive dependencies that exist for
example are the supervisor id and the supervisor name. Creating an additional relation would
Queries
1. Can I find the start date, end date, pharmacy name, drug name, and the pharma company of a
SELECT
contracts.start_date AS contract_start_date,
contracts.end_date AS contract_end_date,
pharmacies.name AS pharmacy_name,
drugs.trade_name AS drug_trade_name,
pharma_companies.name AS pharma_company_name
FROM contracts
2. Can I find the lowest price that Motrin is sold for across all of our pharmacy locations?
SELECT
pharmacies.name AS pharmacy_name,
drugs.trade_name AS drug_trade_name,
price
FROM pharmacies_has_drugs
) AS Motrins
WHERE price = (
SELECT
pharmacies.name AS pharmacy_name,
drugs.trade_name AS drug_trade_name,
price
11
FROM pharmacies_has_drugs
) AS Motrins
);
4. How many contracts are being supervised by our supervisor Jimmy Johns?
) AS new_table;
GROUP BY formulas.active_ingredient;
Screenshots
Invalid Data Entry for FDA Report FDA Report Error Page
16
Conclusion
We have learned how an ER diagram works and have executed creating our own ER
diagram. We logically made accurate the cardinality for relationships in our diagram and have
identified primary keys for all entities. We then mapped the ER diagram to relational database
schema. We wrote down the database schema as SQL create table statements. We then checked
our relational schema for normal form. After that, we documented any functional dependencies
17
that would indicate the need for normalization. We also thought of at least 5 questions about the
data that would be interesting to management of the drug store chain. We then wrote each of
them in SQL. We also created test data in our tables that demonstrated our SQL query was
working correctly.
After designing the database, our team wrote a relational schema derived from the ER
model. In the report we presented the schema in SQL as CREATE TABLE statements. We
included foreign key constraints and updated and deleted rules, “not null” or “null” column
properties, SQL check constraints, and any other constraints that cannot be implemented in SQL
and must be done by application code. We normalized the relational schema. We included our
SQL queries and included particular ones that express the queries in the report.
Our team learned a lot through the process of performing the work entailed in this
project. For example we were unaware how much more intuitive it would be to be able to
visually see the relationships created using the ER diagram. Having the boxes with the types of
relationships to look at made hammering it out, to make sense in the real world, a lot simpler. We
found that having the ER diagram to consult made it possible to scan over the entire database