I am doing a UML model based on my ERD generated with MySQL workbench. But now i have a doubt about the primary and foreign keys representation in a class diagram.
In the traditional UML diagram we should include the primary keys as atributes of each class? For example, the id_user
or the id_list
? And about the foreign keys ? These ones are ignored as attribute but reflected as associations?
thanks
A primary key is a set of columns in a table that together uniquely identify a row. For example, name and address may uniquely identify a 'Customer'.
A primary key is used to assure the value in the particular column is unique. The foreign key provides the link between the two tables. Find out the major difference between primary key and unique key.
Foreign Key The Foreign Key is the constraint implementing a relationship. The constraint is always implemented on the child table. The Foreign Key Constraint uses the stereotype <<FK>> in the UML Data Modeling Profile.
First off: why do you want to draw it in UML? If you already have an ERD, what additional/alternate property do you want to illustrate with a UML class diagram that the ERD doesn't give you?
Why? Because UML is a tool. On the assumption the diagram is for human consumption only (i.e. you're not code generating from it) then you should use UML to expose the information you're trying to communicate.
As a general standard UML says nothing about how you formalise identity (PK/FK). UML follows the OO idiom that every object has implicit identity - therefore you don't need to specify it explicitly. So in the simplest case, you could:
If that meets your modelling needs then you're done.
As a second refinement, you could tag the PK attributes with an ocl isUnique()
constraint, again ignoring the FKs.
Another option would be to use the rules of Executable UML. It denotes both PKs ('Identifiers'} and FKs ('referential attributes'} directly on the class diagram. So it's closest to capturing everything in the ERD.
So in summary: there is no UML-imposed right answer. It all depends on what you want to communicate with the diagram.
You can use/create a "database profile" to annotate your UML model with this information. Without a profile you could use OCL to specify uniqueness constraints while normal associations should be enough to represent what would be transformed into foreign keys at the database level
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With