Sub topic 13
Inspecting Constraints and Identity Columns in the Data Dictionary
Assume you know basic SELECT statements, relational keys, and the departments and employees tables from earlier chapters. This chapter uses Oracle Database 12c or later and focuses on inspecting definitions rather than creating them.
Oracle stores metadata about database objects in data dictionary views. These views describe tables, columns, constraints, indexes, and identity columns. The USER_ views show objects owned by your current schema. They are usually the best starting point because they do not require special catalog privileges.
Find constraints on a table
USER_CONSTRAINTS contains one row for each constraint in your schema. The CONSTRAINT_TYPE column identifies the kind of constraint:
| Type | Meaning |
|---|---|
| P | Primary key |
| U | Unique constraint |
| R | Referential integrity, or foreign key |
| C | Check constraint, including many `NOT NULL` definitions |
Constraint names and table names are normally stored in uppercase when they were created without quoted identifiers.
SELECT constraint_name,
constraint_type,
status,
validated,
deferrable,
deferred
FROM user_constraints
WHERE table_name = 'EMPLOYEES'
ORDER BY constraint_name;STATUS tells you whether Oracle is enforcing the constraint. VALIDATED tells you whether Oracle has checked existing rows against it. DEFERRABLE indicates whether checking can be postponed until a transaction commits. DEFERRED shows the current timing for a deferrable constraint.
A foreign key appears with type R. To find the table and columns involved, join USER_CONSTRAINTS to itself. The foreign key points to a referenced, or parent, constraint through R_CONSTRAINT_NAME.
SELECT child.constraint_name AS foreign_key_name,
child.table_name AS child_table,
child_cols.column_name AS child_column,
parent.table_name AS parent_table,
parent_cols.column_name AS parent_column
FROM user_constraints child
JOIN user_cons_columns child_cols
ON child_cols.constraint_name = child.constraint_name
AND child_cols.table_name = child.table_name
JOIN user_constraints parent
ON parent.constraint_name = child.r_constraint_name
JOIN user_cons_columns parent_cols
ON parent_cols.constraint_name = parent.constraint_name
AND parent_cols.table_name = parent.table_name
AND parent_cols.position = child_cols.position
WHERE child.table_name = 'EMPLOYEES'
AND child.constraint_type = 'R'
ORDER BY child.constraint_name, child_cols.position;USER_CONS_COLUMNS lists the columns participating in primary, unique, foreign key, and check constraints. The POSITION column matters for composite constraints because column order affects the key.
Inspect identity columns
USER_TAB_IDENTITY_COLS lists identity columns owned by your schema. It shows how the column generates values and the system-managed sequence associated with it.
SELECT table_name,
column_name,
generation_type,
sequence_name,
identity_options
FROM user_tab_identity_cols
ORDER BY table_name, column_name;GENERATION_TYPE is typically ALWAYS, BY DEFAULT, or BY DEFAULT ON NULL. IDENTITY_OPTIONS records settings such as the starting value, increment, and cache. The sequence name is metadata for the generator. Do not alter that sequence directly to change application behavior; change the identity definition through supported table operations.
If you need metadata for objects owned by another schema, use ALL_CONSTRAINTS, ALL_CONS_COLUMNS, and ALL_TAB_IDENTITY_COLS, subject to your privileges. DBA_ versions expose database-wide information and require greater access.
Read metadata as a dependency map
The following relationship helps when tracing a table design:
When diagnosing an insert error, first check the constraint status and type. Then inspect the participating columns. For generated-key issues, check the identity generation mode and options before changing application SQL.
Exercise
Which data dictionary view shows whether an identity column uses ALWAYS or BY DEFAULT generation, and which view shows the columns in a composite primary key?
Show answer
Use USERTABIDENTITYCOLS for the identity generation mode. Use USERCONS_COLUMNS for the columns and their positions in the composite primary key.