1、物理数据模型 PDM
物理数据模型(Physical Data Model)PDM,提供了系统初始设计所需要的基础元素,以及相关元素之间的关系;数据库的物理设计阶段必须在此基础上进行详细的后台设计,包括数据库的存储过程、操作、触发、视图和索引表等。
PowerDesigner能够用于创建多种不同类型的DBMS(数据库管理系统),每种类型的DBMS都包含一个标准定义的文件用于在PD和DBMS中确定彼此的关联而提供一套接口;创建不同的DBMS可以生成不同的数据库脚本。
物理数据模型(PDM)的主要功能: 1)可以将数据库的物理设计结果从一种数据库移植到另一种数据库; 2)可以通过反向工程将已经存在的数据库物理结构重新生成物理模型或概念模型; 3)可以定制生成标准的模型报告; 4)可以转换为面向对象模型(OOM); 5)完成多种数据库的详细物理设计(涵盖常用的各种数据库的DBMS),并生成数据库对象的sql脚本。
2、新建物理数据模型
1)文件 —> 新建模型 —> 物理数据模型
File —> New Model —> Physical Data Model
填写模型名称(Model name),选择数据库类型(DBMS),点击"OK"
2)Toolbox下的Physical Diagram 中鼠标单击Table图标,放在设计区域面板中,点击一下,即可生成一张表
3)点击Toolbox下的Pointer图标,回到设计区域面板,双击表,可以修改表的信息
4)修改表名、表注释,增加表字段,包括字段名称、类型、注释、是否主键等属性。
以班级、学生为例
5)学生表中添加班级表外键 双击学生表,点击columns,班级id字段上勾选“Mandatory”,表示不能为空,应用并关闭。
字段 | 含义 |
---|---|
Mandatory | When selected, indicates a column that must be assigned a not null value |
6)点击Toolbox下的Physical Diagram中的Reference,将两个实体连接起来。
注意:箭头指向父表,即学生表中存在班级id外键,则箭头指向班级表。 默认外键是加在子表的主键上,但实际上我们要求是班级id作为外键。
7)双击连接线,弹出窗口,选择"Joins",其中Parent Table Column默认为父表主键,点击应用,确定后,学生表中标志class_id为外键。
Power Designer之前的版本,要求Praent Key必须选为none,否则学生表的外键是学生表主键和班级id。
8)这样一个简单的物理数据模型就创建完了。
tips:在设计区域面板中,我们还可以自定义展示的字段列。点击右键,选择Display Preferences,选择Table,右侧点击Advanced...,然后选择Columns,点击List Columns右侧小图标,通过勾选,自定义显示的列内容。
9)物理数据模型创建完成后,下一步就是需要导入到数据库中供我们使用了。 我们可以通过PowerDesigner导出sql脚本。
10)点击:数据库 —> 生成数据库
Database —> Generate Datebase
11)弹窗中,设置文件生成路径和文件名称
名字 | 含义 |
---|---|
Directory | 文件路径 |
File name | 文件名称 |
12)弹窗切换至选项(Selection)页签,选择要导出的表
13)点击确定后,即开始生成SQL文件。生成完成后,弹出框中,点击Edit,即可查看SQL文件。
14)PowerDesigner支持生成多种数据库文件,生成的SQL文件支持的数据库是由新建物理数据模型时选择的数据库类型决定的。
比如,我们第1)步选择的是MySQL5.0,则生成的SQL文件也是支持MySQL5.0的。
15)我们可以导出数据库文件,同时我们也可以根据SQL文件,生产PDM。 文件 —> 反向工程 —> 数据库 File —> Reverse Engineer —> Database
16)填写模型名称(Model name),选择数据库来源(DBMS),要和导出SQL文件的数据库一致
17)确定后,弹窗,点击添加文件,选择要导入的SQL文件
18)点击确定,即可导入
19)生成的模型中,点击表,查看字段,发现name和id都是英文,但在面板中,我们看到的是name,这就需要我们手动去修改字段的name值。但是,我们每次修改,id也会跟着变,就需要把id一个个的再重新修改。 我们有没有什么合适的方式进行批量修改呢?
20)我们可以运行脚本,来进行统一修改 Tools —> Execute Commands —> Edit/Run Scripts 或者Ctrl + Shift + X 快捷键,打开运行脚本弹窗
复制脚本内容,点击Run
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
21)完成后,我们即可看到name列填充的是comment列内容了。当然我们也可以将name列内容直接写入至comment列。
说明:如果comment为空,则填入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
' 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
if trim(tab.comment)="" then '如果有表的注释,则不改变它;如果没有表注释,则把name添加到注释中.
tab.comment = tab.name
end if
Dim col ' running column
for each col in tab.columns
if trim(col.comment)="" then '如果col的comment为空,则填入name;如果已有注释,则不添加.这样可以避免已有注释丢失.
col.comment= col.name
end if
next
end if
next
Dim view 'running view
for each view in folder.Views
if not view.isShortcut and trim(view.comment)="" 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
22)这其实也方便了我们自己新建表,添加字段时,不需要重复填写name和comment。采用这两个脚本,可以直接name和code采用英文编码,注释采用中文编码,提高效率。