最近用PowerDesigner15做数据库设计的时候发现如果先做好了CDM图,然后生成PDM,再生成SQL 2005或SQL 2008脚本,发现始终无法生成外键约束。
我估计很多人都遇到了类似的问题。 经过查找和摸索终于解决了这个问题,现在将过程叙述如下,希望对大家有帮助。
实验过程如下:
1、生成在CDM中建立两个实体Entity_1和Entity_2,并建立关系Relationship。
2、自动生成MS SQL Server 2008的PDM。
3、选择DBMS类型为SQL 2008然后生成Database脚本
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2008 */
/* Created on: 2010/4/5 19:36:09 */
/*==============================================================*/
if exists (select 1
from sysobjects
where id = object_id('Entity_1')
and type = 'U')
drop table Entity_1
go
if exists (select 1
from sysindexes
where id = object_id('Entity_2')
and name = 'Relationship_1_FK'
and indid > 0
and indid < 255)
drop index Entity_2.Relationship_1_FK
go
if exists (select 1
from sysobjects
where id = object_id('Entity_2')
and type = 'U')
drop table Entity_2
go
/*==============================================================*/
/* Table: Entity_1 */
/*==============================================================*/
create table Entity_1 (
Attribute_1 int not null,
Attribute_2 int null,
constraint PK_ENTITY_1 primary key nonclustered (Attribute_1)
)
go
/*==============================================================*/
/* Table: Entity_2 */
/*==============================================================*/
create table Entity_2 (
Attribute_3 int not null,
Attribute_1 int null,
Attribute_4 int null,
constraint PK_ENTITY_2 primary key nonclustered (Attribute_3)
)
go
/*==============================================================*/
/* Index: Relationship_1_FK */
/*==============================================================*/
create index Relationship_1_FK on Entity_2 (
Attribute_1 ASC
)
go
从上面脚本可以看出没有生成需要的外键约束,只是生成了相关索引。
解决方法如下:
- 打开生成的PDM
- 将DBMS类型换为MS SQL 2000
- 在Model菜单中选择References命令,可以看到最后的Implementation被设置为Trigger
- 将Implementation被设置为Declarative(如果没有第二步就不能完成本步)
- 将DBMS类型换为MS SQL 2008
-
生成MS SQL 2008数据库生成脚本,能够正常生成需要的外键
/*==============================================================*/
/* DBMS name: Microsoft SQL Server 2008 */
/* Created on: 2010/4/5 19:49:25 */
/*==============================================================*/
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('Entity_2') and o.name = 'FK_ENTITY_2_RELATIONS_ENTITY_1')
alter table Entity_2
drop constraint FK_ENTITY_2_RELATIONS_ENTITY_1
go
if exists (select 1
from sysobjects
where id = object_id('Entity_1')
and type = 'U')
drop table Entity_1
go
if exists (select 1
from sysindexes
where id = object_id('Entity_2')
and name = 'Relationship_1_FK'
and indid > 0
and indid < 255)
drop index Entity_2.Relationship_1_FK
go
if exists (select 1
from sysobjects
where id = object_id('Entity_2')
and type = 'U')
drop table Entity_2
go
/*==============================================================*/
/* Table: Entity_1 */
/*==============================================================*/
create table Entity_1 (
Attribute_1 int not null,
Attribute_2 int null,
constraint PK_ENTITY_1 primary key nonclustered (Attribute_1)
)
go
/*==============================================================*/
/* Table: Entity_2 */
/*==============================================================*/
create table Entity_2 (
Attribute_3 int not null,
Attribute_1 int null,
Attribute_4 int null,
constraint PK_ENTITY_2 primary key nonclustered (Attribute_3)
)
go
/*==============================================================*/
/* Index: Relationship_1_FK */
/*==============================================================*/
create index Relationship_1_FK on Entity_2 (
Attribute_1 ASC
)
go
alter table Entity_2
add constraint FK_ENTITY_2_RELATIONS_ENTITY_1 foreign key (Attribute_1)
references Entity_1 (Attribute_1)
go
(完)
分享到:
相关推荐
通常数据表字段使用英文,备注comment完成字段的说明。使用该脚本能够在PowerDesigner批量生成SQL脚本时将name生成数据库中字段的comment
使用Powerdesigner的逆向工程生成PDM.pdf
PowerDesigner根据SQL语句自动生成ER图
Powerdesigner逆向工程生成PDM 中文列名表名替换为注释
在PowerDesigner里执行脚本命令,从Excel里读取表结构生成pdm文件。
POWERDESIGNER逆向工程从现有数据库生成PDM
通过配置资源库,使用PowerDesigner工具根据数据库中表之间的关系生成PDM模型。
使用Powerdesigner生成sql脚本
powerdesigner的使用 sql语句 sql语句的使用
使用PowerDesigner通过excel模板文件与vb可执行代码,生成创建数据库sql建表语句脚本,资源包含.vb 和 excel模板文件。
很多的朋友对 E-R图 都不是很熟悉,或者说有的朋友在开发的项目过程中,根本就...我个人根据一些资料整理了一下关于使用PowerDesigner工具对ER图的 描述 及用法 示例:希望对,对E-R图并不是很熟悉的朋友有所帮助吧….
用PowerDesigner自动生成数据库的表结构的方法,很实用哦~
Powerdesigner连接Mysql数据库生成PDM
PowerDesigner15的注册码和注册方法在readme文件中,因上传大小的限制 无法给PowerDesigner15的安装包 需要的话留言给你云盘地址
NULL 博文链接:https://superfofo.iteye.com/blog/635937
PowerDesigner逆向工程生成PDM模型及数据库
PowerDesigner生成SQL脚本步骤详解.pdf
powerdesigner中去掉生成sql中的引号
当PowerDesigner连接好数据库,并生成模型后,在PowerDesigner用Ctrl+Shift+x快捷键打开,然后将脚本内容粘贴运行,就可以开始生成数据库中存在的注释了
powerdesigner逆向工程导入mysql脚本生成PDM带全注释