1. Oracle Specifics

Using Integrity Constraints

When to Use Integrity Constraints

 

Taking Advantage of Integrity Constraints

 

Using NOT NULL Integrity Constraints

By default, all columns can contain nulls. Only define NOT NULL constraints for columns of a table that absolutely require values at all times.

 

Using UNIQUE Key Integrity Constraints

Choose unique keys carefully. In many situations, unique keys are incorrectly comprised of columns that should be part of the table's primary key (see the previous section for more information about primary keys). When deciding whether to use a UNIQUE key constraint, use the rule that a UNIQUE key constraint is only required to prevent the duplication of the key values within the rows of the table. The data in a unique key is such that it cannot be duplicated in the table.

Note:

Although UNIQUE key constraints allow the input of nulls, because of the search mechanism for UNIQUE constraints on more than one column, you cannot have identical values in the non-null columns of a partially null composite UNIQUE key constraint.

Do not confuse the concept of a unique key with that of a primary key. Primary keys are used to identify each row of the table uniquely. Therefore, unique keys should not have the purpose of identifying rows in the table.

Some examples of good unique keys include

 

Using Referential Integrity Constraints

Whenever two tables are related by a common column (or set of columns), define a PRIMARY or UNIQUE key constraint on the column in the parent table, and define a FOREIGN KEY constraint on the column in the child table, to maintain the relationship between the two tables. Depending on this relationship, you may want to define additional integrity constraints including the foreign key, as listed in the following.

 

Relationships Between Parent and Child Tables

Several relationships between parent and child tables can be determined by the other types of integrity constraints defined on the foreign key in the child table.

No Constraints on the Foreign Key

An example of such a relationship is shown in the above picture between EMP and DEPT; each department (parent key) has many employees (foreign key), and some employees might not be in a department (nulls in the foreign key).

NOT NULL Constraint on the Foreign Key

When nulls are not allowed in a foreign key, each row in the child table must explicitly reference a value in the parent key because nulls are not allowed in the foreign key. In this case, employees must have a reference to a specific department.

 

UNIQUE Constraint on the Foreign Key

For example, assume that the EMP table had a column named MEMBERNO, referring to an employee's membership number in the company's insurance plan. Also, a table named INSURANCE has a primary key named MEMBERNO, and other columns of the table keep respective information relating to an employee's insurance policy. The MEMBERNO in the EMP table should be both a foreign key and a unique key:

UNIQUE and NOT NULL Constraints on the Foreign Key

When both UNIQUE and NOT NULL constraints are defined on the foreign key, only one row in the child table can reference a parent key value. Because nulls are not allowed in the foreign key, each row in the child table must explicitly reference a value in the parent key.

This model establishes a "one-to-one" relationship between the parent and foreign keys that does not allow undetermined values (nulls) in the foreign key.

If you expand the previous example by adding a NOT NULL constraint on the MEMBERNO column of the EMP table, in addition to guaranteeing that each employee has a unique membership number, you also ensure that no undetermined values (nulls) are allowed in the MEMBERNO column of the EMP table.

Multiple FOREIGN KEY Constraints

Oracle allows a column to be referenced by multiple FOREIGN KEY constraints; effectively, there is no limit on the number of dependent keys. This situation might be present if a single column is part of two different composite foreign keys.

 

Using CHECK Integrity Constraints

 

 

Examples of appropriate CHECK constraints include the following:

Restrictions on CHECK Constraints

Designing CHECK Constraints

When using CHECK constraints, consider the ANSI/ISO standard, which states that a CHECK constraint is violated only if the condition evaluates to false; true and unknown values do not violate a check condition. Therefore, make sure that a CHECK constraint that you define actually enforces the rule you need enforced.

For example, consider the following CHECK constraint:

	CHECK (sal > 0 OR comm >= 0)

At first glance, this rule may be interpreted as "do not allow a row in the EMP table unless the employee's salary is greater than zero or the employee's commission is greater than or equal to zero." However, note that if a row is inserted with a null salary and a negative commission, the row does not violate the CHECK constraint because the entire check condition is evaluated as unknown. In this particular case, you can account for such violations by placing NOT NULL integrity constraints on both the SAL and COMM columns.


Note:

If you are not sure when unknown values result in NULL conditions, review the truth tables for the logical operators AND and OR.


Nulls in Conditions

The following table shows examples of various evaluations involving nulls in conditions. If the conditions evaluating to UNKNOWN were used in a WHERE clause of a SELECT statement, then no rows would be returned for that query.

 

Table 2-9 Conditions Containing Nulls

If A is:

Condition

Evaluates to:

10

a IS NULL

FALSE

10

a IS NOT NULL

TRUE

NULL

a IS NULL

TRUE

NULL

a IS NOT NULL

FALSE

10

a = NULL

UNKNOWN

10

a != NULL

UNKNOWN

NULL

a = NULL

UNKNOWN

NULL

a != NULL

UNKNOWN

NULL

a = 10

UNKNOWN

NULL

a != 10

UNKNOWN

 

NOT Operator able 3-6 NOT Truth Table

NOT

TRUE

FALSE

UNKNOWN

 

FALSE

TRUE

UNKNOWN

 

AND Operator Table 3-7 AND Truth Table

AND

TRUE

FALSE

UNKNOWN

TRUE

TRUE

FALSE

UNKNOWN

FALSE

FALSE

FALSE

FALSE

UNKNOWN

UNKNOWN

FALSE

UNKNOWN

 

OR Operator Table 3-8 OR Truth Table

OR

TRUE

FALSE

UNKNOWN

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

UNKNOWN

UNKNOWN

TRUE

UNKNOWN

UNKNOWN

 

Multiple CHECK Constraints

A single column can have multiple CHECK constraints that reference the column in its definition. There is no limit to the number of CHECK constraints that can be defined that reference a column.

CHECK and NOT NULL Integrity Constraints

	CHECK (column_name IS NOT NULL)

For example, the following expression of a CHECK integrity constraint allows a key value in the composite key made up of columns C1 and C2 to contain either all nulls or all values:

		CHECK ((c1 IS NULL AND c2 IS NULL) OR 
			(c1 IS NOT NULL AND c2 IS NOT NULL))

Defining Integrity Constraints

Define an integrity constraint using the constraint clause of the SQL commands CREATE TABLE or ALTER TABLE.

The CREATE TABLE Command

The following examples of CREATE TABLE statements show the definition of several integrity constraints:

CREATE TABLE dept (
	deptno  NUMBER(3) PRIMARY KEY, 
	dname   VARCHAR2(15), 
	loc     VARCHAR2(15), 
        CONSTRAINT dname_ukey UNIQUE (dname, loc), 
        CONSTRAINT loc_check1 
                CHECK (loc IN ('NEW YORK', 'BOSTON', 'CHICAGO')));
CREATE TABLE emp ( 
	empno    NUMBER(5) PRIMARY KEY, 
	ename    VARCHAR2(15) NOT NULL, 
	job      VARCHAR2(10), 
	mgr      NUMBER(5) CONSTRAINT mgr_fkey 
                 REFERENCES emp,
	hiredate DATE, 
	sal      NUMBER(7,2), 
	comm     NUMBER(5,2), 
	deptno   NUMBER(3) NOT NULL 
		   CONSTRAINT dept_fkey 
		       REFERENCES dept ON DELETE CASCADE);

 

The ALTER TABLE Command

You can also define integrity constraints using the constraint clause of the ALTER TABLE command. For example, the following examples of ALTER TABLE statements show the definition of several integrity constraints:

ALTER TABLE dept 
	ADD PRIMARY KEY (deptno); 
ALTER TABLE emp 
	ADD CONSTRAINT dept_fkey FOREIGN KEY (deptno) REFERENCES dept 
	MODIFY (ename VARCHAR2(15) NOT NULL);

 

Restrictions with the ALTER TABLE Command

Because data is likely to be in the table at the time an ALTER TABLE statement is issued, there are several restrictions to be aware of. Table 9-1 lists each type of constraint and the associated restrictions with the ALTER TABLE command.

Table 9-1 Restrictions for Defining Integrity Constraints with the ALTER TABLE Command

Type of Constraint

Added to Existing Columns of the Table

Added with New Columns to the Table

NOT NULL

Cannot be defined if any row contains a null value for this column*

Cannot be defined if the table contains any rows

UNIQUE

Cannot be defined if duplicate values exist in the key*

Always OK

PRIMARY KEY

Cannot be defined if duplicate or null values exist in the key*

Cannot be defined if the table contains any rows

FOREIGN KEY

Cannot be defined if the foreign key has values that do not reference a parent key value*

Always OK

CHECK

Cannot be defined if the volume has values that do not comply with the check condition*

Always OK

Assumes DISABLE clause not included in statement.

If you attempt to define a constraint with an ALTER TABLE statement and violate one of these restrictions, the statement is rolled back and an informative error is returned explaining the violation.

Required Privileges

The creator of a constraint must have the ability to create tables (that is, the CREATE TABLE or CREATE ANY TABLE system privilege) or the ability to alter the table (that is, the ALTER object privilege for the table or the ALTER ANY TABLE system privilege) with the constraint. Additionally, UNIQUE key and PRIMARY KEY integrity constraints require that the owner of the table have either a quota for the tablespace that contains the associated index or the UNLIMITED TABLESPACE system privilege. FOREIGN KEY integrity constraints also require some additional privileges.

Naming Integrity Constraints

"Listing Integrity Constraint Definitions" on page 9-27 for examples of data dictionary views.

Enabling and Disabling Constraints Upon Definition

By default, whenever an integrity constraint is defined in a CREATE or ALTER TABLE statement, the constraint is automatically enabled (enforced) by Oracle unless it is specifically created in a disabled state using the DISABLE clause.

See Also:

"Enabling and Disabling Key Integrity Constraints" on page 9-22 for more information about important issues for enabling and disabling constraints.

UNIQUE Key, PRIMARY KEY, and FOREIGN KEY

When defining UNIQUE key, PRIMARY KEY, and FOREIGN KEY integrity constraints, you should be aware of several important issues and prerequisites. For information about defining and managing FOREIGN KEY constraints

See Also:

"Managing FOREIGN KEY Integrity Constraints" on page 9-25. UNIQUE key and PRIMARY KEY constraints are usually enabled by the database administrator, and the Oracle8 Administrator's Guide .

 

Enabling and Disabling Integrity Constraints

This section explains the mechanisms and procedures for manually enabling and disabling integrity constraints.

enabled constraint

When a constraint is enabled, the rule defined by the constraint is enforced on the data values in the columns that define the constraint. The definition of the constraint is stored in the data dictionary.

disabled constraint

When a constraint is disabled, the rule defined by the constraint is not enforced on the data values in the columns included in the constraint; however, the definition of the constraint is retained in the data dictionary.

In summary, an integrity constraint can be thought of as a statement about the data in a database. This statement is always true when the constraint is enabled; however, the statement may or may not be true when the constraint is disabled because data in violation of the integrity constraint can be in the database.

 

Why Enable or Disable Constraints?

To enforce the rules defined by integrity constraints, the constraints should always be enabled; however, in certain situations, it is desirable to disable the integrity constraints of a table temporarily for performance reasons. For example:

In cases such as these, integrity constraints may be temporarily turned off to improve the performance of the operation.

Integrity Constraint Violations

If a row of a table does not adhere to an integrity constraint, this row is said to be in violation of the constraint and is known as an exception to the constraint. If any exceptions exist, the constraint cannot be enabled. The rows that violate the constraint must be either updated or deleted in order for the constraint to be enabled.

Exceptions for a specific integrity constraint can be identified while attempting to enable the constraint. This procedure is discussed in the section "Exception Reporting" on page 9-23.

On Definition

When you define an integrity constraint in a CREATE TABLE or ALTER TABLE statement, you can enable the constraint by including the ENABLE clause in its definition or disable it by including the DISABLE clause in its definition. If neither the ENABLE nor the DISABLE clause is included in a constraint's definition, Oracle automatically enables the constraint.

Enabling Constraints

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

CREATE TABLE emp (
    empno NUMBER(5) PRIMARY KEY,   . . . );
 ALTER TABLE emp 
    ADD PRIMARY KEY (empno);

An ALTER TABLE statement that defines and attempts to enable an integrity constraint may fail because rows of the table may violate the integrity constraint. In this case, the statement is rolled back and the constraint definition is not stored and not enabled.

Disabling Constraints

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

CREATE TABLE emp (  
	empno NUMBER(5) PRIMARY KEY DISABLE,   . . . ); 
ALTER TABLE emp 
	ADD PRIMARY KEY (empno) DISABLE;

An ALTER TABLE statement that defines and disables an integrity constraints never fails. The definition of the constraint is always allowed because its rule is not enforced.

Enabling and Disabling Defined Integrity Constraints

Use the ALTER TABLE command to

Enabling Disabled Constraints

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

ALTER TABLE dept  
	ENABLE CONSTRAINT dname_ukey; 
 
ALTER TABLE dept 
	ENABLE PRIMARY KEY,  
	ENABLE UNIQUE (dname, loc); 

An ALTER TABLE statement that attempts to enable an integrity constraint fails when the rows of the table violate the integrity constraint. In this case, the statement is rolled back and the constraint is not enabled.

 

Disabling Enabled Constraints

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

ALTER TABLE dept  
	DISABLE CONSTRAINT dname_ukey;
 
ALTER TABLE dept 
	DISABLE PRIMARY KEY,  
	DISABLE UNIQUE (dname, loc);

Tip - Using the Data Dictionary for Reference:

The example statements in the previous sections require that you have some information about a constraint to enable or disable it. For example, the first statement of each section requires that you know the constraint's name, while the second statement of each section requires that you know the unique key's column list. If you do not have such information, you can query one of the data dictionary views defined for constraints; for more information about these views, see "Listing Integrity Constraint Definitions" on page 9-27 and Oracle8 Reference.


 

Enabling and Disabling Key Integrity Constraints

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