咱們常常用PowerDesigner來進行數據庫表結構的設計,而且設計出來的表比較直觀的看出之間的相互關係,方便理解;但其自動生成的腳本並不必定符合咱們實際需求,因此須要通過必定配置後才能真正達到要求,下面用一個簡單的案例來學習如何配置PD。sql
需求:數據庫
這裏假設數據庫代碼版本維護是經過sql腳本文件來管理的,構造可重複執行的建立表、添加字段、索引等ide
用PowerDesigner生成符合本身實際需求的腳本,要求以下學習
1.建表語句可重複執行測試
2.表名要有中文註釋spa
3.在PD裏外鍵關聯不體如今生成腳本里設計
4.主鍵、外鍵等能夠自定義命名code
測試表:orm
學生表(Student)和班級表(Classes)blog
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
從上面腳本能夠看出
第一每次表存在都會先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
其實對於自定義腳本,你們應該發現大部分都是經過數據庫->Edit Current DBMS->Script->Objects來定義的,如Table來定義表,Column來定義列,不少功能只要去嘗試修改下就能知道了。