1 of 32

Maintaining Data Integrity

13

Copyright © Oracle Corporation, 2001. All rights reserved.

2 of 32

Objectives

After completing this lesson, you should be able to do the following:

    • Implement data integrity constraints
    • Maintain integrity constraints
    • Obtain constraint information from the data dictionary

13-2

Copyright © Oracle Corporation, 2001. All rights reserved.

3 of 32

Data Integrity

Application�code

Table

Data

Integrity�constraint

Database�trigger

13-3

Copyright © Oracle Corporation, 2001. All rights reserved.

4 of 32

13-4

Copyright © Oracle Corporation, 2001. All rights reserved.

5 of 32

Types of Constraints

Constraint�

NOT NULL��

UNIQUE����PRIMARY KEY���FOREIGN KEY��

CHECK��

Description�

Specifies that a column cannot contain null values�

Designates a column or combination of columns as unique�

Designates a column or combination of columns as the table’s primary key��Designates a column or combination of columns as the foreign key in a referential integrity constraint��Specifies a condition that each row of the table must satisfy�

13-5

Copyright © Oracle Corporation, 2001. All rights reserved.

6 of 32

Constraint States

ENABLE�NOVALIDATE

Existing data

New data

DISABLE�NOVALIDATE

DISABLE�VALIDATE

=

=

ENABLE�VALIDATE

13-6

Copyright © Oracle Corporation, 2001. All rights reserved.

7 of 32

13-7

Copyright © Oracle Corporation, 2001. All rights reserved.

8 of 32

Constraint Checking

DML statement

Check nondeferred constraints

COMMIT

Check deferred constraints

13-8

Copyright © Oracle Corporation, 2001. All rights reserved.

9 of 32

Defining Constraints �Immediate or Deferred

    • Use the SET CONSTRAINTS statement to make constraints either DEFERRED or IMMEDIATE.
    • The ALTER SESSION statement also has clauses to SET CONSTRAINTS to DEFERRED or IMMEDIATE.

13-9

Copyright © Oracle Corporation, 2001. All rights reserved.

10 of 32

Primary and Unique Key Enforcement

Is an index �available �for use?

Yes

No

No

Yes

Yes

No

Create nonunique� index

Create unique� index

Do not use � index

Use existing � index

Key�enabled?

Constraint�deferrable?

Constraint

Deferrable?

Is the index

nonunique?

Yes

No/Yes

No

13-10

Copyright © Oracle Corporation, 2001. All rights reserved.

11 of 32

Foreign Key Considerations

Appropriate Solution

Desired Action

Drop parent table Cascade constraints

Truncate parent table Disable or drop foreign key

Perform DML on child table Ensure that the tablespace containing the parent key is online

Use the CASCADE CONSTRAINTS clause

Drop tablespace containing�parent table

13-11

Copyright © Oracle Corporation, 2001. All rights reserved.

12 of 32

13-12

Copyright © Oracle Corporation, 2001. All rights reserved.

13 of 32

Defining Constraints While�Creating a Table

CREATE TABLE hr.employee(�id NUMBER(7) � CONSTRAINT employee_id_pk PRIMARY KEY� DEFERRABLE � USING INDEX� STORAGE(INITIAL 100K NEXT 100K)� TABLESPACE indx,

last_name VARCHAR2(25) � CONSTRAINT employee_last_name_nn NOT NULL,

dept_id NUMBER(7))

TABLESPACE users;

13-13

Copyright © Oracle Corporation, 2001. All rights reserved.

14 of 32

13-14

Copyright © Oracle Corporation, 2001. All rights reserved.

15 of 32

13-15

Copyright © Oracle Corporation, 2001. All rights reserved.

16 of 32

13-16

Copyright © Oracle Corporation, 2001. All rights reserved.

17 of 32

Guidelines for Defining Constraints

    • Primary and unique constraints:
      • Place indexes in a separate tablespace.
      • Use nonunique indexes if bulk loads are frequent.
    • Self-referencing foreign keys:
      • Define or enable foreign keys after the initial load.
      • Defer constraint checking.

13-17

Copyright © Oracle Corporation, 2001. All rights reserved.

18 of 32

Enabling Constraints

    • No locks on table
    • Primary and unique keys must use nonunique indexes

ENABLE �NOVALIDATE

ALTER TABLE hr.departments�ENABLE NOVALIDATE CONSTRAINT dept_pk;

13-18

Copyright © Oracle Corporation, 2001. All rights reserved.

19 of 32

13-19

Copyright © Oracle Corporation, 2001. All rights reserved.

20 of 32

13-20

Copyright © Oracle Corporation, 2001. All rights reserved.

21 of 32

Enabling Constraints

    • Locks the table
    • Can use unique or nonunique indexes
    • Needs valid table data

ENABLE �VALIDATE

ALTER TABLE hr.employees�ENABLE VALIDATE CONSTRAINT emp_dept_fk;

13-21

Copyright © Oracle Corporation, 2001. All rights reserved.

22 of 32

13-22

Copyright © Oracle Corporation, 2001. All rights reserved.

23 of 32

Using the EXCEPTIONS Table

    • Create the EXCEPTIONS table by running the utlexcpt1.sql script.
    • Execute the ALTER TABLE statement with EXCEPTIONS option.
    • Use subquery on EXCEPTIONS to locate rows with invalid data.
    • Rectify the errors.
    • Reexecute ALTER TABLE to enable the constraint.

13-23

Copyright © Oracle Corporation, 2001. All rights reserved.

24 of 32

13-24

Copyright © Oracle Corporation, 2001. All rights reserved.

25 of 32

13-25

Copyright © Oracle Corporation, 2001. All rights reserved.

26 of 32

Obtaining Constraint Information

Obtain information about constraints by querying the following views:

    • DBA_CONSTRAINTS
    • DBA_CONS_COLUMNS

13-26

Copyright © Oracle Corporation, 2001. All rights reserved.

27 of 32

13-27

Copyright © Oracle Corporation, 2001. All rights reserved.

28 of 32

13-28

Copyright © Oracle Corporation, 2001. All rights reserved.

29 of 32

Summary

In this lesson, you should have learned how to:

    • Implement data integrity
    • Use an appropriate strategy to create and maintain constraints
    • Obtain information from the data dictionary

13-29

Copyright © Oracle Corporation, 2001. All rights reserved.

30 of 32

Practice 13 Overview

This practice covers the following topics:

    • Creating constraints
    • Enabling unique constraints
    • Creating an EXCEPTIONS table
    • Identifying existing constraint violations in a table, correcting the errors, and reenabling the constraints

13-30

Copyright © Oracle Corporation, 2001. All rights reserved.

31 of 32

13-31

Copyright © Oracle Corporation, 2001. All rights reserved.

32 of 32

13-32

Copyright © Oracle Corporation, 2001. All rights reserved.