SQL Diagram creation

From OpenPetra Wiki
Revision as of 09:44, 1 February 2012 by Christiankatict (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


What we wanted to achieve with this task has now been achieved by other means (using the open source SchemaSpy tool). The output can be found here: [1]

This wiki page is therefore left for reference information only.


This page describes the sql2diagram tool, and how to use it for OpenPetra.org.

We used to have this project description:

  • Design some database diagrams
    • Please also have a look at the old Petra 2.1 diagrams (download 9 png files in a zip file). The diagrams might need to be a bit smaller and more specific, otherwise they get too big and complicated.

Now that we are using SchemaSpy which autogenerates diagrams depending on their first and second degree related tables, we won't need any manual diagrams anymore.


We wrote this tool a while ago in C/C++ and uploaded it to sourceforge: http://sql2diagram.wiki.sourceforge.net/


  • it reads the SQL tables and attributes from the SQL file with all the CREATE TABLE statements
  • it generates HTML documentation
  • it is able to generate diagrams for the DIA program (http://live.gnome.org/Dia)
  • it generates an HTML map for the diagram png file, and links to it from the HTML documentation

The generation of the HTML documentation works fine.

For the current results, check http://openpetraorg.sourceforge.net/dbdoc; There is a diagram for the Accounts Payable sub system: http://openpetraorg.sourceforge.net/dbdoc/img/img_AccountsPayable.html?a_ap_document


There are some problems with generating the diagrams. On the one hand it allows manual moving of the tables and connections in the dia editor, and the sql2diagram tool should pick up the manual positions and reuse them. But on the other hand, it is quite tedious to move the tables and foreign key constraints all by hand.

Task 1: Use the new Database tools, instead of the ER shapes

Database shapes were only added recently to dia, and might look better than the ER shapes we are using currently.

Issues to consider: how to show which fields are part of a foreign key? At the moment, we print FK at the back of the row that describes the field name and type.

Task 2: Do an automatic layout of the tables and connections

Some discussions and first hints:

So my approach to Task 2 would be to use some good algorithms when arranging the diagram, and make sure the generated diagram has the tables in the best position (most referenced tables in the centre, tables that are most linked to each other closest to each other), and the linking constraints are not crossing any tables, and are still possible to be read.

How to use sql2diagram for OpenPetra.org

See a sample result here: diagram for the Accounts Payable sub system

See the file themed.prj in OpenPetra\db\doc; that contains already a collection of tables. You might also copy the table names from the file master_alltables.prj in the same directory.

Then you run nant dbdoc to generate the diagram file (extension .dia). You then can edit that with the dia editor (available for Linux and Windows). First, the tables are all on the invisible layer. See the instructions on the sql2diagram page how to copy the tables to the front layer, and then to arrange the tables. Next time you run nant dbdoc the constraint lines will be inserted. Please try to make them look ok by moving the lines and inserting new segments.

List of alternatives to sql2diagram

The following list of alternatives (from here and here) could be evaluated for ease of use instead of sql2diagram:

Listed earlier: