Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primary/foreign keys in uml

Tags:

uml

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

like image 812
user947462 Avatar asked Apr 23 '12 14:04

user947462


People also ask

What is primary key in UML?

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'.

What is primary foreign key?

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.

What is a foreign key in UML?

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.


2 Answers

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.

like image 110
sfinnie Avatar answered Jan 03 '23 14:01

sfinnie


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

like image 23
Jordi Cabot Avatar answered Jan 03 '23 13:01

Jordi Cabot