Fork me on GitHub

Powerdesigner使用技巧

数据模型概念

ConceptualDataModel(概念数据模型)

        概念数据模型(CDM),可以帮助你分析信息系统的概念结构,识别主要的实体,及其属性,以及它们之间的关系。CDM比逻辑(LDM)或物理数据模型(PDM)更抽象。
        基于需求综合、归纳、抽象后对数据和信息进行建模,利用实体关系图(E-R图)的形式组织数据。
        CDM反映了业务领域中信息之间的关系,它不依赖于物理实现。
        CMD不考虑物理实现细节,只考虑实体之间的关系。
        目的:统一业务概念,方便业务人员与技术人员沟通。
        分析阶段的CDM转换成PDM后,便将抽象的实体、属性与关系,对应到实际数据库的数据表、字段、主键、外部索引键等内容。

LogicalDataModel(逻辑数据模型)

        逻辑数据模型(LDM)可以帮助你分析信息系统的结构,独立于任何特定的物理数据库实现。LDM已确定实体标识符,没有概念数据模型(CDM)抽象,但不允许你建视图模型,索引等具体的物理数据模型(PDM)元素。
        逻辑模型是对概念数据模型的进一步细化与分解
        形成DBMS所支持的数据结构(一般是关系数据模型)
        既要面向业务用户,又要面向系统
        影响数据库设计方案选择

PhysicalDataModel(物理数据模型)

        物理数据模型(PDM)可以帮助你分析表,视图和其他数据库对象,包括数据仓库的多维对象。 PDM的是更具体的一个概念(CDM)或逻辑(LDM)的数据模型。你可以为所有常用的DBMS建模、反向工程、生成数据库。
        基于特定DBMS,在概念数据模型、逻辑数据模型的基础上进行设计。
        PDM叙述数据库的物理实现。主要目的是把CDM中建立的现实世界模型生成特定的DBMS脚本,产生数据库中保存信息的储存结构,保证数据在数据库中的完整性和一致性。

新建数据模型

新建数据库

文件 -> 新建模型 -> Categories -> Information -> Physical Data -> 输入模型名称,DBMS选择Mysql5.0

显示自定义工具栏

工具 -> 自定义工具栏,在弹出窗口中选中Palette。

新建表

右击数据库名 -> New -> Table

列注释显示

设置主键自增长

选中column,右击Properties,勾上Identity。

设置唯一索引

Table Properties -> Indexes -> 输入唯一索引名称并勾上unique -> Columns -> add Columns给唯一索引加字段

 

必备三字段

id(主键)、sys_create(datetime,记录创建时间)、sys_modified(datetime,记录更新时间)

id:类型为unsigned int(10),设为主键、自增。

sys_create:类型为datetime,默认值为CURRENT_TIMESTAMP,在创建是默认当前时间。

sys_modified:类型为datetime,默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,在更改记录时更新时间。

is_delete:类型为unsigned tinyint(10),表示是否已删除。

建立多对多关系

Palette面板: tools->customize toolbars->palette

建立此关系即建立了外键。其他一对一、一对多关系同理。

设置父表子表,点击Reference的时候从子表往父表上拉。

设置关联字段

设置对应关系,以及级联关系。

CASCADE:父表delete、update的时候,子表会delete、update掉关联记录;
SET NULL:父表delete、update的时候,子表会将关联记录的外键字段所在列设为null,所以注意在设计子表时外键不能设为not null;
RESTRICT:如果想要删除父表的记录时,而在子表中有关联该父表的记录,则不允许删除父表中的记录;
NO ACTION:同 RESTRICT,也是首先先检查外键;

cardinality

0..1:一个父表记录对应0或1个子表记录

0..*:一个父表记录对应0到n个子表记录

1..1:一个父表记录对应1个子表记录

1..*:一个父表记录对应1到n个子表记录

逆向工程

根据Mysql库逆向

安装MYSQL的ODBC驱动,因为PowerDesigner连接MySQL,需要中间纽带,MySQL Connector/ODBC相当于中间商一样,既可以像mysql沟通,也可以与PowerDesigner沟通。

Connector/ODBC 5.1.13下载地址:dev.mysql.com/downloads/connector/odbc/5.1.html,根据32位系统或64位系统下载相应安装包。(需要登录)

安装好后,在ODBC数据源中中添加Mysql ODBC Driver。

开始逆向生成,向PowerDesigner中导入MySQL数据库。

Database - > Connect。选择数据库,输入密码。

File -> Reverse Engineer -> Database

选择Mysql数据源

连接后一定要先全部取消deselect all,然后在选择库再勾上要逆向的表。

根据SQL文件逆向

File -> Reverse Engineer -> Database

和从库中导入不同的是这里选择脚本文件。

导出数据库

导出SQL

Database - > Generate Database (Ctrl + G)

输入目录和文件名

导出到库

直接导出到库中,

其他技巧

1.设置name不映射code

Tools --》General Options --》dialog,取消name到code映射

2.生成的时候自动把name更新到comment列

3.comment设为name脚本

'使用方法使用方法
'PowerDesigner->Tools->Execute Commands->Edit/Run Scripts

可以保存该脚本为:comment2name.vbs

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 

4.将name设为comment脚本

'使用方法使用方法
'PowerDesigner->Tools->Execute Commands->Edit/Run Scripts

可以保存该脚本为:name2comment.vbs

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

5.删除外键时会删掉父表的id

Tool->Model Options->Model Settings->Reference中去掉Auto-migrate columns即可

 6.Database->Generate Database生成的sql语句中会在drop table之前有一个drop primary key的操作

去掉删除主键的操作

显示表一对一一对多关系

Tool -> Display Preferences -> Reference 中 Cardinality勾上

其他问题

mysql脚本字段多个national关键字

Database -> Edit Current DBMS -> General -> Profile -> Column -> Extended Attributed - > National,勾上Computed

 

posted @ 2018-05-15 16:08  秋夜雨巷  阅读(766)  评论(0编辑  收藏  举报