Momen Zalabany
Momen Zalabany

Reputation: 9007

Too relation or not to relation ? A MySQL, PHP database workflow

im kinda new with mysql and i'm trying to create a kind complex database and need some help.

My db structure

 Tables(columns)

 1.patients (Id,name,dob,etc....)
 2.visits (Id,doctor,clinic,Patient_id,etc....)
 3.prescription (Id,visit_id,drug_name,dose,tdi,etc....)
 4.payments (id,doctor_id,clinic_id,patient_id,amount,etc...) etc..

I have about 9 tables, all of them the primary key is 'id' and its set to autoinc.

i dont use relations in my db (cuz i dont know if it would be better or not ! and i never got really deep into mysql , so i just use php to run query's to Fitch info from one table and use that to run another query to get more info/store etc..)

for example: if i want to view all drugs i gave to one of my patients, for example his id is :100

1-click patient name (name link generated from (tbl:patients,column:id))
2-search tbl visits WHERE patient_id=='100' ; ---> that return all his visits ($x array)
3-loop prescription tbl searching for drugs with matching visit_id with $x (loop array).
4- return all rows found.

as my database expanding more and more (1k+ record in visit table) so 1 patient can have more than 40 visit that's 40 loop into prescription table to get all his previous prescription.

so i came up with small teak where i edited my db so that patient_id and visit_id is a column in nearly all tables so i can skip step 2 and 3 into one step ( search prescription tbl WHERE patient_id=100), but that left me with so many duplicates in my db,and i feel its kinda stupid way to do it !!

should i start considering using relational database ?

if so can some one explain a bit how this will ease my life ?

can i do this redesign but altering current tables or i must recreate all tables ?

thank you very much

Upvotes: 0

Views: 399

Answers (1)

O. Jones
O. Jones

Reputation: 108839

Yes, you should exploit MySQL's relational database capabilities. They will make your life much easier as this project scales up.

Actually you're already using them well. You've discovered that patients can have zero or more visits, for example. What you need to do now is learn to use JOIN queries to MySQL.

Once you know how to use JOIN, you may want to declare some foreign keys and other database constraints. But your system will work OK without them.

You have already decided to denormalize your database by including both patient_id and visit_id in nearly all tables. Denormalization is the adding of data that's formally redundant to various tables. It's usually done for performance reasons. This may or may not be a wise decision as your system scales up. But I think you can trust your instinct about the need for the denormalization you have chosen. Read up on "database normalization" to get some background.

One little bit of advice: Don't use columns named simply "id". Name columns the same in every table. For example, use patients.patient_id, visits.patient_id, and so forth. This is because there are a bunch of automated software engineering tools that help you understand the relationships in your database. If your ID columns are named consistently these tools work better.

So, here's an example about how to do the steps numbered 2 and 3 in your question with a single JOIN query.

           SELECT p.patient_id p.name, v.visit_id, rx.drug_name, rx.drug_dose
            FROM patients     AS p
       LEFT JOIN visits       AS v    ON p.patient_id = v.patient_id
       LEFT JOIN prescription AS rx   ON v.visit_id = rx.visit_id
           WHERE p.patient_id = '100'
        ORDER BY p.patient_id, v.visit_id, rx.prescription_id

Like all SQL queries, this returns a virtual table of rows and columns. In this case each row of your virtual table has patient, visit, and drug data. I used LEFT JOIN in this example. That means that a patient with no visits will have a row with NULL data in it. If you specify JOIN MySQL will omit those patients from the virtual table.

Upvotes: 1

Related Questions