|
||
|
Enabling and disabling Oracle ConstraintsEnabling and disabling Oracle constraints can be accomplished by using
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] '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 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] Options: USING INDEX storage_options storage_options: PCTFREE int 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 ( Table created. SQL> ALTER TABLE test1 ADD ( Table altered. SQL> SELECT constraint_name SQL> ALTER TABLE test RENAME CONSTRAINT test1_pk TO test_pk; Table altered. SQL> ALTER INDEX test1_pk RENAME TO test_pk; Index altered.
SQL> SELECT constraint_name
SQL> SELECT index_name, column_name
You can modify a constraint as follows :-
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 For example: ALTER TABLE supplier 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:
Free eBookSubscribe 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.
Read more
Testimonials
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 |
Theory & Practice DB Normalization Analysis Phase Database Keys DB Glossary Appl.Architecture Oracle DBA MySQL DBA SQL Server DBA Install Oracle Install SQL Server Proj.Management Oracle Constraint Programming Tips Database Normalization eBook: |
||
Copyright © www.databasedesign-resource.com /
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
The name Oracle is a trademark of Oracle Corporation. |