SQL Reference

SET INTEGRITY

The SET INTEGRITY 105 statement is used to do one of the following:

When the statement is used to check integrity for a table after it has been loaded, the system will by default incrementally process the table by checking only the append portion for constraint violations. However, there are some situations in which the system will decide that full processing (by checking the entire table for constraints violations) is necessary to ensure data integrity. There is also a situation in which user needs to explicitly request incremental processing by specifying the INCREMENTAL option. See Notes for details.

The SET INTEGRITY statement is under transaction control.

Invocation

This statement can be embedded in an application program or issued through the use of dynamic SQL statements. It is an executable statement that can be dynamically prepared. However, if the bind option DYNAMICRULES BIND applies, the statement cannot be dynamically prepared (SQLSTATE 42509).

Authorization

The privileges required to execute SET INTEGRITY depend on the use of the statement, as outlined below:

  1. Turn off integrity checking.

    The privileges of the authorization ID of the statement must include at least one of the following:

  2. Both turn on integrity checking and carry out checking.

    The privileges of the authorization ID of the statement must include at least one of the following:

  3. Turn on integrity checking without first carrying out checking.

    The authorization ID of the statement must have at least one of the following:

Syntax

                  (1)
>>-SET--INTEGRITY----------------------------------------------->
 
             .-,-------------.
             V               |
>-----+-FOR-----table-name---+---+-OFF---------------------------------------+--+>
      |                          +-TO DATALINK RECONCILE PENDING-------------+  |
      |                          '-IMMEDIATE CHECKED--+--------------------+-'  |
      |                                               '-| check-options |--'    |
      |      .-,------------------------------------.                           |
      |      V                                      |                           |
      '-FOR-----table-name--| integrity-options |---+--IMMEDIATE UNCHECKED-- ---'
 
>--------------------------------------------------------------><
 
check-options
 
|--*---+-------------+---*---+-----------------+---*------------>
       '-INCREMENTAL-'       '-FORCE GENERATED-'
 
>-----+----------------------+----------------------------------|
      '-| exception-clause |-'
 
exception-clause
 
                      .-,----------------------------------.
                      V                                    |
|---FOR--EXCEPTION-------IN--table-name--USE--table-name---+----|
 
integrity-options
 
|---+-ALL----------------------------------+--------------------|
    |  .-,-------------------------------. |
    |  V                                 | |
    '----+-FOREIGN KEY----------------+--+-'
         +-CHECK----------------------+
         +-DATALINK RECONCILE PENDING-+
         +-SUMMARY--------------------+
         '-GENERATED COLUMN-----------'
 

Notes:

  1. For compatibility with previous versions, the keyword CONSTRAINTS will continue to be supported.

Description

table-name
Identifies a table for integrity processing. It must be a table described in the catalog and must not be a view, catalog table, or typed table.

OFF
Specifies that the tables are to have their foreign key constraints, check constraints, and column generation turned off and are, therefore to be placed into the check pending state. If it is a summary table, then immediate refreshing is turned off (if applicable) and the summary table is placed into check pending state.

Note that it is possible that a table may already be in the check pending state with only one type of integrity checking turned off; in such a situation the other type of integrity checking will also be turned off.

If any table in the list is a parent table, the check pending state for foreign key constraints is extended to all dependent and descendent tables.

If any table in the list is an underlying table of a summary table, the check pending state is extended to such summary tables.

Only very limited activity is allowed on a table that is in the check pending state. Notes lists the restrictions.

TO DATALINK RECONCILE PENDING
Specifies that the tables are to have DATALINK integrity constraint checking turned off and the tables placed in check pending state. If the table is already in DataLink Reconcile Not Possible (DRNP) state, it remains in this state with check pending. Otherwise, the table is set to DataLink Reconcile Pending (DRP) state.

Dependent and descendent table are not affected when this option is specified.

IMMEDIATE CHECKED
Specifies that the table is to have its integrity checking turned on and that the integrity checking that was deferred is to be carried out. This is done in accordance with the information set in the STATUS and CONST_CHECKED columns of the SYSCAT.TABLES catalog. That is:

If it is a summary table, then the data is checked against the query and refreshed as necessary.

DATALINK values are not checked, even when the table is in DRP or DRNP state. The RECONCILE command or API should be used to perform the reconciliation of DATALINK values. The table will be taken out of check pending state but continue to have the DRP or DRNP flag set. This makes the table usable while the reconciliation of DATALINK values can be deferred to another time.

check-options

FORCE GENERATED
If the table includes generated columns, the values are computed based on the expression and stored in the column. If this clause is not specified, the current values are compared to the computed value of the expression as if an equality check constraint existed.

INCREMENTAL
Specifies the application of deferred integrity checks on the appended portion (if any) of the table. If such a request cannot be satisfied (i.e. the system detects that the whole table needs to be checked for data integrity), an error (SQLSTATE 55019) will be returned. If the attribute is not specified, the system will determine if incremental processing is possible; if not, the whole table will be checked. See Notes for situations in which system will favor full processing (checking whole table for integrity) over incremental processing. Also, see Notes for situations in which the INCREMENTAL option is necessary and situations in which it cannot be specified.

If the table is not in the check pending state, an error (SQLSTATE 55019) is returned.

exception-clause

FOR EXCEPTION
Indicates that any row that is in violation of a foreign key constraint or a check constraint will be copied to an exception table and deleted from the original table. See Appendix N, Exception Tables for more information on these user-defined tables. Even if errors are detected the constraints are turned back on again and the table is taken out of the check pending state. A warning (SQLSTATE 01603) is issued to indicate that one or more rows have been moved to the exception tables.

If the FOR EXCEPTION clause is not specified and any constraints are violated, then only the first violation detected is returned to the user (SQLSTATE 23514). In the case of a violation in any table, all the tables are left in the check pending state, as they were before the execution of the statement. This clause cannot be specified if the table-name is a summary table (SQLSTATE 42997).

IN table-name
Specifies the table from which rows that violate constraints are to be copied. There must be one exception table specified for each table being checked.

USE table-name
Specifies the exception table into which error rows are to be copied.

integrity-options
Used to define the integrity options that are set to IMMEDIATE UNCHECKED.

ALL
This indicates that all integrity-options are to be turned on.

FOREIGN KEY
This indicates that foreign key constraints are to be turned on.

CHECK
This indicates that check constraints are to be turned on.

DATALINK RECONCILE PENDING
This indicates that DATALINK integrity constraints are to be turned on.

SUMMARY
This indicates that immediate refreshing should be turned on for a summary table with the REFRESH IMMEDIATE attribute.

GENERATED COLUMN
This indicates that generated columns are to be turned on.

IMMEDIATE UNCHECKED
Specifies one of the following:

The state change is not extended to any tables not explicitly included in the list.

If the parent of a dependent table is in the check pending state, the foreign key constraints of a dependent table cannot be marked to bypass checking (the check constraints checking can be bypassed).

The implications with respect to data integrity should be considered before using this option. See Notes.

Notes

Example

Example 1:  The following is an example of a query that gives us information about the check pending state of tables. SUBSTR is used to extract the first 2 bytes of the CONST_CHECKED column of SYSCAT.TABLES. The first byte represents foreign key constraints, and the second byte represents check constraints.

   SELECT TABNAME,
     SUBSTR( CONST_CHECKED, 1, 1 ) AS FK_CHECKED,
     SUBSTR( CONST_CHECKED, 2, 1 ) AS CC_CHECKED
     FROM SYSCAT.TABLES
     WHERE STATUS = 'C'

Example 2:  Set tables T1 and T2 in the check pending state:

   SET INTEGRITY FOR T1, T2 OFF

Example 3:  Check the integrity for T1 and get the first violation only:

   SET INTEGRITY FOR T1 IMMEDIATE CHECKED    

Example 4:  Check the integrity for T1 and T2 and put the violating rows into exception tables E1 and E2:

   SET INTEGRITY FOR T1, T2 IMMEDIATE CHECKED
      FOR EXCEPTION IN T1 USE E1,
                    IN T2 USE E2

Example 5:  Enable FOREIGN KEY constraint checking in T1 and CHECK constraint checking in T2 to be bypassed with the IMMEDIATE CHECKED option:

   SET INTEGRITY FOR T1 FOREIGN KEY,
                     T2 CHECK IMMEDIATE UNCHECKED

Example 6:  Add a check constraint and a foreign key to the EMP_ACT table, using two ALTER TABLE statements. To perform constraint checking in a single pass of the table, integrity checking is turned off before the ALTER statements and checked after execution.

   SET INTEGRITY FOR EMP_ACT OFF;
   ALTER TABLE EMP_ACT ADD CHECK (EMSTDATE <= EMENDATE);
   ALTER TABLE EMP_ACT ADD FOREIGN KEY (EMPNO) REFERENCES EMPLOYEE;
   SET INTEGRITY FOR EMP_ACT IMMEDIATE CHECKED

Example 7: Set integrity for generated columns.

   SET INTEGRITY FOR T1 IMMEDIATE CHECKED 
      FORCE GENERATED


Footnotes:

105
The SET INTEGRITY statement, rather than the SET CONSTRAINTS statement, is the preferred method for working with integrity checking in DB2.


[ Top of Page | Previous Page | Next Page ]