Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Altering Integrity Constraints

Guidelines for Enabling and Disabling Key Integrity Constraints

When enabling or disabling UNIQUE, PRIMARY KEY, and FOREIGN KEY integrity constraints, you should be aware of several important issues and prerequisites. UNIQUE key and PRIMARY KEY constraints are usually managed by the database administrator.

.

See Also: Oracle Database Administrator's Guide and "Managing FOREIGN KEY Integrity Constraints" on page 3-26

Fixing Constraint Exceptions

When you try to create or enable a constraint, and the statement fails because integrity constraint exceptions exist, the statement is rolled back. You cannot enable the constraint until all exceptions are either updated or deleted. To determine which rows violate the integrity constraint, include the EXCEPTIONS option in the

ENABLE clause of a CREATE TABLE or ALTER TABLE statement.

See Also: Oracle Database Administrator's Guide for more information about responding to constraint exceptions

Altering Integrity Constraints

Starting with Oracle8i, you can alter the state of an existing constraint with the

MODIFY CONSTRAINT clause.

See Also: Oracle Database SQL Reference for information on the parameters you can modify

MODIFY CONSTRAINT Example #1

The following commands show several alternatives for whether the CHECK constraint is enforced, and when the constraint checking is done:

CREATE TABLE X1_tab (a1 NUMBER CONSTRAINT y CHECK (a1>3) DEFERRABLE DISABLE);

ALTER TABLE X1_tab MODIFY CONSTRAINT Y_cnstrt ENABLE;

ALTER TABLE X1_tab MODIFY CONSTRAINT Y_cnstrt RELY;

ALTER TABLE X1_tab MODIFY CONSTRAINT Y_cnstrt INITIALLY DEFERRED;

ALTER TABLE X1_tab MODIFY CONSTRAINT Y_cnstrt ENABLE NOVALIDATE;

Maintaining Data Integrity Through Constraints 3-23

Altering Integrity Constraints

MODIFY CONSTRAINT Example #2

The following commands show several alternatives for whether the NOT NULL constraint is enforced, and when the checking is done:

CREATE TABLE X1_tab (A1 NUMBER CONSTRAINT Y_cnstrt

NOT NULL DEFERRABLE INITIALLY DEFERRED NORELY DISABLE);

ALTER TABLE X1_tab ADD CONSTRAINT One_cnstrt UNIQUE(A1)

DEFERRABLE INITIALLY IMMEDIATE RELY USING INDEX PCTFREE = 30

ENABLE VALIDATE;

ALTER TABLE X1_tab MODIFY UNIQUE(A1)

INITIALLY DEFERRED NORELY USING INDEX PCTFREE = 40

ENABLE NOVALIDATE;

ALTER TABLE X1_tab MODIFY CONSTRAINT One_cnstrt

INITIALLY IMMEDIATE RELY;

Modify Constraint Example #3

The following commands show several alternatives for whether the primary key constraint is enforced, and when the checking is done:

CREATE TABLE T1_tab (A1 INT, B1 INT);

ALTER TABLE T1_tab add CONSTRAINT P1_cnstrt PRIMARY KEY(a1) DISABLE;

ALTER TABLE T1_tab MODIFY PRIMARY KEY INITIALLY IMMEDIATE

USING INDEX PCTFREE = 30 ENABLE NOVALIDATE;

ALTER TABLE T1_tab MODIFY PRIMARY KEY

USING INDEX PCTFREE = 35 ENABLE;

ALTER TABLE T1_tab MODIFY PRIMARY KEY ENABLE NOVALIDATE;

Renaming Integrity Constraints

Because constraint names must be unique, even across multiple schemas, you can encounter problems when you want to clone a table and all its constraints, because the constraint name for the new table conflicts with the one for the original table.

Or, you might create a constraint with a default system-generated name, and later realize that you want to give the constraint a name that is easy to remember, so that you can easily enable and disable it.

One of the properties you can alter for a constraint is its name. The following SQL*Plus script finds the system-generated name for a constraint and changes it:

3-24 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g