2. Objectives
After completing this lesson, you should be able to
do the following:
• Implement data integrity constraints
• Maintain integrity constraints
• Obtain constraint information from the data
dictionary
4. Types of Constraints
Constraint
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Description
Specifies that a column cannot contain null
values
Designates a column or combination of
columns as unique
Designates a column or combination of
columns as the table’s primary key
Designates a column or combination of
columns as the foreign key in a referential
integrity constraint
Specifies a condition that each row of the
table must satisfy
7. Defining Constraints as
Immediate or Deferred
Use the command ALTER SESSION or SET
CONSTRAINTS to set the mode for your constraint.
ALTER SESSION
SET CONSTRAINT[S] =
{IMMEDIATE|DEFERRED|DEFAULT};
8. Primary and Unique Key Enforcement
Is an index
available
for use?
Yes
No
No
Yes
Yes
No
Create nonunique
index
Create unique
index
Do not use
index
Use existing
index
Key enabled?
Constraint
deferrable?
9. Foreign Key Considerations
Desired Action
Drop parent table
Truncate parent table
Drop tablespace
containing parent table
Avoid locks on child
table while performing
DML on parent table
Perform DML on child
table
Appropiate Solution
Cascade constraints
Disable or drop foreign key
Use CASCADE CONSTRAINTS
clause
Create index on foreign key
Ensure tablespace containing
parent key index online
10. Defining Constraints While
Creating a Table
CREATE TABLE summit.employee(
id NUMBER(7)
CONSTRAINT employee_id_pk PRIMARY KEY
DEFERRABLE
USING INDEX
STORAGE(INITIAL 100K NEXT 100K)
TABLESPACE indx,
last_name VARCHAR2(25)
CONSTRAINT employee_last_name_nn NOT NULL,
dept_id NUMBER(7))
TABLESPACE data;
11. Guidelines for
Defining Constraints
• Primary and unique constraints:
– Place indexes in a separate tablespace
– Use nonunique indexes if bulk loads are
frequent
• Self-referencing foreign keys:
– Define or enable foreign keys after initial load
– Defer constraint checking
12. Enabling Constraints
• No locks on table
• Primary and unique keys
must use nonunique indexes
Enable
NOVALIDATE
ALTER TABLE summit.department
ENABLE NOVALIDATE CONSTRAINT dept_pk;
13. Enabling Constraints
• Locks table
• Can use unique or nonunique
indexes
• Needs valid table data
Enable
VALIDATE
ALTER TABLE summit.employee
ENABLE VALIDATE CONSTRAINT emp_dept_fk;
14. Using the EXCEPTIONS Table
1. Create EXCEPTIONS table (utlexcpt.sql).
2. Execute ALTER TABLE with EXCEPTIONS clause.
3. Use subquery on EXCEPTIONS to locate rows
with invalid data.
4. Rectify the errors.
5. Reexecute ALTER TABLE to enable the
constraint.
15. Dropping Constraints
• Drop constraints using this command:
• Drop a table and any referencing foreign key using
this command:
ALTER TABLE summit.employee
DROP CONSTRAINT emp_ln_uk;
DROP TABLE department
CASCADE CONSTRAINTS;
17. Summary
In this lesson, you should have learned how to:
• Implement data integrity
• Use an appropriate strategy for creating and
maintaining constraints
• Obtain information from the data dictionary
Editor's Notes
Oracle Programmer™ is a comprehensive suite of products which facilitates enterprise application development with the Oracle8™ database.
Oracle Programmer with Oracle8 consists of:
Three call level interfaces:
1) The Oracle8 Oracle Call Interface (OCI).
2) The Oracle Open Database Connectivity (ODBC) driver.
3) Two Java Database Connectivity (JDBC) drivers.
Three embedded, SQL-style interfaces:
1) The Oracle Precompilers (Pro*C™, Pro*COBOL®, and Pro*FORTRAN®).
2) SQL*Module® (for ADA).
3) JSQL™ (embedded SQL in Java).
Oracle Objects® for OLE are an easy-to-use OLE/COM interface to access Oracle from Visual Basic and Microsoft Windows applications.
The Object Type Translator is a utility that maps Oracle8 object schemas to client-side language variables, such as C header files.
The availability of each of these products and the Oracle Programmer bundle itself on various platforms needs to be determined from Oracle.