通过脚本在PowerDesigner PDM中将唯一索引批量转成主键

'Write by Stone 20110407
dim sIndexName
dim sIndexCode
dim sIndexCols
dim index_cols_code(20)   '索引列名称
dim index_cols_name(20)
dim index_col_num    '索引字段数
dim pk_name          '主键名称
dim pk_code          '主键编码
dim pk_constraint_name '约束名称
dim pk_obj           'PK对象
dim existpk
dim pk_col
dim index 
for each TABLE in ActiveModel.tables
  FOR each index in table.indexes
    sIndexName = index.GetAttribute("Name")       '索引Name的内容  
    sIndexCode = index.GetAttribute("Code")       '索引Code的内容   
    if   Right(sIndexName, 6)   =  "_UIDX1" then  '目前暂以此为作为检查索引为唯一索引的依据
      '读取索引字段内容
      index_col_num = 0
      sIndexCols = ""
      for each indexcol in index.IndexColumns
        index_cols_code(index_col_num) = indexcol.Code
        index_cols_name(index_col_num) = indexcol.Name
        index_col_num = index_col_num + 1
        sIndexCols = sIndexCols + indexcol.Code + "," 
      next
      'MsgBox sIndexName + "--"+ sIndexCols
      
      '检查是否已PK
      existpk = false
      for each key in table.keys 
       if key.Primary = true then
         existpk = true
         exit for
       end if
      next
      
      if existpk = false then
       for each key in table.keys 
         if key.Name = sIndexName then
           existpk = true
           exit for
         end if
        next
      end if
      
      if existpk = false then
        'Name Key field
        pk_name = sIndexName
        pk_code = sIndexCode
        pk_constraint_name = "PK_" + TABLE.Code
        'Create PK
        set pk_obj = table.Keys.CreateNew()
        pk_obj.Name = pk_name
        pk_obj.Code = pk_code
        pk_obj.ConstraintName = pk_constraint_name
        pk_obj.Primary = true
        
        'add columns
        index = 0
        while index < index_col_num
          for each col in table.Columns 
            if col.Name = index_cols_name(index) then
              pk_obj.Columns.Add col
            end if
          Next
          index = index + 1 
        Wend
      end if
    end if   
  next   
NEXT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值