8

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

user947462
  • 929
  • 6
  • 18
  • 28

3 Answers3

3

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:

  • list PK attributes as normal attributes;
  • ignore FK attributes altogether.

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.

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
sfinnie
  • 9,854
  • 1
  • 38
  • 44
  • 1
    Ignoring FK does not make much sense, imo. Using associations (which have properties for the member ends as well) like the OP asked is more suitable. – Christian Apr 24 '12 at 14:01
  • @Christian: Yes, FKs could be shown as roles on the association ends. My point was there's no hard and fast set of rules that must be applied. You have latitude to do something that works in your particular case. FKs as roles would be a good default though. – sfinnie Apr 24 '12 at 14:37
1

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

Jordi Cabot
  • 8,058
  • 2
  • 33
  • 39
0

You can use EclipseUML Omondo trial and reverse your database.

You will get the information you need and you can then copy it in a free or open source tool.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
UML GURU
  • 1,452
  • 1
  • 9
  • 7