前言
因为公司要从 oracle 迁移到 mysql,同时也没有关于数据库的文档,同时数据库表太多了,一个一个手动建模,太没有效率了,所以网上查了一下,powerdesigner 使用的 visual basic 语言,可以使用脚本批量导入创建。
如何使用脚本
excel 样式
vbs 脚本
' 第一行是表头
' 第二行开始是列的描述,分别是:表名、表注释、列名、列数据类型、列数据长度、列是否为空、列注释
' Excel的sheet名称统一为Sheet1
'开始
Option Explicit
Dim mdl ' the current model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim HaveExcel
Dim RQ
RQ = vbYes 'MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")
If RQ = vbYes Then
HaveExcel = True
' Open & Create Excel Document
Dim x1 '
Set x1 = CreateObject("Excel.Application")
x1.Workbooks.Open "C:\Users\hp\Desktop\sql.xlsx" '指定 excel文档路径
x1.Workbooks(1).Worksheets("Sheet1").Activate '指定要打开的sheet名称
Else
HaveExcel = False
End If
a x1, mdl
sub a(x1, mdl)
dim rwIndex
dim table
dim tableName
dim colname
dim col
dim dataType
dim tableComment
on error Resume Next
set tableName = ""
set tableComment = ""
For rwIndex = 2 To 9713 '
With x1.Workbooks(1).Worksheets("Sheet1")
If tableName = "" Then
' 创建表
set table = mdl.Tables.CreateNew '创建一个 表实体
tableName = .Cells(rwIndex, 1).Value '表名
tableComment = .Cells(rwIndex, 2).Value
If tableComment = "" Then
table.Name=tableName '表名
Else
table.Name=tableComment '表名
End If
table.Code=.Cells(rwIndex, 1).Value '表标识
table.Comment=tableComment '表注释
ElseIf tableName = .Cells(rwIndex, 1).Value Then
Else
set table = mdl.Tables.CreateNew '创建一个 表实体
tableName = .Cells(rwIndex, 1).Value '表名
table.Name=.Cells(rwIndex, 1).Value '表名
table.Code=.Cells(rwIndex, 1).Value '表标识
table.Comment=.Cells(rwIndex, 2).Value '表注释
End If
' 创建列
set col = table.Columns.CreateNew '创建一列/字段
If .Cells(rwIndex, 7).Value = "" Then
col.Name = .Cells(rwIndex, 3).Value '指定列名
Else
col.Name = .Cells(rwIndex, 7).Value '指定列名
End If
col.Code = .Cells(rwIndex, 3).Value
col.Comment = .Cells(rwIndex, 7).Value '指定列说明
dataType = .Cells(rwIndex, 4).Value '指定列数据类型
Select Case dataType
Case "DATE"
col.DataType = "datetime"
CASE "VARCHAR2"
col.DataType = Replace("varchar(%s)","%s", .Cells(rwIndex, 5).Value)
CASE "NVARCHAR2"
col.DataType = Replace("varchar(%s)","%s", .Cells(rwIndex, 5).Value)
CASE "CHAR"
col.DataType = Replace("char(%s)","%s", .Cells(rwIndex, 5).Value)
CASE "CLOB"
col.DataType = "text"
CASE "BLOB"
col.DataType = "BLOB"
CASE "FLOAT"
col.DataType = Replace("float(%s)","%s", .Cells(rwIndex, 5).Value)
CASE "NUMBER"
col.DataType = "bigint"
CASE "RAW"
col.DataType = Replace("binary(%s)","%s", .Cells(rwIndex, 5).Value)
CASE "TIMESTAMP(6)"
col.DataType = "timestamp"
END Select
If .Cells(rwIndex, 3).Value = "ID" Then
col.Primary = true '指定列是否主键 true 为主键 '指定列是否可空 true 为不可空
End If
If .Cells(rwIndex, 6).Value = "Y" Then
col.Mandatory = true '指定列是否主键 true 为主键
End If
End With
Next
MsgBox "生成成功"
Exit Sub
End sub