用PowerDesigner生成自定義建表語句


  我們經常用PowerDesigner來進行數據庫表結構的設計,並且設計出來的表比較直觀的看出之間的相互關系,方便理解;但其自動生成的腳本並不一定符合我們實際需求,所以需要經過一定配置后才能真正達到要求,下面用一個簡單的案例來學習如何配置PD。

需求:

  這里假設數據庫代碼版本維護是通過sql腳本文件來管理的,構造可重復執行的創建表、添加字段、索引等

  用PowerDesigner生成符合自己實際需求的腳本,要求如下

  1.建表語句可重復執行

  2.表名要有中文注釋

  3.在PD里外鍵關聯不體現在生成腳本里

  4.主鍵、外鍵等可以自定義命名

測試表:

  學生表(Student)和班級表(Classes)

PD設計如下:

 

自動生成腳本:  

if exists (select 1
   from sys.sysreferences r join sys.sysobjects o on (o.id = r.constid and o.type = 'F')
   where r.fkeyid = object_id('School_Student') and o.name = 'FK_SCHOOL_S_REFERENCE_SCHOOL_C')
alter table School_Student
   drop constraint FK_SCHOOL_S_REFERENCE_SCHOOL_C
go

if exists (select 1
            from  sysobjects
           where  id = object_id('School_Classes')
            and   type = 'U')
   drop table School_Classes
go

if exists (select 1
            from  sysobjects
           where  id = object_id('School_Student')
            and   type = 'U')
   drop table School_Student
go

/*==============================================================*/
/* Table: School_Classes                                        */
/*==============================================================*/
create table School_Classes (
   ID                   int                  not null,
   Name                 nvarchar(20)         null,
   CreateTime           datetime             null default getdate(),
   constraint PK_SCHOOL_CLASSES primary key (ID)  
)
go

/*==============================================================*/
/* Table: School_Student                                        */
/*==============================================================*/
create table School_Student (
   ID                   int                  not null,
   Name                 nvarchar(20)         null,
   ClassID              int                  null default 0,
   Age                  tinyint              null default 0,
   StuNo                nvarchar(10)         null,
   Remark               nvarchar(500)        null,
   constraint PK_SCHOOL_STUDENT primary key (ID)  
)
go

alter table School_Student
   add constraint FK_SCHOOL_S_REFERENCE_SCHOOL_C foreign key (ClassID)
      references School_Classes (ID)
go
View Code

  從上面腳本可以看出

  第一每次表存在都會先drop然后在create,在自動升級腳本里容易造刪除真實表;

  第二圖上班級編號是外鍵,但這里假設只是為了方便查看關系,真實情況下可能我們並不需要生成外鍵關系;

  第三如果當表名很長的時候,主鍵也會被截斷顯示或不是我們期望的格式

  所以雖然表設計好了,但要簽入數據庫腳本的話,自己還是需要進行一定的修改,下面我們一步步來實現自定義配置以達到要求

自定義配置PD

  1.去掉腳本中的外鍵關聯

  1)雙擊表結構,如下圖所示去掉create foreign key和drop foreign key,然后點應用,你會發現Preview中外鍵相關腳本已經沒有了

 

  2.去掉自動生成的表注釋,換成自定義的

  1)依次點擊數據庫->Generate Database->Format去掉Title前面的勾,這時候自定生成的注釋已經沒了,下一步添加自定義注釋;

  2)依次點擊數據庫->Edit Current DBMS->Script->Objects->Table->Create,加上如下圖所示腳本,這時候Preview已經有這段注釋了

  3.讓建表語句可以重復執行,如if not exists create這樣

  1)去掉自帶drop table操作,通過1.1中Show Generation Options中,去掉drop table勾就可以了;

  2)加上自定義重復腳本判斷語句,還是剛才2.2圖所在Table->Create地方,修改Value值如下圖

  4.自定義主、外鍵名稱

  1)位置如下,其中PK_%.U27:TABLE%就是主鍵的規則名稱,U27就是長度最多只能是27位,TABLE就是表名,修改這里即可改變主鍵的生成規則

通過上面配置后,最終生成的SQL腳本就是按我們設想的來了,如下

/* 
    表名:班級表 
*/
if not exists (select 1
            from  sysobjects
           where  id = object_id('School_Classes')
            and   type = 'U')
begin
    create table School_Classes (
       ID                   int                  not null,
       Name                 nvarchar(20)         null,
       CreateTime           datetime             null default getdate(),
       constraint PK_SCHOOL_CLASSES primary key (ID)  
    )
    
end
go

/* 
    表名:學生表 
*/
if not exists (select 1
            from  sysobjects
           where  id = object_id('School_Student')
            and   type = 'U')
begin
    create table School_Student (
       ID                   int                  not null,
       Name                 nvarchar(20)         null,
       ClassID              int                  null default 0,
       Age                  tinyint              null default 0,
       StuNo                nvarchar(10)         null,
       Remark               nvarchar(500)        null,
       constraint PK_SCHOOL_STUDENT primary key (ID)  
    )
    
end
go
View Code

  其實對於自定義腳本,大家應該發現大部分都是通過數據庫->Edit Current DBMS->Script->Objects來定義的,如Table來定義表,Column來定義列,很多功能只要去嘗試修改下就能知道了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM