PowerDesigner设置MYSQL主键自动增长的方法


PowerDesigner设置MYSQL主键自动增长的方法
2010年07月21日
  我一直用的是Oracle数据库,所以在设计CDM的时候,就没有选定表主键自动增长。这是SQL Server和MYSQL才有的方式。Oracle中一般是采用SEQUENCE的方式来处理主键增长的问题的。相对写程序来说,Oracle的处理方式比较复杂一点。
  打开PDM,选中一张表,双击打开表属性,选择Columns列,选中主键字段,双击打开,界面的最下方右下角有一个Identity,选中这个之后,生成的表主键就是自动增长的了。
  要一个表一个表的手工去修改。这要改到什么时候啊!
  在google上搜索了一下,发现居然有很多人碰到这个问题,还有一个vbs的脚本执行一次就解决了。大家可以去搜索一下,脚本名字叫SetIdentity.vbs。
  在PD中执行脚本方法也很简单,Tools--Excecute Commands--Edit/Run Scripts。
  另外还有一个脚本,是用来把字段的name统一设置为数据库字段的comments字段内容的name2comment.vbs代码。也很好用。(一般name都是中文描述,code就是代码)
  SetIdentity.vbs
  ''''********************************************** *******************************
  ''''文件:SetIdentity.vbs
  ''''版本:1.0
  ''''版权:floodzhu ([email protected]),2004.12.31
  ''''功能:遍历物理模型中的所有表,把是主键但不是外键的字段设置为Identity,适用于
  '''' 物理模型为MS Sql Server的类型。
  ''''用法:打开物理模型,运行本脚本(Ctrl+Shift+X)
  ''''备注:我有两个习惯,一个是把所有表的主键都定义为自增长的int类型,另一个是定义
  '''' 一个Domain叫ID,在设计概念模型时把所有的PrimaryKey字段的Domain设置为ID
  '''' 类型。
  ''''
  '''' 如果我进行了上面的设置,则在转化为物理模型时需要手工设置Identity,
  '''' 最笨的方法是一个表一个表进行设置,最简单的方法是在物理模型中直接对Domain
  '''' 进行设置。对Domain进行设置有一个小缺点,就是如果该字段不是主键也不是生
  '''' 成的外键,而是一个一般字段,例如表示树状结构的PID,则它也会被设置为
  '''' Identity,不过由于这种字段比较少,而且在生成数据库时会发生错误可以提醒
  '''' 你进行纠正,所以可以轻松过关而不至于隐藏错误,所以是一种好方法。
  ''''
  '''' 用下面的代码可以给你第三种选择,而不会发生任何错误。
  ''''********************************************** *******************************dim model ''''current model
  set model = ActiveModel
  If (model Is Nothing) Then
  MsgBox "There is no current Model"
  ElseIf Not model.IsKindOf(PdPDM.cls_Model) Then
  MsgBox "The current model is not an Physical Data model."
  Else
  ProcessTables model
  End If
  ''''********************************************** *******************************
  ''''函数:ProcessTables
  ''''功能:递归遍历所有的表
  ''''********************************************** *******************************
  sub ProcessTables(folder)
  ''''处理模型中的表
  dim table
  for each table in folder.tables
  if not table.IsShortCut then 
  ProcessTable table
  end if
  next
  ''''对子目录进行递归
  dim subFolder
  for each subFolder in folder.Packages
  ProcessTables subFolder
  next 
  end sub
  ''''********************************************** *******************************
  ''''函数:ProcessTable
  ''''功能:遍历指定table的所有字段,如果该字段是主键但不是外键,则设置为Identity
  ''''********************************************** *******************************
  sub ProcessTable(table)
  dim col
  for each col in table.Columns
  ''''对于是主键且不是外键的字段设置为Identity(自增长类型)
  if col.Primary and not col.ForeignKey then
  col.Identity = true
  end if
  next 
  end sub
  name2comment.vbs
  '************************************************* ***************************** 
  '*   File:           name2comment.vbs 
  '*   Purpose:     Database   generation   cannot   use   object   names   anymore   
  '                         in   version   7   and   above. 
  '                         It   always   uses   the   object   codes. 
  ' 
  '                         In   case   the   object   codes   are   not   aligned   with   your   
  '                         object   names   in   your   model,   this   script   will   copy   
  '                         the   object   Name   onto   the   object   Comment   for   
  '                         the   Tables   and   Columns. 
  ' 
  '*   Title:         
  '*   Version:     1.0 
  '*   Company:     Sybase   Inc.   
  '************************************************* ***************************** 
  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

猜你喜欢

转载自emzt82emzt.iteye.com/blog/1363184