The Database Design Resource Center



Enabling and disabling Oracle Constraints

Enabling and disabling Oracle constraints can be accomplished by using
  1. Drop constraint command
  2. Disable constraint command
Constraints specified in the enable and disable clauses of a CREATE TABLE statement must be defined in the statement.

Enabling and disabling Oracle constraints can also be done with the ENABLE and DISABLE keywords of the CONSTRAINT clause.

If you define a constraint but do not explicitly enable or disable it, ORACLE enables it by default.

Any SQL INSERT, UPDATE or DELETE command applied to a table with constraints enabled has the possibility of failing.

For example, updates applied to a Parent Table may fail if the statement leaves orphaned rows in a child table, INSERT command against a Child Table may fail if a matching foreign key value does not exist in the parent table.

Constraint failures will result in the statement being rolled back - coding an application front end to deal with such errors is generally easier than handling all the business rules in code.

You can design applications to use constraint data dictionary information to provide user feedback about integrity constraint violations.

DROP Constraint command Drop an integrity constraint.

Syntax:

DROP PRIMARY KEY [CASCADE]
DROP UNIQUE column [CASCADE]
DROP CONSTRAINT constraint_name

'Column' can be either a single column name or several columns separated with commas,

- DISABLE allow incoming data, regardless of whether it conforms to the constraint

- VALIDATE ensure that existing data conforms to the constraint

- NOVALIDATE existing data does not have to conform to the constraint

These can be used in combination

DISABLE { VALIDATE | NOVALIDATE }
- DISABLE NOVALIDATE is the same as DISABLE.

- DISABLE VALIDATE disables the constraint, drops the index on the constraint, and disallows any modification of the constrained columns.

For a UNIQUE constraint, this enables you to load data from a nonpartitioned table into a partitioned table using the

ALTER TABLE.. EXCHANGE PARTITION clause.

When a unique or primary key is Enabled, if there is no existing index, a unique index is automatically created. When a unique or primary key is Disabled, the unique index is dropped.

When a constraint is Validated, all data must be checked. (this can be very slow.)

The basic syntax for dropping a constraint is:

alter table tablename drop constraint someconstraint;

where tablename is the table name and someconstraint is the constraint name:

For example:

ALTER TABLE students DROP CONSTRAINT SYS_C001400;

Drop constraind command implemented by using unix script

If data migrations occur between databases, with the CONSTRAINTS=N clause missing, you end up with lots of duplicate check constraints.

if [ ${column_name_next} = ${column_name} ]

then

echo "ALTER TABLE SYSADM.${table_name} DROP CONSTRAINT

${constraint_name_n ext};" >> ${TAB_NAME}_${ORACLE_SID}.sql

Disable a constraint associated with a table: Syntax:

DISABLE [[NO]VALIDATE] [UNIQUE] (column [,...] ) options [CASCADE] [{DROP|KEEP} INDEX]

DISABLE [[NO]VALIDATE] PRIMARY KEY options [CASCADE] [{DROP|KEEP} INDEX]

DISABLE [[NO]VALIDATE] [UNIQUE] CONSTRAINT constraint_name options [CASCADE] [{DROP|KEEP} INDEX]
DISABLE ALL TRIGGERS

Options: USING INDEX storage_options
USING INDEX (create_index_statement)
SORT | NOSORT
LOCAL | GLOBAL PARTITION BY RANGE (column_list)
(PARTITION partition
VALUES LESS THAN (value [,value...])
EXCEPTIONS INTO [schema.]table

storage_options:

PCTFREE int
PCTUSED int
INITTRANS int
MAXTRANS int
STORAGE storage_clause
TABLESPACE tablespace
LOGGING|NOLOGGING

Disabling 'anonymous' constraint

CREATE TABLE foo (bar NUMBER, baz NUMBER, UNIQUE (bar, baz));

ALTER TABLE foo DISABLE UNIQUE (bar, baz);

Disabling named constraint

CREATE TABLE foo (bar NUMBER, baz NUMBER, CONSTRAINT uq_foo UNIQUE (bar, baz));

ALTER TABLE foo DISABLE CONSTRAINT uq_foo;

In addition to renaming tables and indexes Oracle9i Release 2 allows the renaming of columns and constraints on tables.

In this example once the the TEST1 table is created it is renamed along with it's columns, primary key constraint and the index that supports the primary key:

SQL> CREATE TABLE test1 (
2 col1 NUMBER(10) NOT NULL,
3 col2 VARCHAR2(50) NOT NULL);

Table created.

SQL> ALTER TABLE test1 ADD (
2 CONSTRAINT test1_pk PRIMARY KEY (col1));

Table altered.

SQL> SELECT constraint_name
2 FROM user_constraints
3 WHERE table_name = 'TEST1'
4 AND constraint_type = 'P';

SQL> ALTER TABLE test RENAME CONSTRAINT test1_pk TO test_pk;

Table altered.

SQL> ALTER INDEX test1_pk RENAME TO test_pk;

Index altered.

Enabling and disabling Oracle constraints

SQL> SELECT constraint_name
2 FROM user_constraints
3 WHERE table_name = 'TEST'
4 AND constraint_type = 'P';

Enabling and disabling Oracle constraints

SQL> SELECT index_name, column_name
2 FROM user_ind_columns
3 WHERE table_name = 'TEST';

Enabling and disabling Oracle constraints

You can modify a constraint as follows :-

  • Add new constraint to column or table.
  • Remove constraint.
  • Enable / disable constraint.
  • You cannot change a constraint definition.

If we want to add a constraint to our new column we can use the following ALTER statement :

ALTER TABLE book MODIFY(review NOT NULL);

Note that we can't specify a constraint name with the above statement. If we wanted to further modify a constraint (other than enable / disable) we would have to drop the constraint and then re apply it specifying any changes.

Assuming that we decide that 200 bytes is insufficient for our review field we might then want to increase its size.

The statement below demonstrates this :

ALTER TABLE book MODIFY (review VARCHAR2(400));

We could not decrease the size of the column if the REVIEW column contained any data.

ALTER TABLE book DISABLE CONSTRAINT b_auth;

ALTER TABLE book ENABLE CONSTRAINT b_auth;

The above statements demonstrate enabling and disabling Oracle constraints: Note that if, between disabling a constraint and re enabling it, data was entered to the table that included NULL values in the AUTHOR column, then you wouldn't be able to re enable the constraint.

This is because the existing data would break the constraint integrity. You could update the column to replace NULL values with some default and then re enable the constraint.

The syntax for enabling a unique constraint is:

ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

For example:

ALTER TABLE supplier
ENABLE CONSTRAINT supplier_unique;

In this example, we're enabling a unique constraint on the supplier table called supplier_ unique.

All tables will have full RI (Referential Integrity), including PK constraints, FK constraints and check constraints.

The default for foreign key constraints will be "On Delete Restrict", unless otherwise specified.

This means that no parent record can be deleted if there are corresponding child records.

A referential column constraint with ON DELETE CASCADE will cascade deletes. Deleting a primary key row will delete all related foreign keys e.g. delete a customer and all that customer's orders will disappear.

Careful with that one, please!

Return to Oracle Database constraints


Exclusive interviews with:
Steven Feuerstein, PLSQL expert
Donald Burleson, Top IT consultant


Free eBook

Subscribe to my newsletter and get my ebook on Entity Relationship Modeling Principles as a free gift:


What visitors say...

"I just stumbled accross your site looking for some normalization theory and I have to say it is fantastic.

I have been in the database field for 10+ years and I have never before come across such a useful site. Thank you for taking the time to put this site together."

Mike, USA

Read more Testimonials



Database Normalization eBook:


Database Normalization eBook




Copyright © www.databasedesign-resource.com / Alf A. Pedersen
All rights reserved.
All information contained on this website is for informational purposes only.
Disclaimer: www.databasedesign-resource.com does not warrant any company, product, service or any content contained herein.

Return to top

Copyright acknowledgement note:

The name Oracle is a trademark of Oracle Corporation.
The names MS Access/MS SQL Server are trademarks of Microsoft Corporation.
Any other names used on this website may be trademarks of their respective owners, which I fully respect.