Constraints are defined for physical files or tables. You can define three types of constraint: Key, Referential and Check. How do you define constraints? You can define them in SQL using the CREATE TABLE or ALTER TABLE commands. But my favorite means of defining constraints is in iSeries Navigator. Use the Key Constraints, Referential Constraints and Check Constraints tabs from the properties of a table, as shown in Figure 1. [TABLE]Figure 1: Constraint tabs in the properties of a table (physical file)<
'); // -->
/p> Key constraints There are two types of Key constraints: unique and primary. A table may have only one primary Key constraint but may have many unique Key constraints. Figure 2 shows the definition of a Primary Key constraint in iSeries Navigator. The constraint is named CategoryPrimaryKey, and the category code is the key field. (For multiple key fields, simply select them in the required sequence.) Note that constraint names are long names, i.e. they are not object names. [TABLE]Figure 2: Defining a Primary Key constraint The same constraint could be defined on green screen using the following command: Referential constraints Figure 3 shows the definition of a Referential constraint that will enforce the rule for you. The constraint (CategoryProductRestriction) is defined between the Product file (dependant) and the Category file (parent). The corresponding fields for the constraint are the category codes. The constraint specifies a delete and an update rule of restrict. [TABLE]Figure 3: Defining a Referential constraint The same constraint could be defined on green screen using the following command: The possible delete rules are as follows: The possible update rules are RESTRICT and NO ACTION, and the insert rule is implicit. (No records can be added that have no matching parent record.) All rules except RESTRICT require that both the parent and dependant files be journalled to the same journal. Take a moment to consider a CASCADE delete rule. Your program deletes a record from a parent file, and the database manager deletes 200 corresponding records on the dependent file. You can see why they need to be journalled. Both the parent and dependent files must be single-member, externally defined files. As with key constraints, a Referential constraint results in the generation of an access path as would be defined by a join logical but without the actual logical file being created. Check constraints Figure 4 shows the definition of a Check constraint (Right_Price) that ensures that the selling price (SELLPR) is always greater than or equal to the buying price (LNDCST). [TABLE]Figure 4: Defining a Check constraint The same constraint could be defined on green screen using the following command: Which constraints? The Referential constraint for the product file has a state of established/enabled and has no records with check pending. The State column shows whether a Referential constraint is in a defined (DEF) or an established (EST) state. In a defined state, one of the files does not have a member, i.e. the constraint is defined but cannot be established. An established state exists when both files have members. In either state, the constraint can be enabled or disabled (more on this in a moment). You also have the option to change or remove constraints or to display records in check pending. [TABLE]Figure 5: Defining a Check constraint Check pending means there are records on the file that contravene the Referential constraint; the parent file can be opened only for read and insert operations (no updates or deletions allowed) and the dependent file cannot be opened at all. To fix the data in the case of a check pending status, you will need to temporarily disable the constraint. At times you might consider it worthwhile to temporarily disable a constraint for performance reasons. For example, suppose a month-end process performs a batch update on a parent file and a dependent file. The month-end process allocates both files, using the Allocate Object (ALCOBJ) command. Based on your confidence that the month-end process will not violate a Referential constraint, you disable the constraint after allocating the files, run the month-end program(s), and re-enable the constraint before de-allocating the files. Of course, if your confidence about your month-end process was ill-founded and the batch job does create data that violates the constraint, then your files will go into a Check Pending status when you re-enable the constraint and you must correct the data before the files can be used. Violating constraints In the end . . . ---------------------------
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iSeries Security - Security Tools, Physical Security and System Security |
| About Us | Contact Us | For Advertisers | For Business Partners | Site Index | RSS |
|
|
|
|||||||