Updating primary key with jdbc

Trying to update a primary key with:
public static void updateTAXID(String coid,String accountnumber,String oldTAXID,String fundname,String newTAXID){ Properties props = TradeHelper.loadProperties(); Connection conn = null; PreparedStatement st = null; String sql ="update FinancialAccount set taxid=? " + "where dealernumber=? and " + "taxid=? and " + "fundname=? and " + "accountnumber=?"; try{ Class.forName(props.getProperty("driver")); conn = DriverManager.getConnection(props.getProperty("brokerageurl"), props.getProperty("user"),props.getProperty("password")); st = conn.prepareStatement(sql); st.setString(1, newTAXID); st.setString(2, coid); st.setString(3, oldTAXID); st.setString(4, fundname); st.setString(5, accountnumber); st.executeUpdate(); System.out.println("Update: new tx:"+newTAXID+":"+"Ol tx: "+oldTAXID+" act:"+accountnumber); conn.close(); st.close(); }catch(Exception e){ e.printStackTrace(); } }
I keep getting this error:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '02673025561-QA-SLFCA-' for key 'PRIMARY'         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1011)         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)         at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)         at com.mysql.jdbc.PreparedStatement.exe
Am I not able to update a single field of a composit key with jdbc? Whay is my update failing telling that there is a duplicate when I am trying to update?

iketurner wrote:
I keep getting this error:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '02673025561-QA-SLFCA-' for key 'PRIMARY'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1011)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.exeAm I not able to update a single field of a composit key with jdbc? Whay is my update failing telling that there is a duplicate when I am trying to update?If you take the error message at its word then the problem is not that you are not permitted to update a primary key but that the values you are trying to use is already in use.
I'll leave it you DBA and/or Data Modeler/Architech to decide if it is a good idea to update the value or not.

Similar Messages

  • Update primary key with a tabular form based on a select list for each row

    Hello!
    I've two tables: Table1 with only one column (primary key) is a foreign key for table2.column1 (primary key). There is also a second primary key column in table2.
    Now I want to change the primary key values in table2.column1 with a tabular form (MRU) based on a select list (LOV based on table1.column1) for each row.
    The user should be able to choose for every row a new value from the select list to change the old primary key value at this position.
    How can I do this with ApEx?
    I've the tabular form and so on, but at the moment I get the following error:
    "Error in mru internal routine: ORA-20001: Fehler in MRU: row= 1, ORA-20001: ORA-20001: Die aktuelle Version der Daten in der Datenbank wurde geändert, seit der Benutzer einen Update-Prozess eingeleitet hat. ..."
    Thank you for your support!
    Kay

    Hello!
    I've two tables: Table1 with only one column (primary key) is a foreign key for table2.column1 (primary key). There is also a second primary key column in table2.
    Now I want to change the primary key values in table2.column1 with a tabular form (MRU) based on a select list (LOV based on table1.column1) for each row.
    The user should be able to choose for every row a new value from the select list to change the old primary key value at this position.
    How can I do this with ApEx?
    I've the tabular form and so on, but at the moment I get the following error:
    "Error in mru internal routine: ORA-20001: Fehler in MRU: row= 1, ORA-20001: ORA-20001: Die aktuelle Version der Daten in der Datenbank wurde geändert, seit der Benutzer einen Update-Prozess eingeleitet hat. ..."
    Thank you for your support!
    Kay

  • Primary Key With Four Column ,How to use that Column As Primary Key In Form

    Dear All,
    i have create composite Key as primary Key With Four Column .
    How can i use these column in Form With Report Page.
    How can i use these four Column as primary Key to Edit Record .
    How can i do this.
    Thanks
    Vedant

    wrap a view around your table with this bogus primary key. Use an instead of trigger to handle the inserts and updates. And then look at using surrogate keys to resolve these bad design issues...
    5 out of 4 people don't understand fractions.
    Thank you,
    Tony Miller
    Webster, TX

  • Update primary key

    Hi
    I am using weblogic 7.0 and ejb 2.0
    I have a requirement where i would want to update my primary key. If i understand right , In ejb you are not allowed to update one of the primary key column of composite primary key.
    Now if i have to update this what are the work arounds. Aslo say if i update that row through stored proc, will my ejb state in cache be in sync with latest data. Is ejb load called for sure beofre any method is called on bean.
    Please give me some inputs on this
    Thanks

    Your problem lies within your database (datasource)structure. You need to have two primary keys for that particular entity. The first primay key is internal meaning it is the true key for any specific record in the table. The second "primary key" is external meaning it is not truely a primary key with regards to the formal contstraints of your database. Instead, this is the key your users will see. It is the responsibilty of the application layer to ensure that the external id is unique. This way, you always have a lasting primary key (the internal key), yet your users can change their "primary key" whenever they like.
    As an example, I'm writing a J2EE application that provides our clients with a unique ID to access their records. My table definition would look something like:
    CREATE TABLE Client (
    clientId BIGINT AUTO_INCREMENT NOT NULL,
    externalClientId VARCHAR(15) UNIQUE NOT NULL,
    ... another field,
    ... another field,
    CONSTRAINT pk_foo PRIMARY KEY (clientId)
    The unique constraint isn't really necessary because the application layer should check for this, but it's a good safety precaution.
    On the application side, you would only expose the 'externalClientId' to the clients. This might force you to use BMP, but it keeps the user from ever changing the true Id and is fairly clean.
    Hope this helps.

  • Update primary key/delete and reinsert records

    Hi, I have been told to update some fields. The database is new to me, and now I see the field is the primary key (and it has child tables with that as foreign key). The person who told me isn't reachable until next week...
    ...wasn't a bad idea to update pk's? I see it's possible, deferring constraints...
    update primary key
    Re: Update Primary Key
    ...but it's better/safer the script deletes and later re-insert the records, right?
    Thanks

    Always is better be able to undo any change .
    But ¡, why would you update pk's? it's possible but you colud find few problems like:
    - many levels of foreign keys exists.
    - not validate pk
    Look at:
    Re: Novalidate primary key

  • Creating a primary key with the parallel option and the tablespace option

    I know I can create a unique index with these options and then make the primary key with the "using index" clause. anyway to do this and skip the create unique index and just create the primary key ?

    SQL> ALTER TABLE t
      2  ADD CONSTRAINT pk_t
      3  PRIMARY KEY (testcol)
      4  USING INDEX
      5  TABLESPACE example
      6  PARALLEL (DEGREE 2);
    PARALLEL (DEGREE 2)
    ERROR at line 6:
    ORA-03001: unimplemented featureYou can name the tablespace but you must do an alter index thereafter.

  • Update primary key in replication environment.

    Hello all,
    I would appreciate, if you could answer for my question.
    I am using oracle9i advanced asynchronous replication. What would be the issue if we update the primary key. Is it going to create any problem ?. What is the drawback on updating Primary key.
    Thanks
    Govind

    Are you using DDL propagartion?
    You should not have any problems if you have unconditional supplement logging for all columns.
    But, probably it is good idea to do it in this way:
    1 stop capture
    2 change session tag
    3. change table defenition on both sides
    4. enable capture.
    5. perform capture checkpoint right after step 4.
    Regards,
    Sergey

  • ON UPDATE CASCADE and Autoincrement primary key with Oracle SQL Developer

    hello everybody,
    I want to know if Oracle SQL Developer manage autoincrement on primary key and "ON UPDATE CASCADE" when i migrate (with data) SQL Server database in Oracle database.
    Can you help me ?
    Thanks for your suggestions.

    Obtain the value of the auto increment key with the getGeneratedKeys() method.
    Statement stmt = null;
    ResultSet rs = null;
    stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                    java.sql.ResultSet.CONCUR_UPDATABLE);
    stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTable");
        stmt.executeUpdate(
                "CREATE TABLE autoIncTable ("
                + "priKey INT NOT NULL AUTO_INCREMENT, "
                + "dataField VARCHAR(64), PRIMARY KEY (priKey))");
    stmt.executeUpdate(
                "INSERT INTO autoIncTable  (dataField) "
                + "values ('data field value')",
                Statement.RETURN_GENERATED_KEYS);
    int autoIncKeyFromApi = -1;
        rs = stmt.getGeneratedKeys();
        if (rs.next()) {
            autoIncKeyFromApi = rs.getInt(1);
        } else {
        rs.close();

  • Tabular Form Page - Insert and Update Primary Key

    Hi -
    I am creating a Tabular Form page which will display 2 columns:
    CODE (Text Field), DESCRIPTION(Field)
    The *'CODE'* is a primary key to the table/view and *'DESCRIPTION'* is the corresponding detail information for the *'CODE'*.
    I am trying to create an updateable report on this page which will show these columns and when a user wants to add a new row,they should be able to add a new row with CODE='somecode' and DESCRIPTION='description' but shouldn't be able to update (but should be displayed) the *'CODE'* value once inserted into the table or view.
    Is there a way to do that? Please share your expertise.
    Thanks,
    -Seenu

    I't a bit unclear what you want.
    In your first post it seems that you don't want the user to change the PK.
    In your second post the user can change the PK, but you get a db error.
    Anyway, I guess you don't want the user to change the PK.
    I have a few support tables, which are the source for some LOV's.
    For those I also add records with a manual assigned PK, rather then via a sequence, just to improve consistency across applications.
    The data is protected by foreign keys to the main tables.
    You could further add a trigger to your table which throws an error the moment it detects a change on your PK.

  • Autofill primary key with another field

    I have 2 fields:
    DISTRIB_CODE ex. ABC
    DISTRIBUTOR ex. ABC COMPANY
    I want the DISTRIB_CODE to be the primary key, but I cannot do that because DISTRIB_CODE occasionally changes. So, I think I thought of a way to work around it:
    DISTRIB_ID (PK constraint)
    DISTRIB_CODE (Unique Key constraint) ex. ABC
    DISTRIBUTOR ex. ABC COMPANY
    I want DISTRIB_ID to auto-fill when I enter a code into DISTRIB_CODE.
    Example: When I enter "ABC" into DISTRIB_CODE I want DISTRIB_ID to auto-fill with "abc".
    Can this be done? Are there any negatives of doing this? I am a BEGINNER, so please be detailed.
    Thank you.

    Hi James,
    I appreciate that using a sequence based primary key is not the same as an updatable one. However, if the PK is updatable and is intended for use elsewhere within the database as a foreign key, you would also need to have cascade-update switched on to ensure that all child tables are updated with any new value. The only thing that you would need when using a sequential PK is a unique constraint on the distrib_code field to ensure that duplicates are not possible.
    Additionally, if the PK remains updatable, using javascript to populate it based on the second field would only be part of the process as you would still need item or page validation to ensure that it is unique before attempting to write the data into the table.
    So, I think it would be easier all round if the PK was numeric and sequential, the distrib_code column had a unique constraint defined on the table and that there is item or page validation that checks for duplicates and returns user-friendly messages to the user.
    Regards
    Andy

  • How to update primary key column

    Hi,
    Can you suggest me best workaround/algorithm for below task:
    (Oracle 10g, Solaris OS.)
    Situation:
    Table P has primary key column "Code", child tables F1, F2, ..., F15 reference with foreign key column "P_Code" column "P.Code", and we don't know which of the child tables has data for particular "P.Code" value.
    Task:
    Change "P.Code" value from 100 to 200. So that result would be that record P[Code = 100] should be updated as:
    update P set
    Code = 200
    where Code = 100;And child tables column "P_Code" should be updated as:
    update F1, F2, .., F15 set
    P_code = 200
    where P_code = 100;The best solution would be that one very easily can repeat that task.
    Edited by: CharlesRoos on 28.12.2010 12:10

    If you are looking for reusable and repetitive solution, then may be...
    SQL> CREATE TABLE p (p_code NUMBER PRIMARY KEY);
    Table created.
    SQL> INSERT INTO p VALUES(100);
    1 row created.
    SQL> INSERT INTO p VALUES(300);
    1 row created.
    SQL> INSERT INTO p VALUES(500);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> CREATE TABLE F1 (p_code NUMBER REFERENCES p(p_code));
    Table created.
    SQL> CREATE TABLE F2 (p_code NUMBER REFERENCES p(p_code));
    Table created.
    SQL> CREATE TABLE F3 (p_code NUMBER REFERENCES p(p_code));
    Table created.
    SQL> INSERT INTO F1 VALUES(100);
    1 row created.
    SQL> INSERT INTO F3 VALUES(100);
    1 row created.
    SQL> INSERT INTO F2 VALUES(500);
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> CREATE OR REPLACE PROCEDURE update_child_parent(pi_p_code_old NUMBER,
      2                                                  pi_p_code_new NUMBER) IS
      3    CURSOR table_to_update IS
      4      SELECT table_name,
      5             to_number(extractvalue(xmltype(DBMS_XMLGEN.getxml('SELECT count(*) c FROM ' ||
      6                                                               table_name ||
      7                                                               ' WHERE p_code=' ||
      8                                                               pi_p_code_old)),
      9                                    '/ROWSET/ROW/C')) cnt
    10        FROM user_tables
    11       WHERE table_name IN ('F1', 'F2', 'F3');
    12 
    13  BEGIN
    14    EXECUTE IMMEDIATE 'ALTER TABLE p DISABLE PRIMARY KEY CASCADE';
    15    UPDATE p SET p_code = pi_p_code_new WHERE p_code = pi_p_code_old;
    16    FOR i IN table_to_update LOOP
    17      IF i.cnt > 0 THEN
    18        EXECUTE IMMEDIATE 'UPDATE ' || i.table_name || ' SET p_code=' ||
    19                          pi_p_code_new || ' WHERE p_code=' || pi_p_code_old;
    20      END IF;
    21    END LOOP;
    22    EXECUTE IMMEDIATE 'ALTER TABLE p ENABLE VALIDATE PRIMARY KEY';
    23  END update_child_parent;
    24  /
    Procedure created.
    SQL> EXECUTE update_child_parent(100,200);
    PL/SQL procedure successfully completed.
    SQL> SELECT * FROM p;
        P_CODE
           200
           300
           500
    SQL> SELECT * FROM F1;
        P_CODE
           200
    SQL> SELECT * FROM F2;
        P_CODE
           500
    SQL> SELECT * FROM F3;
        P_CODE
           200
    SQL> INSERT INTO p VALUES(300);
    INSERT INTO p VALUES(300)
    ERROR at line 1:
    ORA-00001: unique constraint (HR.SYS_C005931) violated
    SQL> EXECUTE update_child_parent(500,900);
    PL/SQL procedure successfully completed.
    SQL> SELECT * FROM p;
        P_CODE
           200
           300
           900
    SQL>  SELECT * FROM F2;
        P_CODE
           900
    SQL>

  • How do u automatically generate a primary key using JDBC?

    ok, basically i'm given a simple schema
    with 5 fields, one of these are made as a constraint made for primary keys.
    I am programming under Oracle, using JDBC, and everytime i insert a new record into the table using an INSERT statement it says it cant make the id null, well yeah, then how exactly do i make it generate a new ID?
    Thanks guys.

    In the table schema, the cid field is a INTEGER
    type.
    Ok i must of accidentally typed an extra digit, it's
    working now hmmm but i'm starting from this digit now
    : 1000000016
    If i go more higher and it truncates again, is there
    anyway to fix that?No. You can however write your code to verify it.
    Noting of course that the problem will next occur after 1 billion records have been added. So I would question at what rate you think that that table is going to grow.
    Of course you could create your own id but that would require modifying the table.

  • Update primary key that’s also a foreign key in another table

    Hi Developers,
    I need to update the primary key for a record but it's also the foreign key in another table.
    Example,
    Table 1 Details
    Name : Parent_Table
    Columns : ID, Name, Age
    Primary Key : ID
    Table 2 Details
    Name : Child_Table
    Columns : ID, Parent_ID, Name, Age
    Primary Key : ID
    Foreign Key : Parent_ID (Primary Key in Parent_Table)
    Parent_Table
    ID Name Age
    1001 Sam 26
    1002 George 25
    Child_Table
    ID Parent_ID Name Age
    1010 1001 Sam 26
    1020 1002 George 25
    Now I want to update ID (1001) in Parent_table as 2001 and also, I want to update Parent_ID (1001) in Child_Table as 2001.
    How we will write the java code to update these columns.
    Thanks in advance.

    dcminter wrote:
    If you're looking at changing the primary keys in your data then there's probably something wrong with your data structure.Depends how you feel about business primary keys versus surrogates. Personally I prefer the latter so I'm with you in theory. In practice, however, a DB that uses a business PK may well find that it's a legitimate use case to change the key value (but then that's why I like surrogates in the first place!)Primary keys should not have meaning.

  • Updating primary key

    HI ,
    I want to update the primary (DATBI) key of SAP table a017 with new value. I am using update statement and giving all the primary keys for identifying the record, but lasting in short dump.
    any pointers on how this can be done?
    Regards
    saurabh.

    hi
    chk this
    <b>You cannot change the primary key when you UPDATE a table for which a synchronous matchcode is defined.</b>
    another eason might b the CLIENT SPECIFIC condition needed
    UPDATE sflight <b>CLIENT SPECIFIED</b>
                   SET   seatsocc = seatsocc + 3
                   WHERE mandt    = '002'  AND
                         carrid   = 'LH'   AND
                         connid   = '0400' AND
                         fldate   = '20010228'.

  • Updating Primary key while creating entries

    Hi All,
    I have a requirement to update a primary key while the entry is being created in the database table.
    The program uses the INSERT statement to create entries but the primary key is unknown at that time. I want to create this primary key value and update that record before being saved in the database.
    Is there a place where i can write this code which gets called while INSERT/UPDATE statements.
    Request you urgent help.
    Thanks in advance

    Hello Archana,
    As per my understanding providing BADI implementation is one of the better options you have.
    Firstly you have to find out the standard BADI Definition provided by SAP for your functionality. You can search the same in the transaction <b>SE18</b>.
    Your BADI definition will have predefined interface methods
    (like BEFORE_SAVE, AFTER_SAVE, BEFORE_UPDATE, AFTER_UPDATE ... )
    with necessary parameters(parameter types: IMPORTING/EXPORTING/CHANGING/RETURNING). Mind you, you can't add your own methods/parameters to these standard BADI definitions.
    Now you can create your own BADI implementation(ie., you can write you code in the one of the predefined methods you have) for the standard BADI definition. For that in your BADI Definition screen go to <i>Implementation</i> in Menu bar and click on <i>Create</i>.
    I hope you will definitely have <b>Primary Key</b> as importing parameter so you can write your code logic by using this primary key in one of the methods you have based on your requirement
    <b>@</b> Please take Backend expert help in your team for finding out BADI Definition Name & for also BADI Implementation Code if you are not sure about BADI concept.
    I hope this answers your question, If YES,  !!! REWARD POINTS !!!
    Regards, Srikanth

Maybe you are looking for

  • Itunes files won't show up in the menu

    to import music into a movie, i found out that the dropdown menu only shows me as available, the preinstalled Apple audio effects and short audio files in iMovie. How do i make other forlders and itunes files available to imovie? imovie 11 mac osx 10

  • Airplay movie stops and start over again

    I have problem with AirPlay while watching play-channels from my iPad. After a while the program just starts over again. What can I do to fix this?

  • Brand new MacBook Pro does not work

    Today I was SO excited to receive my brand new Mac in the mail only to find out the USB ports do not work I can't sign into my wifi no matter how many times I restart and try all these other tricks. A few hours on the phone with apple care later the

  • HT2347 USB mouse not detected

    I just got my new Mac mini today and when trying to set it up, I keep getting asked to turn on my Magic Mouse or my trackpad.  I didn't buy either yet, as I figured I would use my old wired mouse for now.  Any thoughts on why it won't detect my mouse

  • Can a job have a start time different from its schedule?

    I have a WEEKDAY schedule defined as follows: begin      dbms_scheduler.create_schedule (           schedule_name => 'DAILY',           repeat_interval => 'FREQ=DAILY; BYDAY=SUN,MON,TUE,WED,THU,FRI,SAT; BYHOUR=0;BYMINUTE=1;BYSECOND=0',           comm