INSERT statement containing these values, you might want your user to be notified immediately after entering a value that violates a business rule. INSERT statement. However, you can design your application to verify the integrity of each value as it is entered and notify the user immediately in the event of a bad value.
By default, all columns can contain nulls. Only define NOT NULL constraints for columns of a table that absolutely require values at all times.
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 |
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
AREA and PHONE (the primary key is the customer number)
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. 

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.
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).
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 is defined on the foreign key, one row in the child table can reference a parent key value. This model allows nulls in 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:
EMP and INSURANCE tables (the FOREIGN KEY constraint) UNIQUE key constraint) 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.
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.
Examples of appropriate CHECK constraints include the following:
CHECK constraint on the SAL column of the EMP table so that no salary value is greater than 10000 CHECK constraint on the LOC column of the DEPT table so that only the locations "BOSTON", "NEW YORK", and "DALLAS" are allowed CHECK constraint on the SAL and COMM columns to compare the SAL and COMM values of a row and prevent the COMM value from being greater than the SAL value CHECK integrity constraint requires that a condition be true or unknown for every row of the table. SYSDATE, UID, USER, or USERENV SQL functions. LEVEL, PRIOR, or ROWNUM; 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 |
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.
|
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 |
TRUE |
FALSE |
UNKNOWN |
|
|
|
|
AND |
TRUE |
FALSE |
UNKNOWN |
|
TRUE |
|
|
|
|
FALSE |
|
|
|
|
UNKNOWN |
|
|
|
|
OR |
TRUE |
FALSE |
UNKNOWN |
|
TRUE |
|
|
|
|
FALSE |
|
|
|
|
UNKNOWN |
|
|
|
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.
NOT NULL integrity constraint is an example of a CHECK integrity constraint, where the condition is CHECK (column_name IS NOT NULL)
NOT NULL integrity constraints for a single column can, in practice, be written in two forms: using the NOT NULL constraint or a CHECK constraint. For ease of use, you should always choose to define NOT NULL integrity constraints instead of CHECK constraints with the "IS NOT NULL" condition. CHECK integrity constraint. 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))
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);
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);
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.
|
Type of Constraint |
Added to Existing Columns of the Table |
Added with New Columns to the Table |
|
Cannot be defined if any row contains a null value for this column* |
Cannot be defined if the table contains any rows |
|
Cannot be defined if duplicate values exist in the key* |
Always OK |
|
Cannot be defined if duplicate or null values exist in the key* |
Cannot be defined if the table contains any rows |
|
Cannot be defined if the foreign key has values that do not reference a parent key value* |
Always OK |
|
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.
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.
NOT NULL, UNIQUE KEY, PRIMARY KEY, FOREIGN KEY, and CHECK constraints using the CONSTRAINT option of the constraint clause. CREATE TABLE and ALTER TABLE statements for examples of the CONSTRAINT option of the Constraint clause. Note that the name of each constraint is included with other information about the constraint in the data dictionary. |
"Listing Integrity Constraint Definitions" on page 9-27 for examples of data dictionary views. |
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. |
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 . |
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.
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.
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.
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.
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.
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.
Use the ALTER TABLE command to
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.
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. |
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.