首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示.
看看怎样用PowerDesigner快速的创建出这个数据库吧.
1.现在开始使用PowerDesigner创建数据库,首先运行程序,进入主界面:
2.File—NewModel—PhysicalDataModel—PhysicalDiagram—Modelname设置为test,DBMS属性设置为MicrosoftSQLServer2005:
3.首先用表格工具创建一个表格模板:
4.双击表格模板,设置属性,我们首先设置Major表:
5.设置好表名,点击Columns标签,设置字段属性,设置如图所示:
6.因为MajorID字段我们要设置为自动增长,所以要设置它的高级属性,选择MajorID字段,点击属性按钮,在General面板中勾选上Identity复选框:
7.确定后我们再创建一个Student表,字段设置如图所示:
8.接着是为Student创建一个MajorID外键,使用PowerDesigner可以很轻松的完成这个工作,选择关系设置工具,在Student表上按住左键不放,拖拽至Major表,便可为Student表添加一个MajorID的外键:
9.哈哈,现在测试表已经设置好了,接着设置一下我们要生成的数据库吧,这些表都将被创建到该数据库中,我们在设计面板空白处右键—Properties,在弹出的属性设置对话框设置如下:
10好了,在此我们对新数据库的设置已经完成,但是在SQL中还是空空如也啊,我们要怎么把这边设计好的结构移植到SQLServer2005中呢?执行操作:Database—GenerateDatabase,设置好存储过程导出目录和文件名,点击确定即可:
11.到你的导出目录,就可以看见导出的数据库创建存储过程了,打开SQL,执行一下,你就会看到数据库被神奇的创建好了:
12.好了,数据库的准备工作做好了,下一篇我们就将使用该数据库结合CodeSmith自动批量生成代码啦~
上述实践中生成的源代码:
SQL.sql/*==============================================================*/
/*Databasename:PD_test*/
/*DBMSname:MicrosoftSQLServer2005*/
/*Createdon:2010/6/13星期日17:27:17*/
/*==============================================================*/
dropdatabasePD_test
go
/*==============================================================*/
/*Database:PD_test*/
/*==============================================================*/
createdatabasePD_test
go
usePD_test
go
/*==============================================================*/
/*Table:Major*/
/*==============================================================*/
createtableMajor(
MajorIDintidentity,
![PowerDesigner使用教程|使用方法 powerdesigner使用](http://img.413yy.cn/images/31101031/31020152t01ed0a14b2a4155006.jpg)
Namenvarchar(20)notnull,
Remarknvarchar(Max)null,
constraintPK_MAJORprimarykey(MajorID)
)
go
/*==============================================================*/
/*Table:Student*/
/*==============================================================*/
createtableStudent(
StudentIDnvarchar(20)notnull,
MajorIDintnull,
Namenvarchar(20)notnull,
Sexbitnotnull,
Ageintnull,
Remarknvarchar(Max)null,
constraintPK_STUDENTprimarykey(StudentID)
)
go
altertableStudent
addconstraintFK_STUDENT_REFERENCE_MAJORforeignkey(MajorID)
referencesMajor(MajorID)
go