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!

Similar Messages

  • 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 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;
    /

  • 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!

  • 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

  • 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.

  • 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

  • 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

  • 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.

  • Enable/disable constraint

    Hi!
    Kindly help.
    I'm using 9207 on win server 2k3.
    I have a master table with 100M records, I need to retain only 10M of data. I plan to put the 10M of data to be retained to a temp table, I truncate the master table, then I put the 10M of data from the temp table back to the master table. I will use the parallel and nologging for the index and table and I'll use the skip_unusable_index option. Is it ok to disable the constraints? After the bulk load I'll still be able to enable them correct?
    I'm just worrying that I might not be able to enable the constraints.
    Thanks and Best REgards
    Dexter

    You can disable the constraints before the operation and enable them afterwards. It will not be a problem unless you insert invalid data not matching your constraints.
    You can insert the rows you want into a new table, create indexes and constraints and then rename that table. This way you do not need to reinsert the rows back to the original table from the temp table.

  • I cannot Disable a constraint in SQL Developer

    Here is my issue:
    I have created a table already named practice1.
    Using SQL Developer PL/SQL try to run a loop as following:
    DECLARE
    COUNTER1  NUMBER(2);
    BEGIN
    COUNTER1 := 30;
    ALTER TABLE practice1
    DISABLE  CONSTRAINT PRK1;
    LOOP
    COUNTER1 := 30;
    INSERT INTO PRACTICE1
    VALUES (COUNTER1, 'test7', 8, 9);
    EXIT WHEN  COUNTER1 >26;
    END LOOP;
    END;
    In other words I Insert the COUNTER1 variable value as Primary Key in the tables field1 column.
    I run the script successfully without the ALTER TABLE DISABLE CONTSRAINT.. command.
    Everytime I run it I had to increase the starting value of Variable COUNTER1 so it will not attempt to insert a duplicate pre-existed value in Primary Key.
    Then I decided to insert the command ALTER TABLE DISABLE CONSTRAINT in order to not have to worry to change the starting value.
    I am able to disable the constraint by using the same command in isolation . If I run it as part of the script as above I get the following error:
    Error report:
    ORA-06550: line 5, column 1:
    PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
       ( begin case declare end exception exit for goto if loop mod
       null pragma raise return select update while with
       <an identifier> <a double-quoted delimited-identifier>
       <a bind variable> << continue close current delete fetch lock
       insert open rollback savepoint set sql execute commit forall
       merge pipe purge
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:
    I would appreciate any suggestions.
    Thank you.

    Your question has NOTHING to do with sql developer.
    Mark this question ANSWERED and repost it in the SQL and PL/SQL forum.
    https://forums.oracle.com/community/developer/english/oracle_database/sql_and_pl_sql
    The problem is that you CANNOT execute DDL directly in PL/SQL. You need to use dynamic SQL to execute DDL within PL/SQL.
    EXECUTE IMMEDIATE 'ALTER TABLE . . .';
    If you need more help use the correct forum as shown above.

  • Stored procedure for disabling all foreign key constraints ?

    hi all,
    I need a stored procedure for disabling / enable all constraints in that database schema.
    Create or replace procedure enable_disable_proc(status varchar2(3),schema_name varchar2(20))
    ---> where 'status' parameter condition should be ( YES for enable) (NO for disable)
    ---> 'schema_name' is a parameter where ,only this schema should be affected with this procedure..
    Thanks in Advance..

    Hi,
    Try this code
    /* Formatted on 2009/07/16 08:15 (Formatter Plus v4.8.8) */
    CREATE PROCEDURE enab_disab_proc (
       enforce          IN   VARCHAR2,
       current_schema   IN   VARCHAR2
    IS
       v_alter_table_sql   VARCHAR2 (2000);
    BEGIN
       FOR rec_con IN (SELECT table_name, constraint_name
                         FROM user_constraints
                        WHERE owner = current_schema)
       LOOP
          IF (enforce = 'NO')
          THEN
             v_alter_table_sql :=
                   ' alter table '
                || current_schema
                || '.'
                || rec_con.table_name
                || ' disable constraint '
                || rec_con.constraint_name;
          ELSE
             v_alter_table_sql :=
                   ' alter table '
                || current_schema
                || '.'
                || rec_con.table_name
                || ' enable constraint '
                || rec_con.constraint_name;
          END IF;
          EXECUTE IMMEDIATE v_alter_table_sql;
       END LOOP;
    END;Edited by: Salim Chelabi on 2009-07-16 05:15

  • Problem in enabling constraint - after disabling and truncation of table .

    Hello Friends,
    I have a table called DRR_TABLES that has list of table names . The requirement is to truncate the tables present in DRR_TABLES except KEY_IDS table and table_name like '%TYPE%' table.
    written a procedure . successfullly truncating the tables from DRR_TABLES but while enabling constraints after truncation , I am getting problem in enabling constraints .
    ERROR at line 1:
    ORA-02270: no matching unique or primary key for this column-list
    ORA-06512: at "schema123.TRUNCATE_DRR_TABLES ", line 49
    ORA-06512: at line 1
    Heres is the code .
    PROCEDURE TRUNCATE_DRR_TABLES is
    x varchar2(200);
    v_tablecount number := 0;
    cursor c is select TABLE_NAME from DRR_TABLES where population_source='PUBLISHING' and TABLE_NAME != 'KEY_IDS' and TABLE_NAME NOT LIKE '%TYPE%';
    BEGIN
    DBMS_OUTPUT.PUT_LINE (' TRUNCATING DRR TABLES ...........');
    OPEN c ;
    LOOP
    FETCH c INTO x ;
    EXIT WHEN c%NOTFOUND;
    for c1 in (select table_name, constraint_name from user_constraints where TABLE_NAME = x and status ='ENABLED' ORDER BY CONSTRAINT_TYPE DESC )
    loop
    begin
    execute immediate ('alter table '||c1.table_name||' disable constraint '||c1.constraint_name|| ' cascade');
    NULL;
    end;
    end loop;
    EXECUTE IMMEDIATE 'TRUNCATE TABLE ' || x ;
    v_tablecount := v_tablecount + 1 ;
    DBMS_OUTPUT.PUT_LINE('TABLE TRUNCATED :'|| x );
    END LOOP ;
    DBMS_OUTPUT.PUT_LINE (' TOTAL TABLES TRUNCATED ' || v_tablecount );
    CLOSE c;
    OPEN c ;
    LOOP
    FETCH c INTO x ;
    EXIT WHEN c%NOTFOUND;
    for c2 in (select table_name, constraint_name from user_constraints where TABLE_NAME = x and status = 'DISABLED' ORDER BY CONSTRAINT_TYPE)
    loop
    begin
    execute immediate ('alter table '||c2.table_name||' enable constraint '||c2.constraint_name);
    NULL;
    end;
    end loop;
    END LOOP ;
    CLOSE c ;
    END TRUNCATE_DRR_TABLES ;
    LINE 49 is the line corresponding to enable constraint statement.
    Edited by: kumar73 on 3 Sep, 2012 11:44 PM

    It is such a pity that a user having 321 posts till date is unaware of basics of Posting a Question.
    1. You need to provide us with your Oracle version
    select * from v$version;2. You need to understand and get accustomed to using tags before and after Code or SQL's for better readability.
    3. You need to provide us with the Table Structure and the Constraints Definition.
    There are many things that looks like Bad Coding practices:
    <font face="Times New Roman" size=2>
    1. Avoid RBAR (Loops).
    2. Implement Bulk Collect.
    3. Why do you need to disable the constraints before truncating? Are you kind of handling the Referential Integrity Constraints?
    4. Duplicate checking of Disabled Constraints.
    5. When the procedure is being executed at Production Environment, are you going to Monitor the DBMS_OUTPUT? Why not Log the Statements into a LOG Table?
    6. Why use a TableCount variable? Would the TableCount Variable be not equal to the Number of Records returned by Cursor C?
    7. What is the need to use a NULL statement after every Execute Immediate?
    8. What is the Need to surround each execute Immediate with Begin .. End block?
    9. Where is your Exception handling Block? Forgot to write?
    </font>
    What has been your effort in finding which Constraint/Table is causing you the trouble?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Maybe you are looking for