Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
29
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

Types of Integrity Constraints

Oracle query optimizer can use declarations to learn more about data to improve overall query performance. (Also, taking integrity rules out of application code and database triggers guarantees that checks are only made when necessary.)

Flexibility for Data Loads and Identification of Integrity Violations

You can disable integrity constraints temporarily so that large amounts of data can be loaded without the overhead of constraint checking. When the data load is complete, you can easily enable the integrity constraints, and you can automatically report any new rows that violate integrity constraints to a separate exceptions table.

The Performance Cost of Integrity Constraints

The advantages of enforcing data integrity rules come with some loss in performance. In general, the cost of including an integrity constraint is, at most, the same as executing a SQL statement that evaluates the constraint.

Types of Integrity Constraints

You can use the following integrity constraints to impose restrictions on the input of column values:

NOT NULL Integrity Constraints

UNIQUE Key Integrity Constraints

PRIMARY KEY Integrity Constraints

Referential Integrity Constraints

CHECK Integrity Constraints

NOT NULL Integrity Constraints

By default, all columns in a table allow nulls. Null means the absence of a value. A NOT NULL constraint requires a column of a table contain no null values. For example, you can define a NOT NULL constraint to require that a value be input in the last_name column for every row of the employees table.

Figure 21–2 illustrates a NOT NULL integrity constraint.

Data Integrity 21-7

Types of Integrity Constraints

Figure 21–2 NOT NULL Integrity Constraints

 

 

Table EMP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EMPNO

 

ENAME

 

JOB

 

MGR

 

HIREDATE

 

SAL

 

COMM

 

DEPTNO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7329

 

SMITH

 

CEO

 

 

 

17–DEC–85

 

9,000.00

 

 

 

20

 

 

 

7499

 

ALLEN

 

VP_SALES

 

7329

 

20–FEB–90

 

7,500.00

 

100.00

 

30

 

 

 

7521

 

WARD

 

MANAGER

 

7499

 

22–FEB–90

 

5,000.00

 

200.00

 

30

 

 

 

7566

 

JONES

 

SALESMAN

 

7521

 

02–APR–90

 

2,975.00

 

400.00

 

30

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NOT NULL CONSTRAINT

 

Absence of NOT

 

(no row may contain a null

 

NULL Constraint

value for this column)

 

(any row can contain

 

 

null for this column)

UNIQUE Key Integrity Constraints

A UNIQUE key integrity constraint requires that every value in a column or set of columns (key) be unique—that is, no two rows of a table have duplicate values in a specified column or set of columns.

For example, in Figure 21–3 a UNIQUE key constraint is defined on the DNAME column of the departments table to disallow rows with duplicate department names.

21-8 Oracle9i Database Concepts

Types of Integrity Constraints

Figure 21–3 A UNIQUE Key Constraint

UNIQUE Key Constraint

(no row may duplicate a value in the constraint's column)

Table DEPT

DEPTNO

DNAME

LOC

 

20

RESEARCH

DALLAS

30

SALES

NEW YORK

40

MARKETING

BOSTON

 

 

INSERT

 

 

INTO

50

SALES

NEW YORK

60

 

BOSTON

This row violates the UNIQUE key constraint, because "SALES" is already present in another row; therefore, it is not allowed in the table.

This row is allowed because a null value is entered for the DNAME column; however, if a NOT NULL constraint is also defined on the DNAME column, this row is not allowed.

Unique Keys

The columns included in the definition of the UNIQUE key constraint are called the unique key. Unique key is often incorrectly used as a synonym for the terms

UNIQUE key constraint or UNIQUE index. However, note that key refers only to the column or set of columns used in the definition of the integrity constraint.

If the UNIQUE key consists of more than one column, that group of columns is said to be a composite unique key. For example, in Figure 21–4 the customer table has a UNIQUE key constraint defined on the composite unique key: the area and phone columns.

Data Integrity 21-9

Types of Integrity Constraints

Figure 21–4 A Composite UNIQUE Key Constraint

Table CUSTOMER

CUSTNO

CUSTNAME

... Other Columns ...

AREA

PHONE

230

OFFICE SUPPLIES

 

303

506–7000

245

ORACLE CORP

 

415

506–7000

257

INTERNAL SYSTEMS

 

303

341–8100

 

 

INSERT

 

 

 

INTO

 

268

AEA CONSTRUCTION

415

506–7000

270

WW MANUFACTURING

 

506–7000

Composite UNIQUE Key Constraint

(no row may duplicate a set of values

in the key)

This row violates the UNIQUE key constraint, because "415/506-7000" is already present in another row; therefore, it is not allowed in the tabl

This row is allowed because a null value is entered for the AREA column; however, if a NOT NULL constraint is also defined on the AREA column, then this row is not allowed.

This UNIQUE key constraint lets you enter an area code and telephone number any number of times, but the combination of a given area code and given telephone number cannot be duplicated in the table. This eliminates unintentional duplication of a telephone number.

UNIQUE Key Constraints and Indexes

Oracle enforces unique integrity constraints with indexes. For example, in Figure 21–4, Oracle enforces the UNIQUE key constraint by implicitly creating a unique index on the composite unique key. Therefore, composite UNIQUE key constraints have the same limitations imposed on composite indexes: up to 32 columns can constitute a composite unique key.

21-10 Oracle9i Database Concepts

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