Home > Oracle Database / Applications Tips > Oracle database administrator > How to work with deferred constraints
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE ADMINISTRATOR

How to work with deferred constraints


Daniel Clamage
01.28.2004
Rating: -4.45- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


A colleague of mine recently asked how he could defer a unique key constraint until the end of his transaction, so that he could temporarily have two key values the same, which he would then correct before concluding the transaction. I suggested making the key constraint deferrable. He then asked how to redefine the constraint as deferred in an UPDATE statement? The correct method is a bit more involved, so I responded with this example:

First you'll have to drop the constraint and recreate it with the DEFERRABLE clause. You can also specify whether this deferrable state is on or off (INITIALLY IMMEDIATE/DEFERRED). Then you can use the SET CONSTRAINT command to control this feature. For example, you may want only one particular application module to have the ability to perform a deferred transaction, and leave the constraint active otherwise, e.g.:

CREATE TABLE blick (num NUMBER, str VARCHAR2(1));
Table created.

ALTER TABLE blick
ADD CONSTRAINT pk_blick PRIMARY KEY (num)
DEFERRABLE INITIALLY IMMEDIATE;
Table altered.

INSERT INTO blick (num, str)
VALUES (1, 'A');
1 row created.

INSERT INTO blick (num, str)
VALUES (1, 'B');
ORA-00001: unique constraint (PPDEV.PK_BLICK) violated

SET CONSTRAINT pk_blick DEFERRED;
Constraint set.

INSERT INTO blick (num, str)
VALUES (1, 'B');
1 row created.

UPDATE blick
SET num=2
WHERE str='B';
1 row updated.

-- validate the new rows pass the constraint
SET CONSTRAINT pk_blick IMMEDIATE;
Constraint set.
COMMIT;
Commit complete.
The DDL to change between deferred and immediate will have to be done using Execute Immediate:
BEGIN
 EXECUTE IMMEDIATE 'SET CONSTRAINT pk_blick DEFERRED';

 INSERT INTO blick (num, str)
 VALUES (3, 'C');

 INSERT INTO blick (num, str)
 VALUES (3, 'D');

 UPDATE blick
 SET num=4
 WHERE str='D';

 EXECUTE IMMEDIATE 'SET CONSTRAINT pk_blick IMMEDIATE';
 COMMIT;
END;
/
Notice I resume constraint checking BEFORE issuing the COMMIT. If the constraint will be violated by my transaction, I want to know it before committing the changes.

For More Information

  • For more info: More info about deferred contstraints from Oracle
  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our applications, SQL, database administration, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Oracle database administrator
Understanding SQL string functions
What is the difference between a database engineer, architect and administrator?
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
How can I find statistics on total memory usage and database connections?
Installing multiple Oracle homes
Modifying SYS password in a RAC environment

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Oracle Development Solutions - SQL, J2EE, XML, SOA
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts