ER/Studio - Embarcadero Technologies Product Documentation

ER/Studio - Embarcadero Technologies Product Documentation ER/Studio - Embarcadero Technologies Product Documentation

docs.embarcadero.com
from docs.embarcadero.com More from this publisher
24.12.2012 Views

USING ER/STUDIO > DEVELOPING THE PHYSICAL MODEL • Set Null: The trigger verifies the existence of the foreign key values in the parent table's primary key. If the values cannot be validated, then the trigger sets the foreign key value to null in the child table and then updates or deletes the parent. For example, you have a parent table persons and a child table tasks, which through a mandatory relationship share an id. By using a Set Null trigger you can allow the user to insert data into the tasks table that contains an id for which their is no corresponding value in the persons table. The database will not throw a referential integrity error in this case. • Set Default: Sets the foreign key to its DEFAULT value, then updates or deletes the parent. • Restrict: The trigger verifies the existence of foreign key values in the parent table's primary key. If the values cannot be validated, then the trigger prevents the insertion, update or deletion of data. For example, you have a parent table persons and a child table tasks, which through a mandatory relationship share an id. By using a Restrict trigger you can prevent the user from inserting data into the tasks table that contains an id for which their is no corresponding value in the persons table. The database will throw a referential integrity error in this case. • Cascade: If a primary key is updated or deleted, the trigger cascades that modification to corresponding foreign key values in dependent tables. Create a Scripted User-Defined Trigger 1 On the Data Model Window or the Data Model Explorer, select the table on which you want to create a Trigger. 2 Select Edit > Edit Database Table. TIP: Alternatively, on the Data Model Window, open the Table Editor by double-clicking the table or right-clicking the object and then selecting Edit Table. 3 On the Table Editor, click the Dependencies tab. 4 Click the Add button and then select Triggers > Scripted. 5 Complete the Trigger Editor and then click Finish to create the trigger. The following describe options that require additional explanation: • In order for code to be generated for procedural logic such as triggers and procedures, the results of the trigger must be stored in a variable called resultstring. For example, in the Northwind.dm1 sample model, the Data Dictionary lists a reusable trigger, SYSUPDATE, for the the Oracle platform. The code for this trigger includes the following statement, which is required for code to be generated for the trigger: resultstring = trigBody • The Trigger Editor includes find and replace functionality for working with SQL text strings. • Name: Lets you enter a trigger name or edit the existing one. SQL tab Lets you enter a CREATE TRIGGER statement for the trigger, or edit the existing script. If you do not want to enter an SQL script, you can use the Import button to import it. • Export: Lets you export the SQL script to a *.sql file. This is useful for when you want to create additional triggers based on the one you are currently creating. You can export the code, and later import it into another procedure. When you click Export, the Save As dialog box opens. Enter the file name. ER/Studio saves the files in the Model folder, if you want to save your files in a different folder, browse and locate it. • Import: Lets you import *.sql files. When you click Import, the Open dialog box opens. You can enter the name of the *.sql file, or browse and locate it. • Validate: Lets you validate the SQL script. If ER/Studio detects any errors, it returns them in a message box. Errors include the error type, line, and column information. EMBARCADERO TECHNOLOGIES > ER/STUDIO® 8.0.3 USER GUIDE 190

USING ER/STUDIO > DEVELOPING THE PHYSICAL MODEL Description tab Includes a text box where you can enter a Trigger description. Attachment Bindings tab Bind an external piece of information, or attachment to the trigger. You can also remove an attachment from an object, override an attachment binding’s default value, or change the position of a bound attachment. To override the value of the attachment you have moved to the Selected Attachments grid, double-click the Value field of the target attachment. ER/Studio opens the Value Override Editor or a list depending on the attachment datatype. Attachments are created in the Attachments folder of the Data Dictionary. For more information, see Attaching External Documents to the Data Model. Create a Templated User-Defined Trigger 1 In the Data Model Explorer, expand the Physical Main Model, right-click the Triggers node, and then click New Trigger. 2 Complete the Trigger Wizard and then click Finish to create the trigger. TIP: Once you have created the auxiliary table, you can edit it by right-clicking the auxiliary table you want to change, and then selecting Edit Auxiliary Table. The following describe options that require additional explanation: • The Trigger Editor includes find and replace functionality for working with SQL text strings. • In order for code to be generated for procedural logic such as triggers and procedures, the results of the trigger must be stored in a variable called resultstring. For example, in the Northwind.dm1 sample model, the Data Dictionary lists a reusable trigger, SYSUPDATE, for the the Oracle platform. The code for this trigger includes the following statement, which is required for code to be generated for the trigger: resultstring = trigBody Description page/tab Enter or edit a definition for the description. If the target database supports it, ER/Studio adds this definition as a comment when generating SQL code. Attachment Bindings tab Bind an external piece of information, or attachment to the trigger. You can also remove an attachment from an object, override an attachment binding’s default value, or change the position of a bound attachment. To override the value of the attachment you have moved to the Selected Attachments grid, double-click the Value field of the target attachment. ER/Studio opens the Value Override Editor or a list depending on the attachment datatype. Attachments are created in the Attachments folder of the Data Dictionary. For more information, see Attaching External Documents to the Data Model. Create a Reusable User-Defined Trigger Reusable user-defined triggers are created in the Data Dictionary. For more information, see Create and Edit Reusable Triggers Creating and Editing Functions Functions are subroutines that you define. Functions can be written in a programming language such as C, COBOL, or Java that returns a scalar value or a complete table, or they can call another function, or they can be written in SQL and return a scalar value. Functions are useful for reusable application logic. You can use functions to determine the best methods for controlling access and manipulation of the underlying data contained in an object. Functions accept a number of parameters and pass a single value back to the calling program. Functions can be used in the database to check the validity of the data being entered. For example, functions can be used to validate zip codes. By invoking a routine with the zip code, the function can return a true or false value based on if the zip code is valid. EMBARCADERO TECHNOLOGIES > ER/STUDIO® 8.0.3 USER GUIDE 191

USING <strong>ER</strong>/STUDIO > DEVELOPING THE PHYSICAL MODEL<br />

• Set Null: The trigger verifies the existence of the foreign key values in the parent table's primary key. If the values<br />

cannot be validated, then the trigger sets the foreign key value to null in the child table and then updates or<br />

deletes the parent. For example, you have a parent table persons and a child table tasks, which through a<br />

mandatory relationship share an id. By using a Set Null trigger you can allow the user to insert data into the<br />

tasks table that contains an id for which their is no corresponding value in the persons table. The database will<br />

not throw a referential integrity error in this case.<br />

• Set Default: Sets the foreign key to its DEFAULT value, then updates or deletes the parent.<br />

• Restrict: The trigger verifies the existence of foreign key values in the parent table's primary key. If the values<br />

cannot be validated, then the trigger prevents the insertion, update or deletion of data. For example, you have a<br />

parent table persons and a child table tasks, which through a mandatory relationship share an id. By using a<br />

Restrict trigger you can prevent the user from inserting data into the tasks table that contains an id for which<br />

their is no corresponding value in the persons table. The database will throw a referential integrity error in this<br />

case.<br />

• Cascade: If a primary key is updated or deleted, the trigger cascades that modification to corresponding foreign<br />

key values in dependent tables.<br />

Create a Scripted User-Defined Trigger<br />

1 On the Data Model Window or the Data Model Explorer, select the table on which you want to create a Trigger.<br />

2 Select Edit > Edit Database Table.<br />

TIP: Alternatively, on the Data Model Window, open the Table Editor by double-clicking the table or<br />

right-clicking the object and then selecting Edit Table.<br />

3 On the Table Editor, click the Dependencies tab.<br />

4 Click the Add button and then select Triggers > Scripted.<br />

5 Complete the Trigger Editor and then click Finish to create the trigger.<br />

The following describe options that require additional explanation:<br />

• In order for code to be generated for procedural logic such as triggers and procedures, the results of the<br />

trigger must be stored in a variable called resultstring. For example, in the Northwind.dm1 sample model,<br />

the Data Dictionary lists a reusable trigger, SYSUPDATE, for the the Oracle platform. The code for this trigger<br />

includes the following statement, which is required for code to be generated for the trigger:<br />

resultstring = trigBody<br />

• The Trigger Editor includes find and replace functionality for working with SQL text strings.<br />

• Name: Lets you enter a trigger name or edit the existing one.<br />

SQL tab<br />

Lets you enter a CREATE TRIGG<strong>ER</strong> statement for the trigger, or edit the existing script. If you do not want to<br />

enter an SQL script, you can use the Import button to import it.<br />

• Export: Lets you export the SQL script to a *.sql file. This is useful for when you want to create additional triggers<br />

based on the one you are currently creating. You can export the code, and later import it into another procedure.<br />

When you click Export, the Save As dialog box opens. Enter the file name. <strong>ER</strong>/<strong>Studio</strong> saves the files in the Model<br />

folder, if you want to save your files in a different folder, browse and locate it.<br />

• Import: Lets you import *.sql files. When you click Import, the Open dialog box opens. You can enter the name<br />

of the *.sql file, or browse and locate it.<br />

• Validate: Lets you validate the SQL script. If <strong>ER</strong>/<strong>Studio</strong> detects any errors, it returns them in a message box.<br />

Errors include the error type, line, and column information.<br />

EMBARCAD<strong>ER</strong>O TECHNOLOGIES > <strong>ER</strong>/STUDIO® 8.0.3 US<strong>ER</strong> GUIDE 190

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!