power designer 使用问题小汇总
最近在使用power designer,用于建数据表,建表可视化挺方便建表的操作,在整理建表思路的同时就完成的表的创建,程序员表示用得很爽,但是功能挺庞大的,一开始用很容易摸不清头脑,要多熟悉,总结一下今天遇到的问题。
常见的问题:建立好数据表模型后,导出对应的sql脚本,用导出的脚本进行创建表等操作时常常会遇到很多问题:
① 导出sql语句时有很多无用的语句,或者说暂时用不上的语句生成,因为建了数据库模型之后,我们需要使用工具(PL/SQL Developer)在数据库中创建出实体表去装载数据使用,直接使用却出现了很多错误,很多暂时不需要的语句生成(其实是不知道有什么作用),如,对触发器的操作(即使没有创建触发器)或者开始结束脚本。。。
后来我看了下导出sql的设置,才发现我导出了不需要的脚本代码,一下应该是不需要的,因为我只需要序列,建表语句或者外键主键之类的,其他的这个时候就不需要了
解决方法:
1. 首先通过
进行导出设置,其中设置 directory(生成sql脚本存放的本地地址)和file name(文件名)就可以了(只需要建表语句和索引等语句时)
2.
导出需要的建表语句,也可以在options自定义需要哪些脚本代码
还有几个小问题,小汇总一下:(解决方法在下方)
② 导出的表名和所有字段名都被包裹了双引号,需要去除,无法直接使用
③ 定义表的字段的name(显示与PD内的字段说明文字)时,默认与code(创表时的字段名)实时更新,实际上这步是多余的,因为创表语句中我们一般不使用中文进行命名,无论是表名还是字段,虽然可以用中文去命名,但是不符合规范并且程序员应该尽量远离中文命名,因为鬼知道会发生什么事情!
所以我们希望真正需要中文说明的comment(说明)与name属性对应,就免去我们复制粘贴的麻烦
④
②:导出的表名和所有字段名都被包裹了双引号,需要去除,无法直接使用
为什么会出现这样的问题呢?
使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号。其实加引号是PL/SQL的规范,数据库会严格按照“”中的名称建表,如果没有“”,会按照ORACLE默认的设置建表(DBA STUDIO里面),默认是全部大写,这样,在ORACLE数据库里的字段就如“Column_1”。
如果你把引号去掉,ORACLE自动默认为全部大写,即“COLUMN_1”,所以这段SQL在PL/SQL中执行的时候是没有任何问题的,如果不加引号,在PL/SQL会自动识别为大写。
如果加了引号,sql或者hql查询“Column_1='XXX'”时,就会报错:ORA-00904: "COLUMN_1": 无效的标识符,除非写为“‘Column_1’='XXX'”。
这个问题是生成脚本格式的问题,因此,我们可以尝试在DBMS配置文件中修改相应的格式设置来解决这个问题。
在power designer菜单中的Database下Edit current DBMS中,
选择Script->Sql->Format,有一项CaseSensitivityUsingQuote,
它的comment为“Determines if the case sensitivity for identifiers is managed using double quotes”,
表示是否适用双引号来规定标识符的大小写,可以看到右边的values默认值为“YES”,改为“No”,点击【应用】按钮。
参考自:https://www.2cto.com/database/201301/186634.html
③:使用pd对name和comment互相转换,减少不必要的操作
在PowerDesigner中使用方法为:
PowerDesigner->Tools->Execute Commands->Edit/Run Scripts
将代码Copy进去执行就可以了,是对整个CDM或PDM进行操作
代码一:将Name中的字符COPY至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 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
另外在使用REVERSE ENGINEER从数据库反向生成PDM的时候,PDM中的表的NAME和CODE事实上都是CODE,为了把NAME替换为数据库中Table或Column的中文Comment,可以使用以下脚本:
代码二:将Comment中的字符COPY至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 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
参考自:https://www.cnblogs.com/galengao/p/5756565.html
④原因是由于generation option 设置出错导致
解决方法:
打开表格属性页面Priview标签, 有个Show generation Options按钮
或
Ctrl+G 弹出设置界面
将自定义的Setting Set 选择为All Objects 默认方案或 Table And View 方案 -> 应用 一般可解决 SQL 预览出现。
原因是由于generation option 设置出错导致
解决方法:
打开表格属性页面Priview标签, 有个Show generation Options按钮
或
Ctrl+G 弹出设置界面
将自定义的Setting Set 选择为All Objects 默认方案或 Table And View 方案 -> 应用 一般可解决 SQL 预览出现。