Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an E-R Diagram by reverse-engineering a database [closed]

Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost any database which has a JDBC driver that can detect foreign-key associations.


Querying PostgreSQL data dictionary for foreign-keys and relationship between tables is very straightforward, but how can I use that information to generate a graph of the relations between tables?

Any recommendations about tools that can do this?

EDIT: I know GraphVIZ/DOT can be useful, however, I don't know have any idea regarding how to code an app that would generate the directed graph .DOT file.

like image 631
Camilo Díaz Repka Avatar asked Oct 09 '08 04:10

Camilo Díaz Repka


5 Answers

Dot is part of the graphviz package, which is a pretty damn cool/useful tool. Of course, you'll need something to generate the dot files for graphviz. I've used SchemaSpy once or twice in the past, and it works pretty well, provided you have the relationships defined in the database.

like image 86
Paul Wicks Avatar answered Sep 25 '22 05:09

Paul Wicks


Microsoft Visio will easily do this.

like image 28
Andrew Avatar answered Sep 22 '22 05:09

Andrew


At least for Oracle I run this query or ask the DBA to run it and send me the results. Results can be copied directly to a text file to be interpreted by Graphviz's tools, resulting in a database diagram.

SELECT '"' || Source.TABLE_NAME || '" -> "' 
           || Destiny.TABLE_NAME || '";' AS For_GraphViz
FROM dba_constraints Source
JOIN dba_constraints Destiny
ON Source.owner='my_db_owner' AND Destiny.owner='my_db_owner'
AND Source.CONSTRAINT_TYPE='R'
-- theoretically this validation should be redundant
-- AND Destiny.Constraint_type = 'P'
AND Source.R_CONSTRAINT_NAME = Destiny.CONSTRAINT_NAME
ORDER BY Source.TABLE_NAME, Source.CONSTRAINT_TYPE, Source.CONSTRAINT_NAME
    , Source.R_CONSTRAINT_NAME, Source.INDEX_NAME;

A similar query can be created easily for SQL Server, don't know about MySQL, PostgreSQL et al.

like image 36
Joe Pineda Avatar answered Sep 25 '22 05:09

Joe Pineda


Dot is multiplatform and may be useful.

like image 25
user20282 Avatar answered Sep 23 '22 05:09

user20282


DBVisualizer is also a free and nice alternative.

like image 41
Zeemee Avatar answered Sep 25 '22 05:09

Zeemee