1. 程式人生 > >軟體工具——PowerDesigner逆向工程匯出Oracle資料庫表結構

軟體工具——PowerDesigner逆向工程匯出Oracle資料庫表結構

一、準備工作:

準備工作包括,安裝或已經有建立好的oracle資料庫,建立好user、tablespace、table等;安裝Oracle客戶端、PLSQL等軟體並配置好資料庫連線,安裝PowerDesigner軟體。可以參考我的博文:

二、PowerDesigner配置資料來源

開啟PowerDesigner,選單欄File-new,建立一個Physical Data Model

Model type 選擇Physical Data Model, Model name自己隨便寫一個,點選確定,可以看到PowerDesigner主頁面出現編輯背景欄,上方選單欄出現database選單。

選擇選單欄Database--Change Current DBMS..更改當前連線的DBMS

DBMS選擇自己要連線的資料庫版本,點選確定。

選單欄選擇Database--Connect.. 連線資料庫

選擇ODBC machine data source,點選Configure...按鈕

 

在ODBC Machine Data Sources選單下,點選Add Data Source圖示或者鍵盤Ctrl+N

警告可以無視,點選確定。

沒有許可權就選擇使用者資料源,有許可權可以選擇系統資料來源,點選下一步。

選擇自己對應的Oracle Client驅動程式(上圖是因為我裝了兩次),點選下一步

點選完成。

Data Source Name隨便寫個自己記得住的名字,Description隨便寫,TNS Service Name在配置OracleClient時,TNS檔案裡有,User ID就是資料建庫登陸使用者名稱。點選OK

可以看到多了一個QFtest的資料來源,點選確定。

回到這個Connect to a Data Source頁面,下拉列表中多了一個剛剛配置的QFtest資料來源,選擇這個資料來源之後填寫login資訊。

三、檢查資料來源連線。

一般完成上面步驟就已經成功連線資料來源了,但是也有手誤寫錯配置的時候,下面檢查一下資料來源是否連線成功。

Database--Configure connections...

眼熟的頁面,雙擊QFtest資料來源

上方截圖刪掉了TNS Service Name資訊,需要補全,點選Test Connection

上方截圖中刪掉了Service Name和User Name,需要補全,輸入密碼,點選OK

四、PowerDesigner逆向工程匯出資料庫表結構

選單欄選擇File--Reverse Engineer--database...

Model name其實隨便寫,DBMS選擇合適的驅動,下面選擇share the DBMS definition,點選確定。

選擇Using a data source,然後點選下方選擇欄

上圖刪除了User ID和Password,需要補全,點選Connect。

資料來源選擇好之後點選確定。

選擇需要匯出的表,點選OK,開始匯出表。

匯出之後會出現備註找不到,name和Code相等的情況。

五、處理沒有中文備註的問題。

首先選單欄選擇Tools--General Options...

選擇Dialog,將Name to Code mirroring去掉,使name和code可以不一致。點選OK

然後選單欄選擇Tools--Excute Commands--Edit/Run Scripts...貼上以下指令碼

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 code 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   
if len(tab.comment) <> 0 then   
tab.name = tab.comment   
end if   
On Error Resume Next   
Dim col 'running column   
for each col in tab.columns   
if len(col.comment) <>0 then   
col.name =col.comment   
end if   
On Error Resume Next   
next   
end if   
next   
end sub

點選Run。備註會出現在表的Name列。上圖Run之後可以直接Close關閉,可以按照預設路徑儲存當前指令碼。其實Close指令碼儲存的時候會發現,在預設的指令碼路徑下(C:\Program Files (x86)\Sybase\PowerDesigner 12\VB Scripts)有一個Name2Code指令碼檔案,開啟後會發現,其實就是上面這段指令碼。

以上步驟完成後,PowerDesigner已經通過逆向工程匯出資料庫表結構了。

關閉軟體時別忘記儲存,全部選擇D盤根目錄的話會出現三個檔案。

分別是model的pdm檔案(主要是匯出的table資訊),資料來源的dsn檔案,工作空間的sws檔案。

後續使用時,建立相關內容可以雙擊Workspace.sws開啟工作空間,建立新的model等。如果只是想簡單看看錶結構,或者共享資訊給他人,使用pdm檔案。Test.dsn檔案只是為了儲存一下資料來源資訊,沒用了可以刪掉。