Labels

Apache Hadoop (3) ASP.NET (2) AWS S3 (2) Batch Script (3) BigQuery (21) BlobStorage (1) C# (3) Cloudera (1) Command (2) Data Model (3) Data Science (1) Django (1) Docker (1) ETL (7) Google Cloud (5) GPG (2) Hadoop (2) Hive (3) Luigi (1) MDX (21) Mongo (3) MYSQL (3) Pandas (1) Pentaho Data Integration (5) PentahoAdmin (13) Polybase (1) Postgres (1) PPS 2007 (2) Python (13) R Program (1) Redshift (3) SQL 2016 (2) SQL Error Fix (18) SQL Performance (1) SQL2012 (7) SQOOP (1) SSAS (20) SSH (1) SSIS (42) SSRS (17) T-SQL (75) Talend (3) Vagrant (1) Virtual Machine (2) WinSCP (1)

Friday, June 8, 2012

Reverse engineer or design a database with Microsoft Visio - Atlanta .NET Regular Guys

Reverse engineer or design a database with Microsoft Visio

How to use Microsoft Visio for Enterprise Architects SP-2 to reverse engineer your database to get nice drawings, and how to turn nice drawings into actual database objects.
Part 1: Reverse Engineer your database

Old database diagram




















First up you simply open Visio and create a new Database Model Diagram
This gives you a database menu option in the menu bar with several helpful commands. There are other database modeling types like ER diagrams and ORM diagrams, but those don’t allow you to make changes to your diagram and update the database or generate new databases later. Now that you have your new, blank document we can use the Reverse Engineer Wizard to connect to a database and generate our drawing automatically. On the menu bar, select DatabaseReverse Engineer to launch the wizard. The first step in the Reverse Engineer Wizard is to use your ODBC data sources to find the database.  If you don’t already have an ODBC connection for your database, you can create a new one inside of the wizard. Use the screenshots at the bottom of this article to guide you through creating a new User Data Source for use with Visio.

New database model diagram











Once you have selected your database connection object, pick which types to reverse engineer (tables, keys, indexes, etc) and then pick the objects you want to reverse engineer. You are first presented with a checkbox list of tables and views followed by a checkbox list of stored procedures. Finally you are given the option to allow the system to add the shapes to the page or to add the shapes manually. I recommend allowing the system to do the work. The last screen you are presented is basically a review of your selections. Click Finish to have the system design the drawing. The resulting drawing will have all the primary keys, foreign keys, relationships, etc. When you click on an object such as a a database table you’ll get the ability to look into the table’s design; column names, indexes, triggers, etc. Here is an example of just such a view.

tables or views to reverse engineertypes to reverse engineer












Database properties









 Part 2: Generate a database from a drawing

Now let’s take a look at things from the other side. In my particular case I felt that I needed to start from scratch to fully redesign my database, so I began with a blank Database Model Diagram and built up each table individually. To design a new table, drag an Entity object from the Shapes toolbox onto the drawing page and modify its database properties as I have done in this screenshot. To create a relationship between two tables, drag a relationship object from the Shapes toolbox onto the drawing page. Drag the arrow end onto the parent table and the other end onto the other table.

New Table






New Relationship







 Visio will attempt to automatically identify the relationship between your tables based on the conceptual names of your tables’ fields.  This example shows just such a relationship

New Relationship with association details












I worked on my table and relationship details until I had this design. I was able to normalize the data, reuse some existing tables, and basically reform the data architecture underneath this application to something that seemed to be a lot more reasonable. Now, with this kind of a table layout, I’m properly positioned to use custom business objects or an OR Mapper like the one from Paul Wilson. The old architecture made that choice difficult if not impossible.
Now that I have my nice new database object design, I want to create these tables. Actually, what I really want is the DDL script I can edit and use in any database to generate these tables. In my case, some of the tables represented in my drawing pre-exist and don’t need to be changed in any way. To do this, I go back to the handy Database menu and select Generate from the list. The first thing that will happen is that the model will be checked for any errors and warnings – things like similar field names with different data types or mismatched relationships. If I have any errors I must fix them first, much like in Visual Studio. 



Promo_NewERD











Once I pass the error checking, I am presented with the Generate Wizard – . The Generate wizard allows me to chose a file location for a DDL script or I can actually have Visio connect to the database engine for me and create a brand new database. Chose to generate the script and click next to get to the database specification portion. Here you can pick which type of database rules you’re going to follow, SQL Server, Access, Oracle, DB2, etc. Pick SQL Server, give your database a name for use in the script and click Next.

GenerateWizard Generate Wizard step 2 





You will be presented with a review of everything you are about to export.  Press next to do an automatic physical review of your model against that database’s rules.  Once the validation is completed you can press Finish to generate the DDL file.  Even though you might not have selected to generate the new database, you might be presented with a SQL Server Create Database dialog.  Click Close on this dialog to get to the option to view the generated DDL script in the Visio code editor.  Here is a screenshot of that generated code:

 CodeEditor

Now that you have this code file you can modify it and run it in query analyzer (yes – I’m still running SQL 2000) against any database you like.  The best part is that you can use a tool like Visio to maintain your database.  You can easily reverse engineer a subset of tables in a database, check on relationships, etc.  You can make modifications and persist those changes back to the database.  This is an easy way to learn about your database at a glance.  There is an Update command on the Visio database menu that I didn’t go into.  This command allows you do generate change scripts, detect differences, log errors, etc.  All in all, Visio can be an important tool in your toolbox.

1 comment:

  1. Hi Mahadevan,
    Can you please reupload all these lost images ?

    ReplyDelete