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

Enabling and Disabling Integrity Constraints

The following CREATE TABLE and ALTER TABLE statements both define and enable integrity constraints:

CREATE TABLE Emp_tab (

Empno NUMBER(5) PRIMARY KEY);

ALTER TABLE Emp_tab

ADD PRIMARY KEY (Empno);

An ALTER TABLE statement that tries to enable an integrity constraint will fail if any existing rows of the table violate the integrity constraint. The statement is rolled back and the constraint definition is not stored and not enabled.

See Also: "Fixing Constraint Exceptions" on page 3-23 for more information about rows that violate integrity constraints

Creating Disabled Constraints

The following CREATE TABLE and ALTER TABLE statements both define and disable integrity constraints:

CREATE TABLE Emp_tab (

Empno NUMBER(5) PRIMARY KEY DISABLE);

ALTER TABLE Emp_tab

ADD PRIMARY KEY (Empno) DISABLE;

Use this technique when creating tables that will be loaded with large amounts of data before anybody else accesses them, particularly if you need to cleanse data after loading it, or need to fill empty columns with sequence numbers or parent/child relationships.

An ALTER TABLE statement that defines and disables an integrity constraints never fails, because its rule is not enforced.

Enabling and Disabling Existing Integrity Constraints

Use the ALTER TABLE command to:

Enable a disabled constraint, using the ENABLE clause.

Disable an enabled constraint, using the DISABLE clause.

Enabling Existing Constraints

Once you have finished cleansing data and filling empty columns, you can enable constraints that were disabled during data loading.

Maintaining Data Integrity Through Constraints 3-21

Enabling and Disabling Integrity Constraints

The following statements are examples of statements that enable disabled integrity constraints:

ALTER TABLE Dept_tab

ENABLE CONSTRAINT Dname_ukey;

ALTER TABLE Dept_tab

ENABLE PRIMARY KEY

ENABLE UNIQUE (Dname)

ENABLE UNIQUE (Loc);

An ALTER TABLE statement that attempts to enable an integrity constraint fails if any of the table rows violate the integrity constraint. The statement is rolled back and the constraint is not enabled.

See Also: "Fixing Constraint Exceptions" on page 3-23 for more information about rows that violate integrity constraints

Disabling Existing Constraints

If you need to perform a large load or update when a table already contains data, you can temporarily disable constraints to improve performance of the bulk operation.

The following statements are examples of statements that disable enabled integrity constraints:

ALTER TABLE Dept_tab

DISABLE CONSTRAINT Dname_ukey;

ALTER TABLE Dept_tab

DISABLE PRIMARY KEY

DISABLE UNIQUE (Dname)

DISABLE UNIQUE (Loc);

Tip: Using the Data Dictionary to Find Constraints

The preceding examples require that you know the relevant constraint names and which columns they affect. To find this information, you can query one of the data dictionary views defined for constraints, USER_CONSTRAINTS or USER_CONS_ COLUMNS. For more information about these views, see "Viewing Definitions of Integrity Constraints" on page 3-28 and Oracle Database Reference.

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

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