Table Constraints

Information about constraints can be obtained from two data dictionary views: ALL_CONSTRAINTS and ALL_CONS_COLUMNS. ALL_CONSTRAINTS returns one row for each constraint, and is the only view you need to look at for the definition of a check constraint. Foreign key, primary key, and unique constraints are defined on one or more columns, so for these there is a one-to-many relationship between ALL_CONSTRAINTS and ALL_CONS_COLUMNS. Foreign keys are the most complicated, because to get a complete picture of a foreign key constraint, you need to join the ALL_CONS_COLUMNS table to itself, then refer back to the ALL_CONSTRAINTS view again. The reason for this is that a foreign key may be attached to either a primary key constraint or a unique constraint. It’s important to know which of the columns in the parent table matter.

Different Constraint Types Need Different Queries

There are four different types of constraints that can be created on a table. The four types are:

CHECK
PRIMARY KEY
UNIQUE
FOREIGN KEY

These types are different enough that, with the exception of the PRIMARY KEY and UNIQUE constraints, you need a slightly different query for each in order to see the definition.

CHECK constraints

A check constraint is simply an expression that must be true for each row in a table. This is the simplest of the constraint types when it comes to querying the data dictionary tables. The check expression is stored in the SEARCH_CONDITION column of the ALL_CONSTRAINTS table. ...

Get Oracle SQL*Plus: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.