UML: 1-To-Many Relationship Representation? - oop

I am new to UML, and I don't quite understand the notation yet.
This is my understanding of the diagram.
The system has many doctors. Doctors can have many patients; however, patients can only have one doctor. Therefore this is a **One-To-Many** relationship
However, I feel like this can also be interpreted as...
The system has 1 doctor. That one doctor has many patients.
Which is the correct interpretation and why?

To put it simply: your first interpretation is correct. Each Doctor is associated with many Patients. Each Patient is associated with one Doctor. The multiplicity of 1 tells you only how many Doctors are associated with each Patient, not how many are in the system. Understand that a class usually represents a set of many instances.
BTW, for an analysis model, you should use association-end names to give these associations semantics. For example, each Doctor treats many Patients. Each Patient is treated by one Doctor. The semantics will expose whether your model is correct for the business domain. Without semantics, the relationship could represent billing, treatment, visits at the hospital, etc. The treats goes on the end with the *, and the is treated by goes on the end with the 1.
For a lower-level model, you should replace treats with treatedPatient and replace is treated by with treatingDoctor.

Lets say your system is a hospital, clearly more than one Doctor should be allowed to work at the hospital so the most correct interpretation is the 1st.

The association line on the diagram shows one association. There could be many associations, or none of them, that doesn't matter. We are speaking on one concrete association:
a doctor can have many patients.
So, your second interpretation is also correct, but it speaks on ONE exemplar of associations only.
If we'll recall, that the number of associations is arbitrary, for the whole system we are having the first sentence.
An association can be realized so, that every patient will have a reference (possibly null one) to a doctor. We could realize it as a list of patients for every instance of doctor. Or both - for easier search, for example. But all this information speaks only about ONE relationship - one doctor to many patients.
Notice, that there could be 2(or more) such relationships. For example, a doctor had visited today some patients. It is the relationship that we'll name DoctorIsActive. And the other one - doctor was visited by some patients. It will be another relationship, PatientIsActive. So, we'll have two lines connecting these blocks, with different names on them, both one-to many. But we don't have two doctors because of it. It could be even more complicated - for example, a doctor can make on operation a day.
Don't be ashamed - I had sometimes the same problem with understanding this fact, too. And as I know, it is very widespread misreading.
Look here for more info on associations.

Related

How to build an active record association between three models

I'm trying to model an application for physicians and am stuck wrapping my head around the db design. Let's assume that a user can create a case, which can be associated to symptoms, diagnoses and therapies. This doesn't sound too hard in the first place.
However, I need the physician to provide the diagnosis from a given table which includes all known diagnoses (seeded by an admin). Furthermore, one diagnosis can have many therapies, while the physician should choose one for that particular case (again all possible therapies are seeded by an admin).
I haven't started actually coding the application but I've created a db diagram. The best I could think of so far is to associate cases and diagnoses through has_and_belongs_to. This doesn't seem to be optimal either as a case can only have one diagnosis.
Anyway, I'm really not quite sure what to do with therapies as a case only has_one therapy and a diagnosis can has_many therapies.
I also thought about working with jsonb columns, but I would rather stick to a relational approach.
Any input on this is highly appreciated!
From what I understand, you want a model that supports the following logic:
a case can only have one diagnosis
a diagnosis can be picked from a list of options
a diagnosis can have many therapies
a case can only have one therapy
If that's correct, then what you have so far looks pretty good. A few comments / questions:
If you can only have one diagnosis per case, why not just include diagnosis_id as an attribute in the cases table? Unless you need to specifically track any other data between the case-diagnosis relationship (which you aren't doing right now), you don't need a separate table. This could potentially speed up your queries by cutting out an additional JOIN. If think down the line it might change, then you can leave it as a separate table.
If a case can only have one therapy, again you can just include this as an attribute in the cases table. See the logic in #1.
When you say a diagnosis can have many therapies, do you mean that the user will assign one or more therapies to a diagnosis? If that's the case, then just create a new table called case_diagnosis_therapy where you can track this. The relationship between a case's diagnosis and its therapies would be through (case_id, diagnosis_id). You already have the diagnoses lookup table.
And yes, I agree that you are better suited to stick with a relational design instead of JSON. Your model looks pretty structured.
Just a suggestion for conventions, when you make your table names usually you use singular names - case instead of cases.
Just a few things to think about, otherwise it looks pretty good! Hope that helps.
From my understanding, a case
has-many :images
has-many :symptoms
has-many diagnoses from a fixed list of possible diagnoses, which you solved correctly. My only remark would be to call the reference diagnose_id (from the case_diagnoses table)
The only thing missing is the therapies associations. I see two possible options:
a therapy is always linked to a single diagnoses
a therapy could be linked to multiple diagnoses
E.g. for a fever and a sprained ankle, the therapy could both be "cooling", or they could be much more specific (and more diagnoses related). So that is unclear.
So if a therapy is always linked to a single diagnoses:
therapy belongs-to diagnose
a case has-many case-therapies
and case-therapy belongs_to therapy
If a therapy can belong to multiple diagnoses, you just have to add a inbetween link table
diagnose-therapies belongs-to therapy and diagnose
... and the rest stays the same.
(oh and thank you for sharing the dbdiagram.io platform, I had never heard of it before, it looks amazing!)

In an ER Diagram, how do I know when you use one or many?

I am trying to understand cardinality in ER diagrams, starting with a simple example of a visitor visiting a city. Thinking about it, you can have many visitors visiting many cities, so I drew the ER diagram as shown. Visitor is an Entity with attributes such as name, Visits is the relationship with the VisitorID and CityID as the primary key and other attributes such as date, and City is an entity, with attributes such as country. This would mean 0 to many visitors can visit 0 to many cities. Does this mean that in a database with this ER diagram, I would have three tables? One for Visitor, one for City, and one for VisitCity? Am I understanding the cardinality correctly?
Yes, you described the relationships correctly. N:M cardinality needs a third table.
Visits could contain other information, too. For example, since a visitor can't be in two places at once, there might be an associated time period.
Visitor could have another relationship to City -- say, born_in with different cardinality, namely N:1, meaning many people were born in the same city (but each is born in only one city!) For that, born_in would be an attribute of Visitor.
yes that is correct. your drawing shows 0 to many visitor(s) can visit 0 to many city(s). so in this case you are correct. as you stated you have named your middle-table visitCity.
but lets say you are required to have a database where visitors can visit 1 city only. you would have one-to-many relationship. this means you don't need to use a middle-table for that. so you will end up with 2 tables only.
just because something logical like "many visitors can visit many cities" doesn't mean is true. the requirements is what makes these rules (project assigned by the client saying what he/she needs. or if u are still in school, this would be the papers assigned to you wich the story in it). so when creating databases you have to listen/read the requirements very carefully to determine when to use what. but from reading what u have said i think u are going in the right track :)
Yes, that's exactly right.
If you wish to keep your DB in 1NF, every time you face yourself with a N x N cardinality, a new table must be created with the keys to both entities.
More info on normalization here.

Confused on SQL Relationships

I am confused in SQL Relationships, specially with One to One and One to Many. I have read many articles like this and this
For an example, below are some statements from those articles and some other articles
The relationship between customer and address tables are one to one, that is because one address is belong to one customer.
The relationship between customer and order tables are one to many, that is because one customer can make many orders.
customer and contactData tables are one to one because one contactData is belong to one customer
Yes, true, but take my argument now.
In point 2 it says the relationship is one to many, but one order is belong to exact one customer right? So you can consider it as one to one as well.
In point 3, contactData and customer relationship said to be one to one, but one customer can have many contactData right? So it is one to many
With all of these, I am seriously confused. Can someone give me a clear definition about how to find the relationship please?
The relationship between customer and order tables are one to many, that is because one customer can make many orders.
This should better read
a customer can place many orders
an order belongs to one customer
So it is a one to many relationship between the tables.
customer and contactData tables are one to one because one contactData is belong to one customer
This should better read
a contactData belongs to one customer
a customer can only have one contactData
So it is a one to one relationship between the tables.
Yes, in reality a customer of some company may have more than one contact, but in this particular database they are talking about, it is defined that a customer can have only one contactData.
EDIT: I should add that you can say "there is a one to many relationship between tables a and b", which doesn't specify which is which, so you can be more precise, saying "there is a one to many relationship from table a to b" or "there is a many to one relationship from table b to a". With one to one, you don't have such problem of course. (Adding to this, there are even more precise definitions, such as a "one to one or zero" etc. where you specify if zero records are allowed, such as a person without contact data.)
Point 2 is clearly one to many because one customer can have multiple orders but every order belongs to exactly one customer.
You have to read the relations in both directions.
Imagine this relation was one to one. You would need to create a new customer account every time you want to place a new order.
Point 3 is probably a design question. If you want to allow multiple contactData there is no reason to keep it one to one.
Dongle. You are making a mistake of looking at the subject symmetrically. Relationship is considered to be one-directional, that's why you felt confused.
Let's take for example this Customer <-> Order relation. Like you observed, customer -> order is something completely different than order -> customer. They are actually independent, however weird it may be. It's because in the real code you're never actually dealing with two objects at once, rather than "acting" from perspective one of the two. E.g. you fetched order object from the db and only then you're who is the customer that made this order. Or you have the customer object and you want to check what are his orders.
As you see, this is very uni-directional (we call relations uni- or bi-directional). You have object X and you want to "enhance" your knowledge of it, by joining it with the other table, object Y. So it's X->Y. Or Y->X. But never actually it is X<->Y. That's why you always have to look at the relations as if they were independent.
Apart from that, you're right and it usually is so, that one relation is One to One and the other One to Many.
Your "point 2" got answered by Rav.
"point 3": I found in one of your first links the one to one example with "Addresses" table. In this example a customer can have one address only. So if your customers can have more then one you will need one to many (as in the example Orders).

Why no many-to-many relationships?

I am learning about databases and SQL for the first time. In the text I'm reading (Oracle 11g: SQL by Joan Casteel), it says that "many-to-many relationships can't exist in a relational database." I understand that we are to avoid them, and I understand how to create a bridging entity to eliminate them, but I am trying to fully understand the statement "can't exist."
Is it actually physically impossible to have a many-to-many relationship represented?
Or is it just very inefficient since it leads to a lot of data duplication?
It seems to me to be the latter case, and the bridging entity minimizes the duplicated data. But maybe I'm missing something? I haven't found a concrete reason (or better yet an example) that explains why to avoid the many-to-many relationship, either in the text or anywhere else I've searched. I've been searching all day and only finding the same information repeated: "don't do it, and use a bridging entity instead." But I like to ask why. :-)
Thanks!
Think about a simple relationship like the one between Authors and Books. An author can write many books. A book could have many authors. Now, without a bridge table to resolve the many-to-many relationship, what would the alternative be? You'd have to add multiple Author_ID columns to the Books table, one for each author. But how many do you add? 2? 3? 10? However many you choose, you'll probably end up with a lot of sparse rows where many of the Author_ID values are NULL and there's a good chance that you'll run across a case where you need "just one more." So then you're either constantly modifying the schema to try to accommodate or you're imposing some artificial restriction ("no book can have more than 3 authors") to force things to fit.
A true many-to-many relationship involving two tables is impossible to create in a relational database. I believe that is what they refer to when they say that it can't exist. In order to implement a many to many you need an intermediary table with basically 3 fields, an ID, an id attached to the first table and an id atached to the second table.
The reason for not wanting many-to-many relationships, is like you said they are incredibly inefficient and managing all the records tied to each side of the relationship can be tough, for instance if you delete a record on one side what happens to the records in the relational table and the table on the other side? Cascading deletes is a slippery slope, at least in my opinion.
Normally (pun intended) you would use a link table to establish many-to-many
Like described by Joe Stefanelli, let's say you had Authors and Books
SELECT * from Author
SELECT * from Books
you would create a JOIN table called AuthorBooks
Then,
SELECT *
FROM Author a
JOIN AuthorBooks ab
on a.AuthorId = ab.AuthorId
JOIN Books b
on ab.BookId = b.BookId
hope that helps.
it says that "many-to-many relationships can't exist in a relational database."
I suspect the author is just being controversial. Technically, in the SQL language, there is no means to explicitly declare a M-M relationship. It is an emergent result of declaring multiple 1-M relations to the table. However, it is a common approach to achieve the result of a M-M relationship and it is absolutely used frequently in databases designed on relational database management systems.
I haven't found a concrete reason (or better yet an example) that explains why to avoid the many-to-many relationship,
They should be used where they are appropriate to be used would be a more accurate way of saying this. There are times, such as the books and authors example given by Joe Stafanelli, where any other solution would be inefficient and introduce other data integrity problems. However, M-M relationships are more complicated to use. They add more work on the part of the GUI designer. Thus, they should only be used where it makes sense to use them. If you are highly confident that one entity should never be associated with more than one of some other entity, then by all means restrict it to a 1-M. For example, if you were tracking the status of a shipment, each shipment can have only a single status at any given time. It would over complicate the design and not make logical sense to allow a shipment to have multiple statuses.
Of course they can (and do) exist. That sounds to me like a soapbox statement. They are required for a great many business applications.
Done properly, they are not inefficient and do not have duplicate data either.
Take a look at FaceBook. How many many-to-many relationships exist between friends and friends of friends? That is a well-defined business need.
The statement that "many-to-many relationships can't exist in a relational database." is patently false.
Many-to-many relationships are in fact very useful, and also common. For example, consider a contact management system which allows you to put people in groups. One person can be in many groups, and each group can have many members.
Representation of these relations requires an extra table--perhaps that's what your book is really saying? In the example I just gave, you'd have a Person table (id, name, address etc) and a Group table (id, group name, etc). Neither contains information about who's in which group; to do that you have a third table (call it PersonGroup) in which each record contains a Person ID and a Group ID--that record represents the relation between the person and the group.
Need to find the members of a group? Your query might look like this (for the group with ID=1):
SELECT Person.firstName, Person.lastName
FROM Person JOIN PersonGroup JOIN Group
ON (PersonGroup.GroupID = 1 AND PersonGroup.PersonID = Person.ID);
It is correct. The Many to Many relationship is broken down into several One to Many relationships. So essentially, NO many to many relationship exists!
Well, of course M-M relationship does exist in relational databases and they also have capability of handling at some level through bridging tables, however as the degree of M-M relationship increases it also increases complexity which results in slow R-W cycles and latency.
It is recommended to avoid such complex M-M relationships in a Relational Database. Graph Databases are the best alternative and good at handling Many to Many relationship between objects and that's why social networking sites uses Graph databases for handling M-M relationship between User and Friends, Users and Events etc.
Let's invent a fictional relationship (many to many relationship) between books and sales table. Suppose you are buying books and for each book you buy needs to generate an invoice number for that book. Suppose also that the invoice number for a book can represent multiple sales to the same customer (not in reality but let's assume). We have a many to many relationship between books and sales entities.
Now if that's the case, how can we get information about only 1 book given that we have purchased 3 books since all books would in theory have the same invoice number? That introduces the main problem of using a many to many relationship I guess. Now if we add a bridging entity between Books and sales such that each book sold have only 1 invoice number, no matter how many books are purchases we can still correctly identify each books.
In a many-to-many relationship there is obvious redundancy as well as insert, update and delete anomaly which should be eliminated by converting it to 2 one-to-many relationship via a bridge table.
M:N relationships should not exist in database design. They are extremely inefficient and do not make for functional databases. Two tables (entities) with a many-to-many relationship (aircraft, airport; teacher, student) cannot both be children of each other, there would be no where to put foreign keys without an intersecting table. aircraft-> flight <- airport; teacher <- class -> student.
An intersection table provides a place for an entity that is dependent on two other tables, for example, a grade needs both a class and a student, a flight needs both an aircraft and an airport. Many-to-many relationships conceal data. Intersection tables reveal this data and create one-to-many relationships that can be more easily understood and worked with. So, the question arises, what table should the flight be in--aircraft or airport. Neither, they should be foreign keys in the intersection table, Flight.

Confusion about 1:1 relationship

I've been learning database design and I'm confused about 1:1 relationships. From what I understand, you can simply add columns to the appropriate table. Can someone provide a real world example of where a 1:1 relationship was either necessary or provided some significant benefit? I.e., where would I use a 1:1 relationship and what would it look like?
I'll give you a real practical example.
In the medical billing world, doctors who want to get paid by medicare handle billing by creating a dictation report for each visit with a patient. This might actually be a recorded audio dictation transcribed by a secretary, but more often it's just a written description of what they did and talked about with the patient, along with history, impressions, and so forth. A licensed medical coder will then read this dictation and decide what the doctor is allowed to bill.
Separate from the dictation, there is demographic information about the patient involved: name, age, billing address, etc. This information must be strictly separate from information about the dictation, to prevent coders from allowing bias to cloud their billing judgements or violating patients' privacy.
This data is often kept well-normalized with a 1:many relationship in the data systems at the point of origin, and only the right parts are displayed to the right people at the right times. However, a significant number of offices out-source their billing function to a third party. This way a small clinic, for example, doesn't have to keep a licensed medical coder on staff; one coder at the billing office can handle the needs of many clinics. When the data is sent from the clinic to the billing office, the patient demographic information and the dictations need to come over as separate pieces, possibly at separate times. At this point, they'll likely be stored in completely separate tables with a 1:1 relationship and a shared ID field to match them up later.
In this case, the 1:1 relationship has very little to do with the data model. You could probably match up the records at the time of import, and as a bill moves through the system eventually the provincial patient information received in the clinic's demographic record will be matched to a real person so the 1:many relationship can be restored. Otherwise you'd get a separate statement on a separate account for each visit to the doctor.
Instead, it has almost everything to do with the systems design. There are likely entirely different people building and using the billing part verses the coding part at our imaginary billing service. This way, each side can each have full control of it's own fiefdom, and you are sure that no one, not even a developer, is breaking any privacy rules.
True one-to-one relationships seldom
occur in the real world. This type of
relationship is often created to get
around some limitation of the database
management software rather than to
model a real-world situation. In
Microsoft Access, one-to-one
relationships may be necessary in a
database when you have to split a
table into two or more tables because
of security or performance concerns or
because of the limit of 255 columns
per table. For example, you might keep
most patient information in
tblPatient, but put especially
sensitive information (e.g., patient
name, social security number and
address) in tblConfidential (see
Figure 3). Access to the information
in tblConfidential could be more
restricted than for tblPatient. As a
second example, perhaps you need to
transfer only a portion of a large
table to some other application on a
regular basis. You can split the table
into the transferred and the
non-transferred pieces, and join them
in a one-to-one relationship.
That's a quote from here: Fundamentals of Relational Database Design
And here's a similar question on SO.
Another reason I can see for using a 1:1 (where I have used it in the past) is if you have a table with a lot of columns, and only a few of them are involved in very intensive and frequent queries which need to be fast, I would break it into two tables that are related 1:1 where I could query the lightweight table and get good performance, but still have the other data related to it easily with a simple join.
I belief tables should be designed with the domain background. So if those columns form two different entities, they should not be mixed in one table. From my experience 1:1 relationships tend to evolve into 1:n relationships over time.
For example you may want to store the postal address of a person. But after some time, you are required to store more than one address per person. Refactoring programs from a 1:1 relationship into 1:n is usually a lot easier than extracting some columns from an old table into a new one.
Many database systems allow defining of access permissions per table in a very easy way. But defining permissions on individual columns is often quite painful.
It's useful if X has a 1:1 relationship with Y and Z also has a 1:1 relationship with Y. Y can be abstracted out into a shared table rather than duplicating in both X and Z.
EDIT: A real world example would be Customers, Companies, and Addresses. There can be a N:N relationship between Customer and Company. But both Customer and Company have 1:1 relationships with Address. Some Address rows could be related to both a Customer and a Person.
First, because they are talking about Access (Jet, Ace, whatever) -- credit to #Richard DesLonde for spotting this -- then they are probably talking about 1:0..1 relationships. I do not believe true 1:1 relationships are workable in Access because it has no mechanism for deferring constraints nor executing multiple statements in a SQL PROCEDURE. Most Access practitioners are satisfied to use a 1:0..1 relationship to model a true 1:1 relationship, so I guess the authors are satisfied to use the term "1:1" informally to refer to both.
Of course, 1:1 and 1:1..0 relationships are common enough in the real world. I rather think they are trying to convey the (valid) point that some 1:1 and 1:1..0 relationships are invented in a data model for business purposes.
Consider a "natural person" (i.e. human) and a "corporation". They have no attributes in common (sure, both have a "name" but their domains are different e.g. "natural person name" has sub atomic domains for "family name", "given name" and "title", etc).
However, in a given data model distinct entity types may play the same role. For example, both a "natural person" and a "corporation" can be the officer of a "corporation". In the data model, we could have two distinct entity types "natural person officers" and "corporate officers" that are likely to have many attributes in common and from the same domains e.g. appointment date, termination date, etc; further, they business rules would be the same e.g. appointment date must be before termination date. Also, both would participate in equivalent relationships e.g. "natural person representing", etc.
The data model could be 'split' at high level, resulting in pairs of very similar tables e.g. "natural person officer" and "corporate officer", "natural person officer natural person representing" and "corporate officer natural person representing", etc.
However, another approach is to model the common attributes and relationships using a fabricated entity type. For example, both a "natural person" and a "corporation" could be considered to be a "legal person" (aside: there is such a concept of "legal person" in law but does this mean the same as existing in the real world?!)
Therefore, we could have a superclass table for "legal persons" and subtype tables for "natural persons" and "corporations" respectively. The "officers" table would reference the "legal persons" table. All subsequent relationship tables could reference the "officers" table, which would half the number of tables from this point down.
There are practical problems to such a 'subclassing' approach. Because a "natural person" and a "corporation" has no attributes in common, they have no common key, therefore the "legal persons" table would need to have an artificial key, with all the problems that this entails, especially if it needs to be exposed in the application. Also, because the relationships between "legal persons", "natural persons" and "corporations" are truly 1:1 some DBMSs, as Access, will lack the necessary functionality to effectively implement them and many will have to settle for making them 1:0..1.
A 1:1 relationship is an abstract concept that you model in your data, but at the database level (assuming RDBMS) doesn't really exist. You always have a foreign key on one table pointing to another, so technically the parent table being pointed to by the FK could have multiple children. This is something you'll want to enforce in your business logic.
A good example of a 1:1 relationship in a modeling sense would be the relationship between employee and person. You have a person with certain data, then you have extra attributes on that same person that you put on an employee. A good way to think of this in OO programming terms is as inherited classes. The Employee class, inherits from Person. In fact may ORM systems will model the 1:1 relationship in the database with each table having a shared primary key.