根据源数据库导出的SQL文件生成物理模型:file-reverse engineer-database,DBMS选择SQL文件的来源数据库类型,确定,use script files可以添加多个sql文件。读取SQLServer导出的SQL文件应特别注意:SQLServer导出的SQL可能包含alter table add column的语句,此类语句默认不会以go分割,在导入时PowerDesigner将go视为分隔符,将无法读到这些alter语句,导致模型中缺失这些字段,解决方法是将"alter table"替换为";alter table",即补上分隔符。
根据已有模型生成目标数据库的物理模型:tools- generate PDM,DBMS选择目标数据库的类型,selection中可以选择导入哪些表。
手动调整模型
- 修改用户:model-user and role,删除原有用户,创建新用户scott,为下一步做准备
- 修改表名、索引名:model-tables,最大化后为name逐个加上前/后缀,将表的owner都改为scott;model-indexes,最大化后为name逐个加上前/后缀,将表的owner都改为scott
- 去除导出SQL的双引号:database-edit current DBMS-general-script-sql-format-CaseSensitivityUsingQuote,选择no(以管理员身份运行PD可以保存该设置到安装目录的文件中)
- 加字段:在第一个表中手动加字段,选中新加的字段,右键-edit-copy,双击其他表-columns,选到最后一行-ctrl+v,即可为每一行批量加字段
生成SQL:database-generate database,general-设置导出路径,options-去除database/user/role的导出勾选,去除table/key/index的drop语句,去除format-title的勾选可以不生成开头的注释。(去除check model选项可以防止导出时报错)
自动排列:symbol-auto layout
执行VB脚本,自动调整模型:tools-execute command-edit/run script
dim model
set model = ActiveModel
mainProcess model
'主程序
'*****************************************************************************
sub mainProcess(model)
dim postfix
'修改表所有者
s_ChangeTableOwner model,"dw_stage"
postfix="_tgfwpt"
for each table in model.tables
'增加字段
s_AddColumn table,"ods_clt_date","DATE"
s_AddColumn table,"modifier_no","VARCHAR2(20)"
'为表名加前后缀
s_ChangeTableName table,"",postfix
'修改索引名称
s_ChangeIndexName table,"",postfix
'修改键名称
s_ChangeKeyName table,"",postfix
'修改唯一索引为普通索引,防止插入失败
s_ChangeIndexType(table)
'去除字段的非空限制
s_ChangeColumnDataType table
'修改特定类型的字段为另一种类型,异构数据库转换时会用到
's_ChangeColumnDataType table,"BIGINT","NUMBER(19,0)"
's_ChangeColumnDataType table,"INT","NUMBER(10,0)"
'删除特定名称的字段
's_DeleteColumn table,"modifier_no"
next
end sub
'功能:增加字段
'*****************************************************************************
sub s_AddColumn(table,colname,coltype)
dim col
Set col=table.Columns.CreateNew
col.name = colname
col.code = colname
'col.comment = ""
col.DataType = coltype
end sub
'功能:删除字段
'*****************************************************************************
sub s_DeleteColumn(table,colname)
dim col
for each col in table.Columns
if col.name=colname then
col.delete
end if
next
end sub
'功能:为表名加前后缀
'*****************************************************************************
sub s_ChangeTableName(table,pre,post)
table.name=pre+table.name+post
table.code=pre+table.code+post
end sub
'功能:修改特定类型的字段为另一种类型
'*****************************************************************************
sub s_ChangeColumnDataType(table,fromType,toType)
dim col
for each col in table.Columns
if(UCase(col.dataType)=fromType) then
col.dataType=toType
end if
next
end sub
'功能:去除字段的非空限制
'*****************************************************************************
sub s_ChangeColumnDataType(table) on error resume next
'主键列取消非空会报错,上面的设置为忽略错误
dim col
for each col in table.Columns
col.Mandatory=False
next
end sub
'功能:创建所有者
'*****************************************************************************
sub s_TouchUser(model,userName)
dim flag
flag=1
for each u in model.Users
if u.Code = userName then
flag=0
exit for
end if
next
if flag=1 then
Set usr=model.Users.CreateNew
usr.name=userName
usr.code=userName
end if
end sub
'功能:修改表的所有者
'*****************************************************************************
sub s_ChangeTableOwner(model,owner)
'需要先创建该名称的用户
s_TouchUser model,owner
'获取用户对象
for each usr in model.Users
if usr.Code = ownerName then
exit for
end if
next
for each usr in model.Users
if usr.Code = owner then '需要先在PDM内新建一个该名称的用户
for each n in model.Tables
set n.Owner = usr
next
exit for
end if
next
end sub
'功能:修改索引名称
'*****************************************************************************
sub s_ChangeIndexName(table,pre,post)
for each inx in table.indexes
inx.name= pre+inx.name+post
inx.code= pre+inx.code+post
next
end sub
'功能:修改唯一索引为普通索引,防止插入失败
'*****************************************************************************
sub s_ChangeIndexType(table)
for each inx in table.indexes
inx.Unique=false
next
end sub
'功能:修改键名称
'*****************************************************************************
sub s_ChangeKeyName(table,pre,post)
for each k in table.keys
k.name= pre+k.name+post
k.code= pre+k.code+post
k.ConstraintName=pre+k.ConstraintName+post
'处理约束名为空的情况
if k.ConstraintName=post then
k.ConstraintName="PK_"+table.name
end if
next
end sub
导出表结构到excel:
Option Explicit
ValidationMode = True
InteractiveMode = im_Batch
Dim mdl
Set mdl = ActiveModel
Dim EXCEL,sheet,rowsNum
rowsNum = 1
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
Else
SetExcel
ListObjects(mdl)
End If
Private Sub ListObjects(fldr)
output "Scanning " & fldr.code
Dim obj
For Each obj In fldr.children
DescribeObject obj
Next
Dim f
For Each f In fldr.Packages
ListObjects f
Next
End Sub
Private Sub DescribeObject(CurrentObject)
if not CurrentObject.Iskindof(cls_NamedObject) then exit sub
if CurrentObject.Iskindof(cls_Table) then
ExportTable CurrentObject, sheet
else
output "Found "+CurrentObject.ClassName+" """+CurrentObject.Name+""", Created by "+CurrentObject.Creator+" On "+Cstr(CurrentObject.CreationDate)
End if
End Sub
Sub SetExcel()
Set EXCEL= CreateObject("Excel.Application")
EXCEL.Visible = True
EXCEL.workbooks.add(-4167)'添加工作表
EXCEL.workbooks(1).sheets(1).name ="pdm"
set sheet = EXCEL.workbooks(1).sheets("pdm")
sheet.Cells(rowsNum, 1).Value = "表名"
sheet.Cells(rowsNum, 2).Value = "表中文名"
sheet.Cells(rowsNum, 3).Value = "表注释"
sheet.Cells(rowsNum, 4).Value = "字段ID"
sheet.Cells(rowsNum, 5).Value = "字段名"
sheet.Cells(rowsNum, 6).Value = "字段中文名"
sheet.Cells(rowsNum, 7).Value = "字段类型"
sheet.Cells(rowsNum, 8).Value = "字段注释"
End Sub
Sub ExportTable(tab, sheet)
Dim col,table_comment_flag
Dim colsNum
table_comment_flag=0
colsNum = 0
for each col in tab.columns
colsNum = colsNum + 1
rowsNum = rowsNum + 1
sheet.Cells(rowsNum, 1).Value = tab.code
sheet.Cells(rowsNum, 2).Value = tab.name
if(table_comment_flag=0) then
sheet.Cells(rowsNum, 3).Value = tab.comment
table_comment_flag=1
end if
sheet.Cells(rowsNum, 4).Value = colsNum
sheet.Cells(rowsNum, 5).Value = col.code
sheet.Cells(rowsNum, 6).Value = col.name
sheet.Cells(rowsNum, 7).Value = col.datatype
'On Error Resume Next
sheet.Cells(rowsNum, 8).Value = col.comment
next
output "Exported table: "+ +tab.Code+"("+tab.Name+")"
End Sub
修改数据库类型,自动适配字段类型
database-change current dbms