Monday, October 18, 2010

Making great ER diagrams without drawing

This one will be short :-)

I needed to draw a decent looking Entity Relationship diagram for an academic assignment --BORING. This is the kind of thing that makes me wonder why I surrendered my soul to this degree hunting battle. Anyway, there are two utilities (Maybe more, who knows) that might help the candidate ER Michelangelo: Dia and kivio. Now, if you are into buttons and mousing that's all you should need; pick one, search for some online examples and you will be done in no time. Chances are your drawing will look like shit but then again, you'd be an artist you proly wouldn't be reading this anyway. Now, I'm not really into WYSIWYG so I searched a little more and discovered a life savior: The Tikz-er2 LaTeX package.

Not wanting to bore you more than what's strictly needed to communicate my joy, here is a complete example almost right from the documentation.

Neat isn't it?

Well, If you are still interested you should take a look at the
package documentation. It's quite short, just a few pages
long. For the time being and to aid your experiments here
is the LaTeX source for the above displayed diagram:



\tikzstyle{every entity} = [top color=white, bottom color=blue!30,
draw=blue!50!black!100, drop shadow]
\tikzstyle{every weak entity} = [drop shadow={shadow xshift=.7ex,
shadow yshift=-.7ex}]
\tikzstyle{every attribute} = [top color=white, bottom color=yellow!20,
draw=yellow, node distance=7em, drop shadow]
\tikzstyle{every relationship} = [top color=white, bottom color=red!20,
draw=red!50!black!100, drop shadow]
\tikzstyle{every isa} = [top color=white, bottom color=green!20,
draw=green!50!black!100, drop shadow]

\begin{tikzpicture}[node distance=8em, every edge/.style={link}]
\node[entity] (persona) {Persona};
\node[attribute] (pid) [left of=persona] {\key{id\_persona}} edge (persona);
\node[attribute] (name) [below left of=persona] {nombre} edge (persona);
\node[multi attribute] (fono) [above of=persona] {fono} edge (persona);
\node[attribute] (anex) [right of=fono] {anexo} edge (fono);
\node[attribute] (number) [below left of=fono, node distance=5em] {number} edge (fono);
\node[attribute] (email) [above right of=persona] {email} edge (persona);
\node[relationship] (pertenece) [right of=persona] {Pertenece} edge node[auto,swap] {1:1} (persona);

\node[entity] (depto) [right of=pertenece] {Depto} edge node[auto,swap] {0:N} (pertenece);
\node[attribute] (iddepto) [above of=depto] {\key{id\_depto}} edge (depto);
\node[attribute] (dname) [above right of=depto] {d\_name} edge (depto);
\node[rectangle, draw=black, fit=(pertenece), inner sep=0em] (ipertenece) {};
\node[relationship] (tieneun) [below of=pertenece] {Ocupa} edge node[auto,swap] {1:1} (pertenece);

\node[entity] (cargo) [right of=tieneun] {Cargo} edge node[auto,swap] {0:N} (tieneun);
\node[attribute] (idcargo) [right of=cargo] {\key{id\_cargo}} edge (cargo);
\node[attribute] (cname) [above right of=cargo] {c\_name} edge (cargo);
\node[attribute] (plevel) [below right of=cargo] {nivel} edge (cargo);

\node[relationship] (responde) [below of=persona] {Responde} edge node[auto,swap] {1:N} (ipertenece);

\node[entity] (pregunta) [below of=tieneun] {Pregunta};
\node[attribute] (idpreg) [below of=pregunta] {\key{id\_preg}} edge (pregunta);
\node[attribute] (ptexto) [below right of=pregunta] {p\_texto} edge (pregunta);
\node[attribute] (pnumber) [right of=pregunta] {p\_numero} edge (pregunta);

\node[relationship] (tiene) [below of=responde] {Tiene} edge node[auto,swap] {1:N} (pregunta) edge node[auto,swap] {0:N} (responde);
\node[rectangle, draw=black, fit=(tiene), inner sep=0em] (itiene) {};

\node[entity] (alternativa) [left of=tiene] {Alternativa} edge node[auto,swap] {1:1} (tiene);
\node[attribute] (idalt) [above of=alternativa] {\key{id\_altern}} edge (alternativa);
\node[attribute] (atexto) [below of=alternativa] {a\_texto} edge (alternativa);
\node[attribute] (anumber) [below right of=alternativa] {a\_numero} edge (alternativa);

Hope it helps.


Anonymous said...

This was exactly what I was looking for. I have to do some (moronic) A-level computing coursework, and now I can do it all in TeX and make it less painful. Thanks!

Macy Dalby said...

Thanks this info was really helpful! I used a website called Lucidchart for drawing er diagrams and it was really easy to understand. If you use diagrams often you should check it out!