ERWin Template Overview By: Dave Wentzel
Agenda u Overview of Templates/Macros u Template editor u Available templates u Independent column browser u Forward engineering process u Trouble shooting
What is a template ? u ERWin contains a macro language u Used for the creation of: –Stored Procedure Templates –Trigger Templates –Table script template (pre & post) u Based on type of template, will automatically generate SP, trigger, or script based on table/relationship information when the model is forward engineered
Why are we using templates? u Provide turn-key approach to data model creation u Reduce the number of triggers and stored procedures which need to be written u Can more easily make model changes without having to ‘re-write’ triggers and SPs
A little on the Macro Language... u ERWin contains a Template Toolbox which is used to create the templates
Types of Macros u Entity –Applied to tables in the diagram –Can be used to act as a loop - ‘For Each Entity’ –Can be used to reference qualities of an entity - ‘For Each (Attribute/Index/Key…)’ u Relationship –Applied to relationship information between entities –Can reference PK, FK, Parent/Child entities
Types of Macros Continued u Attribute –References ‘attributes’ of attributes such as length, field type, FK/PK, etc. u Constraint –References database constraint information u Miscellaneous –Arithmetic operators –Comparison operators –System Information
Stored Procedure Templates u Generates a stored procedure for WRITING / UPDATING a record in a table u Based on primary key attributes u If the key values are equal to an existing record, the record will be updated u If no matching record is found, a new record will be inserted u To be used as low level write routine called from higher level SPs.
Accessing the SP Templates u Right mouse click on a table u Select Table Editor / Stored Procedure option u Attach the appropriate template to the table u One must be attached for each table
Available Templates u Write Template u Stand Alone Table Triggers u Write Dimension Template u Write Fact Template
Write Template u Creates spWriteTableName u Assumes PK is an IDENTITY column u Assumes PK is FieldNameInst u Assumes columns: –Create Date / Create User –Modify Date / Modify User –If PK provided, updates record otherwise inserts new record
Standalone Table Triggers u Generates the insert/update triggers for standalone tables u Standalone tables may occur if created as a reference table u Generated through the SP template instead of the Trigger template because the trigger template assumed relationships
Write Dimension Template u Found in Datamart diagrams u Assumes a PK of identity u Assumes PK is FieldNameInst u Assumes no updates u If PK does not exist, will write new record
Write Fact Template u Found in Datamart diagrams u Inserts new records into fact tables
Trigger Templates u Generates Update/Delete/Insert triggers for a table and it associated tables u Helps maintain RI and date validations between related tables
Triggers and Relationships u Triggers are generated based on relationships between two entities u Double click on a relationship line –Child Delete / Insert / Update –Parent Delete / Insert / Update u Options: –Restrict - prevent –Cascade - propagate change –None - no trigger relationship
Accessing the Trigger Templates u Left mouse click on a table u Select SQL Server Trigger u Select SQL Server Trigger Template to attach Triggers
Available Trigger Templates u Approach Child Delete Restrict u Approach Child Delete Cascade u Approach Child Insert Restrict u Approach Child Update Restrict u Approach Parent Delete Cascade u Approach Parent Delete Restrict u Approach Parent Insert Restrict u Approach Parent Update Restrict
Available Trigger Templates u Approach Delete Footer u Approach Delete Header u Approach Insert Footer u Approach Insert Header u Approach Update Footer u Approach Update Header
Parent / Child Templates u On restrict, provides Raise Error message when a parent / child relationship exists u On cascade, will delete all associated tables with the same primary key u Verifies records do not overlap based on from and thru dates u Requires fields either FromDate / ThruDate or BeginDate / EndDate u Assumes domain type of ID in some models
Header / Footer Templates u Header creates the ‘Create Trigger’ code, parameters, and initial date checks against the table u Footer templates update the ModifyDate/User in the update triggers u Assume ModifyDate/ ModifyUser fields
Standard selections for RI Actions
Table Templates u Pre / post scripts generated during table creation u Used to generate print statement before table creation so implementation script is easier to read u Used for the OLAP fact tables to generate update triggers
Accessing Table Templates u Left click on table u Select Table Editor / Pre & Post Script
Independent Column Browser u Provides method of creating standard fields for use in current data model u Example: Used for standard fields applied to all tables u Domain type of ID exists which is referenced by some templates and must be assigned PK identity columns
Accessing the Independent Column Browser u Under Window select Independent Column Browser Option
Forward Engineering the Model u Triggers, stored procedures, and table scripts will be generated when you forward engineer the data model - based on the options you select u To forward engineer the model, select: Tasks \ Forward Engineer/Schema Generation
Schema Generation
Schema Options to Select u Table –Create Table –Drop Table –Pre-Script –Post-Script –Create Procedure –Drop Procedure
Options continued u Column –Validation –Default u Index –Create Index »Alternate Key »Foreign Key »Inversion Entry »CLUSTERED »Physical Storage
Options Continued u Referential Integrity –Primary Key »Alter (PK) –Foreign Key »Alter (FK) u Trigger –User Defined »RI Type Override »Relationship Override
Forward Engineer u Select PREVIEW u Cut / paste into SQL query window to run u Do not directly forward engineer into the database –Could delete items you did not anticipate –May be connected to a production server
Troubleshooting u Execute script u Review error statements u Review script code with error u Access the template to see what it is expecting u Verify correct relationships, triggers, SPs, etc. are associated with the table u Verify required fields are assigned to the table