How to "Relate" Tables

Paul Cooke

Registered User.
Local time
Today, 11:11
Joined
Oct 12, 2001
Messages
288
Hi I have read quite a few posts and googled loads after advice fron some of the forum users and am still confused (which is not hard :confused: )

I am in the process of designing a new database to keep records for patients (medical type!)

At the moment I am designing the tables and am somewhat confused as to how I would link these in the future?

The tables I will have are

Patient Details (First Name, Last Name, Date Of Birth, Address etc...)
Treatment Table (various fields relating to a treatment)
Consuambles used Table (induvidual items used to treat a patient and the quantities used, bandages plasters medications ect)
Vaccination Table (Various field relating to vaccinations)
Accident Report Form Table (various fields relating to a treatment that was need because of an accident)


What I hope to be able to do in the end is to "pull" up a form / report that will show all the treatments and consumables used for that treatment, Vaccinations and accidents a particular patient has had?

I really would appriciate any help anyone can give me on this but more so if they could explain it very basically

Cheers guys
 
Hi Mark, thanks very much for your reply I will take a look

Thanks again
Paul
 
Hi Mark, Thanks again for your reply. I have looked at the link and to be honest do not have a clue what I'm looking at !!

I not very good with Access but i really want to learn not only for the DB i'm trying to build now but also for the future.

Could you or anyone else explain this to me !!

Thanks again
 
Hi Paul, I get confused too. but hopefully might be able to point you in the right direction. I always find it easier to jot things down on a pice of paper first.

Looking at what you have said, i would propably have TblPatients and in that table would have a primary key PatientID, then name etc etc

I would then create a table TblTreatments and in that Tbl would have TreatmentID (PrimaryKey) and PatientID. In relationships I would have a one to many relationship between the two. Thus a patient can have many treatments. Once you have that you may find the rest follows.

Does that help?
 
Hi Charles,

Thanks for the reply I think I do actually understand a bit about what you've said (even I'm suprised !!)

So just to make sure...... in any relevent Table I would also put in a PatientID field and use this to link all the relevant data to that particular patient?

If that is correct how would I make sure the patientID entered was for a particular Patient? Or would it be a simple as (Hmmmm!!) forcing the users to search for a patient first to see if they exsist and then adding controls to a form to enable them to add information to thier particular record?

Does that make sense??

Thanks again

Paul
 
It sounds to me like you need to study a bit of Database Normalization before going too far.

Access likes tables to be normalized, at least 3rd normal form (which articles on normalization would explain.)

I've got to scoot but I'll come back later and post some ideas for you to ponder.
 
Thanks Docman, I did read a few articles that you pointed me towards from an earlier post but it was a bit confusing for my poor old brain !! it's never easy to find something explained in basic laymans terms these days:confused:

Thanks for the offer of more advice its most welcome!!

PAul
 
Here is where you get a quick and dirty lesson in normalization.

Think of a spreadsheet. It is rows and columns. Data gets repeated all over the place. Patient's name, doctor's name, name of a prescriptive drug, and all of the things that go with each.

Now there's Access with relational database technology.

You have a table of patients with all the data about the patient - and an ID field that is unique to the patient. Read up on AUTONUMBER to see just one of many ways to do that. But other ways exist to do that.

You have a table of doctors. (Presuming your office is big enough for many doctors to be on staff.) Again, a Doctor ID. Could be another autonumber or could be something else, but almost always is simple, whatever it is. But it has to be unique.

Now there's a table of possible drugs. Again, everything you wanted to know but were afraid to ask about each drug. Plus some ID number. Since the FDA publishes tables of drugs, your ID could come from that. Or you can roll your own numbering system. Just has to be unique.

What's going on here? I'm identifying entities, things/people, objects - related to your business. Because the first step in building an Access database to be used for a business is to establish the entities of the business. A business entity can be many things. But an ELEMENTARY entity stands on its own. Doctors exist whether they have appointments today or not. Patients exist. Drugs exist. Consumables exist. Vaccines exist. Even treatments exist on their own as abstract entities. Surely there is a book (or two) that list treatment codes approved by the AMA, so you have ready-made ID fields even for that concept.

Why do you need this? Because you are about to build a MODEL of your business using records in the various tables to repesent the elementary entities of your business. That's the first step, and is often the second-hardest part.

The hardest part is next - figuring out how these entities interact. THIS is where your model comes alive, representing the daily ebb and flow of your business operations. Patients have appointments with doctors. Doctors administer drugs or vaccines or treatments to patients. Patients cough up ... money for their bills - and we hope that's all they cough up, but in a doctor's office,?

So now we come to the next topic - JUNCTION tables. But I must digress. You need to understand that Access has a technical limitation regarding how one table can relate to another table. Access only supports one-to-one and one-to-many relationships. Note, of course, that relationships are defined in both directions, so seen from the other side, one/many becomes many/one. But Access cannot directly represent many-to-many relationships. Like, what happens when a patient sees more than one doctor in your office on the same day? What happens when a doctor sees more than one patient in the same day?

Junction tables help you make lists of interactions. I'm going to break the topic here and pick up in a new post because of reading limits and size limits.
 
Junction tables are part of your model. They tell you how two or more of your different entities interrelate.

Take the most obvious relationship. Patient visits. OK, what do you need to know about a visit? The patient and doctor, for sure. The time? Naturally. The level of visit? Yep. Some indication of diagnoses, treatments, vaccines, and prescriptions, plus any case of handing out samples. What about ... notes, instructions, comments, remarks, whatever journal entries you make for a patient. This sounds confusing, but follow it slowly and you'll see where this goes.

A "Visit" will require certain fields that have data not part of the "basic" tables. A doctor is a doctor regardless of what date and time it is. But a visit has a date, time, doctor, and patient. Here is where a relational table becomes valuable. If a Visit is counted as a JUNCTION table, you give it an ID number (here, I'd bet autonumber IS the right answer), a date, time, and the ID numbers of the doctor (from the Doctors table) and the patient (from the Patients table). You don't need the doctor's name if you have the ID. You can look up the name. Ditto, patient. But the date and time need to be part of the Visit record because they don't go with either the basic doctor record or basic patient record. They are an attribute of the VISIT that occurs where the doctor and patient come together. (One reason it is called a JUNCTION table...)

Remember I said that a visit would be granted an ID? Here's where it becomes useful. Above, I mentioned the chance that a vaccination occurred at a visit. So you would have a vaccine table, right? Put a record in a VACCINATION table that is a junction between the VISIT table and the VACCINE table, showing that somebody got stuck. You can also include the doctor and patient IDs and a date, though in this case it would be overkill since the VISIT table holds that info. Two vaccinations at the same visit? Add TWO records to the vaccination table, with the same visit ID but with different vaccine ID codes.

What about writing a prescription? Prescriptions have various factors including the drug, number of doses, dose instructions, dose size, number of refills allowed, and a few other things I'm sure you can define better than I can. You have the drug table to show drug ID - thus not having to store the name of the drug. You can include the visit ID - thus showing when the scrip was written. The details of dosage and refills go in the Prescription table, which is a junction between visit and the drugs table.

You can have a "child" table from the visit where you have a visit ID and a memo field for notes. Lots of notes - per visit.

And here is where you made life more complex - but also allowed it to be represented. When you have these ID fields, you allow LINKS between the tables. And those LINKS in the junction field show you the interactions of the entities being linked.

Break again because this is an ugly topic...
 
Why do you use the links? So you won't have to write the doctor's name every time. But wait, you say... won't I need to write out the name anyway?

If you define formal relationships between the tables, no.

Look up COMBO BOXES as a way for you to use drop-down lists to look up something and store something else. A combo box will let you find the doctor's name but store the doctor's code number using a form. And the form's TOOLS WIZARD for combo boxes has this exact action as one of its presets - look up one thing but store something else.

I can see your Appointments Form having a doctor drop-down, a patient drop-down, and some scheduling things. SEARCH THIS FORUM for "Scheduling" as a topic and see how folks have approached the problem of resolving conflicts, finding openings, and the like.

I can also see a multi-tabbed form with links to drug tables, vaccine tables, notes tables, treatment tables, etc.

Notice that I glossed over something earlier? In the first part of the discussion I named a few tables. But later I named a few more. THIS IS NORMAL. When you explore a problem, you discover new features that you did not at first realize would be required. But they crop up and get you every time.

I know this is one hellacious lump, but I would be remiss if I didn't toss in a few tips here anyway. In order for this to work, remember that when you define a one-to-many relationship, the "one" side of the relationships MUST have an index on that unique field that is your ID number. In fact, that is usually how you ASSURE that it is unique. You declare an index on it with the "No Dups" attribute. You should only have one of these unique indexes per table, though I could imagine cases where you MIGHT have more than one. But in any case, you pick the unique index as the table's PRIMARY KEY - which we call a PK here for short.

In the junction tables, you repeat those keys as part of the links between the junctions and their elementary tables. In the junction table, they are pointers to the right record in the elementary table and in that role are FOREIGN KEYS (we call 'em FK for short). Remember I suggested that a Visit table, which is itself a junction between doctor and patient, might also have a PK of its own? It is not only possible but quite reasonable for a junction table to have one PK and several FKs in it at the same time.

The idea of a Vaccination, Prescription, or Note being associated with a Visit means that a visit record is the PARENT of the corresponding records in the vaccination, prescription, and note tables' CHILD records. So this forms a hierarchy.

Now... this should get you going. You must analyze the data sets in light of normalization, which is how you started and how I'm going to finish.

Normalization prevents you from mixing apples and oranges. Which is why I told you to find the elementary entities and put them in their own tables, then build junctions between the elmentaries to represent the interactions. If you didn't do that, you would something that looked like a spreadsheet with doctor names, patient names, drug names, vaccine names, and everything on one line. Heaven help you if you did more than one thing of the same class but with different details in the same visit. And normalizaiton prevents you from having to make room for extra details of that sort, which is the other part of why you normalize.

What advantages does this give you? By splitting out elementary entities, it does not matter if you have records on a drug that you don't prescribe. If you ever do, its data is still there, waiting to be used. If you have a patient who went out of town for six months, you still have patient data handy. If you did multiple things in one visit, you can see a list of things you did by following the various links - or SEARCHING for the various links as a reverse-logic way of finding what was done.

Read the three posts and see if they help you orient your thinking. Access will let you work with what I just described. It will help you once you learn what you want to do.

BUT the sine qua non of Access is to research your problem so well that you can write down your business rules on paper. When you can do that AND the stuff on paper makes sense (reasonably reflects what you do), you can hope to make Access do it for you. Until then, you aren't ready.

Good luck on this. I know it is an ambitious undertaking. Things worth doing usually are.
 
I'm back.

Here's where we get theoretical and practical at the same time.

Purists would look at what I told you and agree with most of it. The part where I said you could put doctor ID and patient ID in the Vaccination table as well as the Visit ID - that would cause hesitation. I also said the Visit ID made it unnecessary to keep the doctor and patient ID in the vaccination table.

A purist would say they HAD to be separate. A pragmatist would say that as long as you NEVER EVER change the doctor and patient ID in the vaccination record, it is OK to have them stored redundantly. It would be OK to copy the date of the visit to the vaccination entry. They MUST be immutable once stored. (If they aren't, you're just piddling around with a mish-mosh of a thing that looks like a practical database.)

Why would you wish to store (or not store) redundant data? Well, here is where the practical and theoretical sides of the house sometimes go their separate ways. To find out who had vaccinations, you need to build what is called a JOIN query linking all visits with all vaccinations. The vaccination table joins with the vaccine table and the visit table joins with the patient table. The result would be a query in which you had a list of patient names (from the patient table), dates (from the visit table), and vaccines (from the join of the vaccine to vaccination to visit tables.) OR you could just include the patient ID in the vaccination table and a copy of the visit date, so that you have only three tables involved, not four. Join the patient ID to the vaccination to the vaccine and get a list of patients.

The above paragraph discusses how you would use these tables. The first thing to notice is that to find who did what to whom when and with what is that you query the VISIT table as the center of your database. You then join the visit to other things through other junction tables until you can build a query that, when opened, looks awfully much like that spreadsheet I started talking about earlier. That is a characteristic of normalized databases. You have base tables for the elementary entities, but it is the JUNCTION tables that show you the meat of your real business. In your case, visits and the actions that can occur during the visits. The vaccines that are used, the consumables expended, the drugs prescribed - these things have their own basic entity tables and the junction tables that, in essence, list the relevant items for each visit.

How do you know if, say, NO injections or vaccinations occurred for a given visit? This is where you learn about domain aggregates. You can use DCount to find how many vaccinations occured for patient John Smith in July of 2006 because you know John Smith's ID code. Depending on the pragmatic or purist view, you can search for his code in the vaccination table or in the JOIN of the vaccination and visit tables. The difference is one query.

The purists will tell you that because the query can be written that would provide you links to everything you needed, you should not include the redundant ID codes copied from the visit table. And they'd be right.

The pragmatists would tell you that this query slows things down a bit because it takes time to organize the join. You can save time at the cost of a little extra space by copying the patient ID and other relevant data to the vaccination record - or other relevant records, as needed. And they'd be right.

Both will tell you that once you make the copy, you cannot change it. And they'd both be right.

I cannot tell you which way to go because each of us has his/her own style. But I can tell you that you should decide on a style and stick to it. If you don't, you really ARE doomed.

Now some further practical advice. Earlier I mentioned that you must have your PK based on an index. But you can have up to 10 indices on a table. There are reasons to choose or not choose a particular field as a place to put an index. Sometimes all you need is a sort within a query, not an actual index on which to base a report order. Sometimes the order of a report's detail records is not the same as the PK of the base table. So here's the rule I usually apply.

There is always an index on the PK and there will be a PK when the table in question is on the "one" side of a one/many a relationship. There will be secondary indices when a very common action involves a search of the field that has this secondary index. However, ...

The smaller the table, the less likely you are to need a secondary index. The less often you touch the table, the less likely you are to need a secondary index. The bigger the field that would be indexed, the less often you should make it an index in the first place. In the ideal case, a secondary index is used on short, frequently-searched fields in long tables.

The balancing act is that the more indices you have, the more expensive it is for Access to maintain them. In tables with relationships, you cannot avoid having one index. But the question is, how many more indices do you need and how much will it cost you to maintain them? See, every time you add a record, delete a record, or change the value of an indexed field, you must change EVERY INDEX on that table to reflect the new table contents.

Something you probably will have to consider is billing. A patient will incur charges for a visit, for vaccinations, for consumable supplies, etc. etc. You treat charges as another basic entity linked to a visit. A charge can be itemized by considering the elementary things that contribute to it. And where do you start? The Visits table already provides the link to every other basic element. The visit itself incurs a charge. Vaccinations, consumables, etc. all LINK to the visit so can be used to group items that relate to the visit.

This leads to the next topic, which I urge you to look up in the Help files and also to search in this forum... A UNION query can be used to define your charges at the individual line-item level, which can be rolled up based on the visit ID to provide a visit charge. I'll leave that research to you for now.

What I just gave you is a full pile of information and ideas that will take a while to digest. But I think if you can pick your way through it, you will see how this sort of thing works.

I really do relate to the idea of being so lost that you don't know whether you are coming or going. I'm having that little headache myself right now with security based on digital certificates and trust chains. Since I'm not up on some of the detailed certificate lingo, I have to pick my way through it slowly. Oh, I've studied it enough that I'm beginning to get the basic ideas, but the technical understanding is slow to come. It will come for me - as it will for you - based on study and meditation.
 
HI Docman, firstly sorry for not replying sooner I havebeen away for a few days. I must say am extreamly greatful to you for such a long reply and for taking the time to write it although it will take me longer to digest it :)

I know there is no way to show you gratitude but let me assure you it is there!!

I will print this off and go through bit by bit and get back to you with the results

once again thank you so much

Best regards

Paul
 
Thank you, Doc Man!

It wasn't my question, but the information you gave was so well presented that I, too, made a hard copy so I can go back and digest it slowly. Thank you so much for taking the time to explain things - patiently and thoroughly.
 
You are both welcome. I can only do so much to help people. This seems to be the best way to provide that help to the widest possible audience, given the narrow field of endeavor.
 
i think doc.s explanation should be made sticky. Not that I would have understood it when I got started, but this is an incredibly informative little lecture series.
 

Users who are viewing this thread

Back
Top Bottom