Generate Enable Constraint Statements (enable_constraint.gen)
What This Script Does
Section titled “What This Script Does”This generator script creates ALTER TABLE statements to enable disabled constraints. It orders primary/unique/check constraints first, then foreign keys to handle dependencies properly.
The Script
Section titled “The Script”rem enable_constraint.genremselect 'alter table ' || owner || '.' || table_name || ' enable constraint ' || constraint_name || ';' from sys.dba_constraints where owner like upper('&owner') and table_name like upper('&table_name') and constraint_type in ('P','U','R','C') and status = 'DISABLED' order by decode(constraint_type,'R',2,1), owner, table_name, constraint_name/
-- Basic usage@enable_constraint.gen
-- When prompted, enter:-- owner: Schema owner pattern (use % for wildcard)-- table_name: Table name pattern (use % for wildcard)
Parameters
Section titled “Parameters”The script prompts for:
- &owner - Schema owner pattern (use % for wildcard)
- &table_name - Table name pattern (use % for wildcard)
Required Privileges
Section titled “Required Privileges”SELECT ANY DICTIONARY-- OR --SELECT ON DBA_CONSTRAINTS
Sample Output
Section titled “Sample Output”alter table HR.EMPLOYEES enable constraint EMP_EMP_ID_PK;alter table HR.EMPLOYEES enable constraint EMP_EMAIL_UK;alter table HR.EMPLOYEES enable constraint EMP_SALARY_MIN;alter table HR.EMPLOYEES enable constraint EMP_DEPT_FK;
Key Features
Section titled “Key Features”- Smart Ordering: Enables primary/unique constraints before foreign keys
- Constraint Types: Handles Primary (P), Unique (U), Referential (R), and Check (C) constraints
- Status Aware: Only generates statements for currently disabled constraints
- Dependency Management: Proper ordering minimizes enable failures
Common Use Cases
Section titled “Common Use Cases”Enable All Constraints After Data Load
@enable_constraint.gen-- Enter: HR for owner, EMPLOYEES for table_name
Enable All Disabled Constraints in Schema
@enable_constraint.gen-- Enter: SCOTT for owner, % for table_name
Related Scripts
Section titled “Related Scripts”- Disable Constraint Generator - Disable constraints
- Compile Invalid Generator - Compile invalid objects after enabling constraints