13 copyright © oracle corporation, 2001. all rights reserved. maintaining data integrity

17
13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

Upload: rafe-park

Post on 18-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13Copyright © Oracle Corporation, 2001. All rights reserved.

Maintaining Data Integrity

Page 2: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-2 Copyright © Oracle Corporation, 2001. All rights reserved.

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

Page 3: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-3 Copyright © Oracle Corporation, 2001. All rights reserved.

Data Integrity

Applicationcode

Table

Data

Integrityconstraint

Databasetrigger

Page 4: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-5 Copyright © Oracle Corporation, 2001. All rights reserved.

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

Page 5: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-6 Copyright © Oracle Corporation, 2001. All rights reserved.

Constraint States

ENABLENOVALIDATE

Existing dataNew data

DISABLENOVALIDATE

DISABLEVALIDATE

=

=

ENABLEVALIDATE

Page 6: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-8 Copyright © Oracle Corporation, 2001. All rights reserved.

Constraint Checking

DML statement

Check nondeferred constraints

COMMIT

Check deferred constraints

Page 7: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-9 Copyright © Oracle Corporation, 2001. All rights reserved.

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.

Page 8: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-10 Copyright © Oracle Corporation, 2001. All rights reserved.

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

Keyenabled?

Constraintdeferrable?

ConstraintDeferrable?

Is the indexnonunique?

Yes

No/YesNo

Page 9: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-11 Copyright © Oracle Corporation, 2001. All rights reserved.

Foreign Key Considerations

Appropriate SolutionDesired 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 containingparent table

Page 10: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-13 Copyright © Oracle Corporation, 2001. All rights reserved.

Defining Constraints WhileCreating a Table

CREATE TABLE hr.employee(id NUMBER(7)

CONSTRAINT employee_id_pk PRIMARY KEYDEFERRABLE 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;

Page 11: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-17 Copyright © Oracle Corporation, 2001. All rights reserved.

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.

Page 12: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-18 Copyright © Oracle Corporation, 2001. All rights reserved.

Enabling Constraints

• No locks on table

• Primary and unique keys must use nonunique indexes

ENABLE NOVALIDATE

ALTER TABLE hr.departmentsENABLE NOVALIDATE CONSTRAINT dept_pk;

Page 13: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-21 Copyright © Oracle Corporation, 2001. All rights reserved.

Enabling Constraints

• Locks the table

• Can use unique or nonunique indexes

• Needs valid table data

ENABLE VALIDATE

ALTER TABLE hr.employeesENABLE VALIDATE CONSTRAINT emp_dept_fk;

Page 14: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-23 Copyright © Oracle Corporation, 2001. All rights reserved.

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.

Page 15: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-26 Copyright © Oracle Corporation, 2001. All rights reserved.

Obtaining Constraint Information

Obtain information about constraints by querying the following views:

• DBA_CONSTRAINTS• DBA_CONS_COLUMNS

Page 16: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-29 Copyright © Oracle Corporation, 2001. All rights reserved.

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

Page 17: 13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity

13-30 Copyright © Oracle Corporation, 2001. All rights reserved.

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