Disabling constraints on a schema

Guys and Gurus
In Oracle 10g, is it possible to temporarily disable all constraints on a particular schema with the view of reenabling them at a later date?
This would save time in a particularly massive schema.
Many Thanks
Message was edited by:
The Flaz

I use this script to disable constraints in a schema, might be an easier way not sure tbh.
PROMPT Please wait while constraints are disabled...
DECLARE
l_SQLstatement VARCHAR2(32000);
CURSOR c_DisableConstraints IS
SELECT 'alter table '||table_name||' disable constraint '||constraint_name||' cascade' sqlstatement
FROM USER_CONSTRAINTS;
BEGIN
FOR r_DisableConstraints IN c_DisableConstraints LOOP
l_SQLstatement := r_DisableConstraints.sqlstatement;
EXECUTE IMMEDIATE (l_SQLstatement);
END LOOP;
END;
/

Similar Messages

  • Disable Constraint in LBACSYS schema

    How wise would it be to disable a constraint in the LBACSYS schema, on a table that has the long name, short name of a rowlabel on it? (We need to create various rowlabels with the same long name, and it violates a constraint. )

    my guess is that the "xsd:key" constraint needs to be in the "transactions" element, not the "transaction" element.

  • How to defer constraint of one schema from another schema

    Hi All,
    I am having a requirement of migrating data from one schema(SCHEMA_A) to another schema(SCHEMA_B). So I tried to implement the same using PL/SQL.
    Because of foreign key contraint while migrating child table, Oracle throws an error message like parent key not available.
    So I tried to set the all contraints to deferred and after completion of migration i planned to set all the contraints to immediate.
    But Here I am executing the procedure from schema(SCHEMA_C) which has rights to access SCHEMA_A and SCHEMA_B.
    How could I able to defer all the constraint in SCHEMA_B from SCHEMA_C?
    Thanks in Advance,
    Antany.

    Yes,
    You could run something like this, for foreign keys:
    BEGIN
      FOR cur_rec IN (SELECT table_name, constraint_name FROM DBA_CONSTRAINTS WHERE OWNER = <owner> AND CONSTRAINT_TYPE = 'R' )
        LOOP
          EXECUTE IMMEDIATE 'ALTER TABLE <owner>.' || cur_rec.table_name || ' DISABLE CONSTRAINT ' || cur_rec.constraint_name;
        END LOOP;
    END;
    /

  • Disabling constraints

    HI,
    Is there a way in oracle 10g to disable all constraints using a single query?If yes,what is it?
    Thanks in advance

    Here is a generic script that will turn off all FKs for MY_SCHEMA. It is easy enough to adapt to more than one schema, more than one constraint type, and etc. Just be careful not to mess with SYS, SYSTEM, and other Oracle supplied schemas.
    set serveroutput on
    BEGIN
      dbms_output.enable(1000000);
        FOR x IN ( SELECT owner,
                          table_name,
                          constraint_name
                     FROM dba_constraints
                    WHERE owner IN ( 'MY_SCHEMA' )
                      AND constraint_type = 'R'
                      AND status          = 'ENABLED')
        LOOP
          BEGIN
            EXECUTE IMMEDIATE 'alter table '||x.owner||'.'||x.table_name||' disable constraint ' ||
                              x.constraint_name;
    --      EXCEPTION
    --        WHEN others THEN
    --              NULL;
    --          dbms_output.put_line('Bad owner = '||x.owner||';  Bad table_name='||x.table_name||
    --                               ';  Bad constraint_name='||x.constraint_name);
          END;
        END LOOP;
    END;
    /

  • ALTER TABLE privilege and CREATE/DROP/ENABLE/DISABLE constraint privilege

    Hi,
    I am looking for some detailed info regarding the below previleges
    ALTER TABLE, CREATE CONSTRAINT, DROP CONSTRAINT, ENABLE CONSTRAINT AND DISABLE CONSTRAINT PRiVILEGES.
    I have two schemas 'A' and 'B', I want to provide user 'A' with Alter table, create or drop constraint,Enable or Disable constraint on schema B.
    Please let me know how to make this work.
    Thank you

    I got the answer for my second question, I have an option to grant 'Alter ANY table' privilege to the user.Yes, but you should not do that.
    Regarding question one, Suppose I have two schemas A and B and I want Schema A to have alter table privilege on all tables of Schema B.
    Can I do this in one command No
    or I need to grant alter on each table saperately?Yes
    If I am chosing the second option for each table saperately then whenever a table is added in schema B we need to grant privilege on that table as well.Yes. But nothing strange there. Designing and creating objects includes the privileges on them.
    If user A is granted with alter table privilege on a table which user B owns then can user A drop/create/enable/disable constraints for that table?Yes, isn't that what all this about?
    Again, letting one user alter the objects of another user is generally not such a good idea. Hope you see this from our discussion.
    Alter table privilege includes adding and dropping columns. This is why I suggested writing a procedure that does exactly what you need. And then grant execute on that to A.
    The best thing of course would be NOT TO disable the constraints, they are probably there for a reason.
    I am currently handling an issue where one session doing this, deadlocks with another session doing only selects - From other tables, that is!
    Regards
    Peter

  • Cannot disable constraint error

    Hi - this might be a bit of a noob question but here goes. I have a table with a constraint (called FK_Category_Product) that I want to disable. When I run this statement the constraint is returned in the results:
    SELECT owner,constraint_name, constraint_type, table_name, status
    FROM user_constraints
    WHERE constraint_type = 'R'However when I run this statement:
    alter table T_PRODUCT disable constraint FK_Category_ProductI get this error:
    SQL Error: ORA-02431: cannot disable constraint (FK_CATEGORY_PRODUCT) - no such constraintAnyone got any ideas?
    Edited by: user9507427 on Mar 17, 2013 5:08 PM

    Ok thanks - I'm guessing it's something to do with the schema / owner. Here's my version ( running on Windows Server 2008 R2):
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit ProductionWhen I run this query:
    SELECT owner,constraint_name, constraint_type, table_name, status
    FROM user_constraints
    WHERE table_name='T_PRODUCT'I get these results:
    OWNER                          CONSTRAINT_NAME                CONSTRAINT_TYPE TABLE_NAME                     STATUS
    PRODOWNER                       FK_Product_SourceSpecies       R               T_PRODUCT                      ENABLED 
    PRODOWNER                       FK_Product_CreatePerson        R               T_PRODUCT                      ENABLED 
    PRODOWNER                       FK_Product_ModifyPerson        R               T_PRODUCT                      ENABLED 
    PRODOWNER                       FK_Product_ExpressionSystem    R               T_PRODUCT                      ENABLED 
    PRODOWNER                       FK_Product_Localisation        R               T_PRODUCT                      ENABLED 
    PRODOWNER                       FK_Batch_Products              R               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908459                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908460                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908461                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908462                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908463                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908464                  C               T_PRODUCT                      ENABLED 
    PRODOWNER                       SYS_C00908465                  P               T_PRODUCT                      ENABLED  Then when I run this statement (as PRODOWNER):
    alter table T_PRODUCT disable constraint FK_Product_SourceSpeciesI get this result:
    Error starting at line 3 in command:
    alter table T_PRODUCT disable constraint FK_Product_SourceSpecies
    Error report:
    SQL Error: ORA-02431: cannot disable constraint (FK_PRODUCT_SOURCESPECIES) - no such constraint
    02431. 00000 -  "cannot disable constraint (%s) - no such constraint"
    *Cause:    the named constraint does not exist for this table.
    *Action:   ObviousI tried the above with and without single quotes around the constraint name, same result but slightly different error message with single quotes

  • Deadlocks with ALTER TABLE DISABLE CONSTRAINT

    Hello,
    We're deleting millions of redundant rows from a particular table in our live 10g database. This is being done online because the downtime would be unacceptable. The table in question has 30 child tables, so for speed I am disabling the foreign keys using ALTER TABLE DISABLE CONSTRAINT before the deletion (we haven't had any constraint violations for ages). Without this, deletion takes about 1 second per row i.e. a very long time.
    However, we're finding that ALTER TABLE DISABLE CONSTRAINT often reports ORA-00060: deadlock detected. This is causing problems with the live system. Can anyone think of the reason why a deadlock might occur in this situation and what we could do to prevent it happening? Note that any solution has to be doable without downtime unless it takes less than 30 minutes.
    Thanks a lot
    Ed
    Edited by: edwiles on Feb 4, 2009 6:02 AM

    look suggestions in the similar thread:
    Re: Deadlock when deleting a not linked data record in a parent table

  • Disabling Constraint !

    Hi,
    when I tried to execute the following code it gave me an error :
    insert into cheap_products_view (
    product_id,product_type_id,name,price
    ) values (
    13,1,'Western front',13.50
    ERROR at line 1:
    ORA-00001: unique constraint (STORE.PRODUCTS_PK) violated
    and when I tried to drop the Constraint and using with or with out cascade after the drop constraint it gave me an error also :
    SQL> alter table products
    2 disable constraint products_pk ;
    alter table products
    ERROR at line 1:
    ORA-02297: cannot disable constraint (STORE.PRODUCTS_PK) - dependencies exist
    so please can any one help me with that !

    insert into cheap_products_view (
    product_id,product_type_id,name,price
    ) values (
    13,1,'Western front',13.50
    );I say, it is a bad approach to disable PK and FK and do some insertion like this. Mainly if your application is OLTP type and there is some purpose to data modeling and create relationship between entities. So not a good idea to violate business rules.
    I can share idea, if you like, Update table cheap_products_view with those new values you want to insert for prodcut_id (13). As Product id (PK) = 13 , record is already there.
    Like : (Not tested)
      UPDATE cheap_products_view SET product_type_id = 1 , name = 'Western front' , price = 13.50
      WHERE product_id = 13
    /Thanks!

  • How to extract all keys (PK, FK) and constraints from the schema?

    hi,
    How to extract all keys (PK, FK) and constraints from the schema?Thanks!

    I have no idea about any tool which only extract the DDL of constraints.
    In oracle 9i or above you can use DBMS_METADATA to extract DDL of a table which also shows contraints defination.
    You can take the tables export without data and use databee tool to extract the DDL.
    www.databee.com

  • Exception Typo: ORA-02297: cannot disable constraint (string.string)

    ORA-02297: cannot disable constraint (string.string) - dependencies exist
    Cause: an alter table disable constraint failed becuase the table has foriegn keys
    that are dpendent on this constraint.
    Action: Either disable the foreign key constraints or use disable cascade
    Note the typo of the word BECAUSE from Version 11R2 back to at least 10R2.
    Thanks.

    Actually, please also correct the words "foriegn" and "dpendent" too!

  • Disable constraints

    Hello ,
    I wrote a query to disable all the constraints of particular table.
    Alter Table Test disable all constraints;
    but it is giving me the error Invalid alter Table option.
    Can anybody give me the solution for how to disable all the constraints of a table using single sql query.
    Thanks

    set serveroutput on
    declare
        sql_stm    varchar2(2000);
    begin
        dbms_output.enable(1000000);
        for x in (select constraint_name
                   from user_constraints
                   where table_name =table_name ) loop
            sql_stm := 'alter table table_name disable constraint '||
                x.constraint_name;
            dbms_output.put_line(sql_stm);
            execute immediate sql_stm;
        end loop;
    end;replace table_name with the required table name
    Regards,
    Mohd. Mehraj Hussain
    http://mehrajdba.wordpress.com

  • From SQLDev2.1 and on... Disable Constraint Bug

    Hi everyone,
    Since SQLDeveloper 2.0 there's a bug in the query generated by the software when you right click a table > constraint > Disable all related constraints
    It generates the following:
    begin
                                  for cur in (select fk.owner, fk.constraint_name , fk.table_name
                                       from all_constraints fk, all_constraints pk
                                       where fk.CONSTRAINT_TYPE = 'R' and
                                       pk.owner = 'OWNER' and
                                       fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME and
                                       pk.TABLE_NAME = 'TABLE_NAME') loop
                                  execute immediate 'ALTER TABLE '||cur.owner||'.'||cur.table_name||' MODIFY CONSTRAINT '||'''cur.constraint_name'''||' DISABLE';
                             end loop;
                             end;
    when it should be:
    begin
                                  for cur in (select fk.owner, fk.constraint_name , fk.table_name
                                       from all_constraints fk, all_constraints pk
                                       where fk.CONSTRAINT_TYPE = 'R' and
                                       pk.owner = 'OWNER' and
                                       fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME and
                                       pk.TABLE_NAME = 'TABLE_NAME') loop
                                  execute immediate 'ALTER TABLE '||cur.owner||'.'||cur.table_name||' MODIFY CONSTRAINT '||cur.constraint_name||' DISABLE';
                             end loop;
                             end;
    [The problem is on cur.constraint_name]
    On the new SQLDeveloper 3.0 EA4 this bug its still valid...
    To enable the constraint is ok.
    Has anyone else seen this?

    Hey,
    I've tested out the disable constraints script generation on 3.1 EA1 [3.1.05.97] and now it creates the script just fine. Also with the 'fk.r_owner = pk.owner' part that was missing.
    The generated script:
    begin
                                  for cur in (select fk.owner, fk.constraint_name , fk.table_name
                                       from all_constraints fk, all_constraints pk
                                       where fk.CONSTRAINT_TYPE = 'R' and
                                       pk.owner = 'TEST_OWNER' and
                  fk.r_owner = pk.owner AND
                                       fk.R_CONSTRAINT_NAME = pk.CONSTRAINT_NAME and
                                       pk.TABLE_NAME = 'TEST_TABLE') loop
                                    execute immediate 'ALTER TABLE "'||cur.owner||'"."'||cur.table_name||'" MODIFY CONSTRAINT "'||cur.constraint_name||'" DISABLE';
                                end loop;
                             end;My thanks to everybody of the SQL Developer Team.

  • Deleting Datas from a table without disabling constraints.

    Hi,
    I am working in Oracle9i and solaris 5.8. In that i want to delete half of the datas in a table . the table contains one lakh rows but i need to delete only 50 thousand rows. But the table is constraints enabled.Please send me some queries how to delete the datas withput disabling the constraints.

    What type of constraint do you have ?
    In case of not null, unique, primary key constraint you can delete the rows without disabling the constraints.
    In case of referential integrity constraint you can also delete the rows without disable the constraints but you have to specify on delete cascade constraints clause. By doing so, Oracle will delete the rows in the child table as well.
    http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php

  • Using ID constraints in XML Schema

    I've been testing out a schema containing a number of key/keyref constraints for validation with the DOM parser and I've been having problems.
    Specifically, when I've used predicates as in:
    blocklist/block[1],
    blocklist/block[position()=1] or
    blocklist/block[position()=last()]
    the parser hasn't found any nodes.
    Does anybody know anything about this - what type of XPath expressions are allowed in selector and field elements and exactly what predicate syntax is recognised (if any!)?

    My mistake - it was a namespace problem!!
    I'd remembered to use namespace prefixes for the xpath names in the keyrefs but not in the keys!
    Because I was getting different messages from the parser when I was defining the keyrefs with and without the predicates, I got the impression that it was working ok without predicates and that it was a predicate problem.
    Actually, I don't think that the error messages returned from the parser for broken key constraints are very infomative.
    I have three keys and keyrefs defined. If there is a constraint error, the parser doesn't tell you which keyref the error is with, only that there is an error with one of them. Additionally, because the the key and keyref tables are calculated after the rest of the document has been schema verified, the line given as the error line is the current line - ie the last line of document!

  • Identity Constraint in XML Schema

    Hi,
    I am trying to validate an xml so that the value of one tag<ns0:transid> should be unique. The XML is given below:
    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:transactions  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
       xmlns:ns0='http://xml.netbeans.org/schema/testSchema'
       xsi:schemaLocation='http://xml.netbeans.org/schema/testSchema src\testSchema.xsd'>
           <ns0:transaction>
               <ns0:transid>1</ns0:transid>
               <ns0:name>abc</ns0:name>
           </ns0:transaction>
           <ns0:transaction>
               <ns0:transid>1</ns0:transid>
               <ns0:name>abc</ns0:name>
           </ns0:transaction>
           <ns0:transaction>
               <ns0:transid>3</ns0:transid>
               <ns0:name>abc</ns0:name>
           </ns0:transaction>
    </ns0:transactions>The The Scheme is given below
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://xml.netbeans.org/schema/testSchema"
                xmlns:tns="http://xml.netbeans.org/schema/testSchema"
                elementFormDefault="qualified">
        <xsd:element name="transactions">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="transaction" maxOccurs="unbounded">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="transid" type="xsd:int"/>
                                <xsd:element name="name" type="xsd:string"/>
                            </xsd:sequence>
                        </xsd:complexType>
                        <xsd:key name="transaction">
                            <xsd:selector xpath="transaction"/>
                            <xsd:field xpath="transid"/>
                        </xsd:key>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>The validation code is given below:
    package schematest;
    import java.io.File;
    import java.io.IOException;
    import javax.xml.XMLConstants;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Source;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;
    * @author irfan.masood
    public class Main {
         * @param args the command line arguments
        public static void main(String[] args) {
            try {
                // Parse an XML document into a DOM tree.
                DocumentBuilder parser =
                        DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document document = parser.parse(new File("test.xml"));
                // Create a SchemaFactory capable of understanding WXS schemas.
                SchemaFactory factory =
                        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                // Load a WXS schema, represented by a Schema instance.
                Source schemaFile = new StreamSource(new File("src/testSchema.xsd"));
                Schema schema = factory.newSchema(schemaFile);
                // Create a Validator object, which can be used to validate
                // an instance document.
                Validator validator = schema.newValidator();
                // Validate the DOM tree.
                validator.validate(new DOMSource(document));
            } catch (ParserConfigurationException e) {
                // exception handling
                e.printStackTrace();
            } catch (SAXException e) {
                // exception handling - document not valid!
                e.printStackTrace();
            } catch (IOException e) {
                // exception handling
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            catch( Throwable t ){
                t.printStackTrace();
    }You can see that the tag (<ns0:transid>) on which the key constraint is applied have duplicate values but still the xml is validated successfully. It should not be validating successfully, please see where I am wrong!
    Many Thanks!
    Irfan

    my guess is that the "xsd:key" constraint needs to be in the "transactions" element, not the "transaction" element.

Maybe you are looking for