PowerDesigner逆向生成表结构
PowerDesginer逆向工程提供两种导入表结构的方式:
- Using script files:使用脚本文件,即通过其他软件到处数据库表结构的
.sql文件
- Using a data source:使用一个数据库源,直接连接数据库进行导入
Using script files
-
从Navicat导出.sql文件,该文件保存了数据库表结构。
这里不再阐述Navicat导出.sql文件的具体操作。
以下为导出的实例sql语句:
SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `account`; CREATE TABLE `account` ( `accountid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '账户标识' , `userid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户标识' , `accountrole` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '账户角色' , `balance` decimal(20,2) NULL DEFAULT 0.00 COMMENT '账户余额' , `availablebalance` decimal(20,2) NULL DEFAULT 0.00 COMMENT '可用余额' , `platform` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '平台标识' , `tenantid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `lastupdateduserid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `lastupdatedstamp` datetime NULL DEFAULT NULL , `createduserid` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `createdstamp` datetime NULL DEFAULT NULL , `deleted` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , PRIMARY KEY (`accountid`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci;
注意:我们需要对该表进行适当修改,才能使得PowerDesigner正确的读入注释信息,否则大部分的注释将丢失。
修改一:去掉各个字段和表的字符集设置和查询校验规则:
SET utf8 COLLATE utf8_general_ci
修改二:去掉表引擎设置:
ENGINE=InnoDB
-
将sql文件导入到PowerDesigner
这里我采用了两种导入方式:
方式一:
点击File>>Reverse Engineer>>Database…
方式二:
点击DataBase>>Update Model from Database
现在导入的表已经包含了注释信息,但是我们从表图形上依然看不到注释信息。
表内部数据:
表显示界面:
- 将注释信息显示在表上
点击Tools>>Execute Commands>>Edit/Run Script
显示如下图:
输入以下两种脚本执行实现数据替换:
将Comment数据替换为Name中数据:
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
Dim mdl ' the current model
' get the current active model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no current Model "
ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then
MsgBox "The current model is not an Physical Data model. "
Else
ProcessFolder mdl
End If
' This routine copy name into comment for each table, each column and each view
' of the current folder
Private sub ProcessFolder(folder)
Dim Tab 'running table
for each Tab in folder.tables
if not tab.isShortcut then
tab.comment = tab.name
Dim col ' running column
for each col in tab.columns
col.comment= col.name
next
end if
next
Dim view 'running view
for each view in folder.Views
if not view.isShortcut then
view.comment = view.name
end if
next
' go into the sub-packages
Dim f ' running folder
For Each f In folder.Packages
if not f.IsShortcut then
ProcessFolder f
end if
Next
end sub
将Name数据替换为Comment数据:(我们需要达到的效果)
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
Dim mdl ' the current model
' get the current active model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no current Model "
ElseIf Not mdl.IsKindOf(PdPDM.cls_Model) Then
MsgBox "The current model is not an Physical Data model. "
Else
ProcessFolder mdl
End If
Private sub ProcessFolder(folder)
On Error Resume Next
Dim Tab 'running table
for each Tab in folder.tables
if not tab.isShortcut then
tab.name = tab.comment
Dim col ' running column
for each col in tab.columns
if col.comment="" then
else
col.name= col.comment
end if
next
end if
next
Dim view 'running view
for each view in folder.Views
if not view.isShortcut then
view.name = view.comment
end if
next
' go into the sub-packages
Dim f ' running folder
For Each f In folder.Packages
if not f.IsShortcut then
ProcessFolder f
end if
Next
end sub
利用第二种脚本实现效果如下:
Using a data source
数据库连接注意:
-
PowerDesigener连接数据库需要对应数据库驱动的支持,如果安装驱动后依然没有识别出对应驱动,则换一下驱动位数(32/64位)即可。
-
创建Physical Data Model
- 导入表结构
点击:File>>Reverse Engineer>>Database…
注意选对DBMS:第二行DNMS选项选择对应的数据库版本,如果导入后的表只显示表名二丢失字段、注释、数据类型等信息,往往是DBMS没有设置正确。
可以看到目前还没有连接任何数据库,因此点击Configure创建数据库连接
选择对应的数据库连接驱动
注意:如果没有对应的数据库驱动程序则需要自行安装,同时需要注意驱动程序的版本和位数(32/64)。如果安装位数不正确,PowerDesigener是识别不了的。
输入连接信息,测试是否连接成功
除了在PowerDesigener内部连接数据库,也可以不启动PowerDesigener连接:
首先打开:控制面板>所有控制面板选项>管理工具
图中分别用于设置32位和64位的数据源,其连接操作同上文操作一致
网络资源链接:
https://blog.csdn.net/chao_1990/article/details/52620206
https://www.cnblogs.com/leilong/p/8926318.html
https://blog.csdn.net/Demis_demis/article/details/83038715