Getting Information on Constraints:user_constraints and all_constraints

user_constraints has information on your constraints. Some of the columns in user_constraints:

ColumnTypeDescription
ownerVARCHAR2(30)Owner of the constraint.
constraint_nameVARCHAR2(30)Name of the constraint.
constraint_typeVARCHAR2(1)Constraint type (P, R, C, U, V, or O).
table_nameVARCHAR2(30)Name of the table on which the constraint is defined.
statusVARCHAR2(8)Constraint status (ENABLED or DISABLED).
deferrableVARCHAR2(14)Whether the constraint is deferrable (DEFERRABLE or NOT DEFERRABLE).
deferredVARCHAR2(9)Whether the constraint is enforced immediately or deferred (IMMEDIATEor DEFERRED).

SELECT constraint_name, constraint_type, status, deferrable, deferred
FROM user_constraints
WHERE table_name = 'EMP';

all_constraints view has information on all the constraints you have access to. user_cons_columns view has information on the constraints for a column.

Some Columns in the user_cons_columns View:

ColumnTypeDescription
ownerVARCHAR2(30)Owner of the constraint
constraint_nameVARCHAR2(30)Name of the constraint
table_nameVARCHAR2(30)Name of the table on which the constraint is defined
column_nameVARCHAR2(4000)Name of the column on which the constraint is defined

SQL> ALTER TABLE emp
  2  ADD CONSTRAINT my_uq UNIQUE (ename)
  3  DEFERRABLE INITIALLY DEFERRED;

Table altered.

SQL> SELECT constraint_name, column_name
  2  FROM user_cons_columns
  3  WHERE table_name = 'EMP'
  4  ORDER BY constraint_name;

CONSTRAINT_NAME
------------------------------
COLUMN_NAME
-------------------------------------------------
MY_UQ
ENAME

SYS_C004212
EMPNO


SQL>

all_cons_columns view has information on all the column constraints you have access to. The next query joins user_constraints and user_cons_columns to get the column_name, constraint_name, constraint_type, and status:


SQL> SELECT ucc.column_name,
  2         ucc.constraint_name,
  3         uc.constraint_type,
  4         uc.status
  5  FROM user_constraints uc,
  6       user_cons_columns ucc
  7  WHERE uc.table_name = ucc.table_name
  8    AND uc.constraint_name = ucc.constraint_name
  9    AND ucc.table_name = 'EMP'
 10  ORDER BY ucc.constraint_name;

COLUMN_NAME       CONSTRAINT_NAME                C STATUS
-----------------------------------------------------------
ENAME             MY_UQ                          U ENABLED
EMPNO             SYS_C004212                    C ENABLED


SQL>
Home »
Oracle »
Table » 

Constraints:
  1. Adding a Constraint with CHECK
  2. Adding a NOT NULL Constraint
  3. Adding a FOREIGN KEY Constraint
  4. ON DELETE CASCADE
  5. ON DELETE SET NULL
  6. Adding a UNIQUE Constraint
  7. CHECK constraint
  8. Multiple Constraints
  9. Dropping a Constraint
  10. Disabling a Constraint
  11. Enabling a Constraint
  12. Deferred Constraints
  13. Getting Information on Constraints:user_constraints and all_constraints
Related: