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

Similar Messages

  • 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

  • No primary key attribute and does not support rowId -- URGENT : ADF Swing

    Hi , i have created a view and while create Entity Object through Business Components wizard for that view i am getting following error.
    ENTITY CREATION FAILED FOR THE FOLLOWING DATABASE OBJECT
    NO PRIMARY KEY ATTRIBUTES AND DOES NOT SUPPORT ROWID . USE ENTITY WIZARD TO CREATE THE ENTITY OBJECT.
    very urgent.
    Jdeveloper version :: 10.1.3.1.0
    oracle version :: 10g
    view script is :
    SELECT
    oh.order_number||'- ('||ol.line_number||'-'||ol.shipment_number||')' so_line,
    oh.cust_po_number customer_po,
    ol.flow_status_code status,
    ol.shipping_method_code carrier,
    ol.ordered_item item_no,
    nvl(xxapplication_express_pkg.getcustomerpart(ol.inventory_item_id, hca.cust_account_id), ' ') customer_item_number,
    nvl(mtl.description , 'na') description,
    nvl(org.organization_name , 'oak park') ship_from,
    hcsu.location||' ('||hl.city||' '||hl.state||' '||hl.postal_code||')' ship_to,
    nvl(ol.promise_date, sysdate) promised_date,
    nvl(ol.ordered_quantity, 0) order_quality ,
    xxapps.xx_eg_calculated_onhand(ol.line_id) available_qty,
    nvl(ol.shipped_quantity, 0) shipped_quality ,
    ol.order_quantity_uom uom,
    hp.party_name customer_name,
    jrs.name sales_person_name,
    0 refer
    FROM
    oe_order_headers_all oh,
    oe_order_lines_all ol,
    hz_cust_accounts hca,
    hz_parties hp ,
    hz_cust_site_uses_all hcsu,
    hz_cust_acct_sites_all hcas,
    hz_party_sites hps,
    hz_locations hl,
    org_organization_definitions org ,
    mtl_system_items_b mtl ,
    jtf_rs_salesreps jrs
    WHERE
    oh.header_id = ol.header_id and
    oh.org_id = ol.org_id and
    oh.sold_to_org_id = hca.cust_account_id and
    hca.party_id = hp.party_id and
    hcas.cust_account_id = hca.cust_account_id and
    hcsu.cust_acct_site_id = hcas.cust_acct_site_id and
    hcas.party_site_id = hps.party_site_id and
    hps.location_id = hl.location_id and
    hcsu.site_use_id = oh.ship_to_org_id and
    hcsu.site_use_code = 'SHIP_TO' and
    org.organization_id = ol.ship_from_org_id and
    org.organization_id = mtl.organization_id and
    ol.inventory_item_id = mtl.inventory_item_id and
    ol.ship_from_org_id = mtl.organization_id and
    ol.salesrep_id = jrs.salesrep_id and
    ol.org_id = jrs.org_id
    can i know will it check for any constraints while correlation multiple table or whether this type of exception can be sorted out .
    need help looking forward to reply...
    Regards
    bhanu prakash
    Message was edited by:
    user579125
    Message was edited by:
    user579125

    User,
    I assume that this is for read-only activity. If so, don't create an EO from the database view; create a read-only view object instead. The only reason you'd want to use an Entity Object is if you were going to insert/update through the EO; unless you have an instead-of trigger, this database view is non-insertable/non-updatable.
    Short answer: I think what you want to do is a read-only View Object (not EO) - put "select x, y, z (your columns) from view_name" as the SQL.
    John

  • SAPF150D-Dunning Notice Print - With Update of Line Items and Master Record

    When I am running this program SAPF150D(Dunning Notice Print - With Update of Line Items and Master Records) with a variant containing run date, runc id, update mode, pimmed & pcount; the job is failing to execute. It is giving message like,
    "The field symbol is no longer assigned because there was an attempt makde previously in a Unicode program to set the field symbol using ASSIGN with offset and/or length specification. Here, the memory addressed by the offset/length specification was not within the allowed area."
    "GETWA_NOT_ASSIGNED_RANGE" "SAPF150D" or "F150DFS0" "SORTFELDER_ERMITTELN"

    Hi,
    Search for routine FORM SORTFELDER_ERMITTELN
    in SAPF150D.
    Put a breakpoint in statement
    ASSIGN (T047F-FELDN) TO <H1>.
    in above routine and debug to the point where error is occuring.
    Most likely there is a mismatch in the length assigned to some field FELDN in table T047F.
    In that case, simply change the T047F-LENGT field accordingly in change mode.
    Cheers,
    Vikram
    Edited by: Vikram Jalali on May 27, 2008 5:56 PM

  • 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

  • Updating Primary Key Records and Related FK dependencies

    1) How can I found out the name of all the tables that are using my primary key as foreign keys? The primary key is a varchar & used in many other tables. Now the PK records must be changed per client request, so it will obviously affect all my dependent tables.
    2) What is the most efficient PL/SQL script to perform this procedure?
    Any suggestions will be helpful.
    For testing I think I'd like a list of all the tables followed by a list of all the records affected. This report should display the PK record & all dependency records requiring an update. Is this reasonable?

    user10998542 wrote:
    1) How can I found out the name of all the tables that are using my primary key as foreign keys? The primary key is a varchar & used in many other tables. Now the PK records must be changed per client request, so it will obviously affect all my dependent tables. Check out the USER_CONSTRAINTS view. You could do something like this:
    SELECT     UC.OWNER
    ,     UC.TABLE_NAME
    ,     UC.R_OWNER
    ,     RUC.TABLE_NAME
    ,     UC.R_CONSTRAINT_NAME
    ,     RUC.CONSTRAINT_TYPE
    FROM     USER_CONSTRAINTS UC
    ,     USER_CONSTRAINTS RUC
    WHERE     UC.R_OWNER          = RUC.OWNER
    AND     UC.R_CONSTRAINT_NAME     = RUC.CONSTRAINT_NAME
    AND     UC.CONSTRAINT_TYPE     = 'R'If you need column names you can use the USER_CONS_COLUMNS view as well.
    2) What is the most efficient PL/SQL script to perform this procedure?
    Any suggestions will be helpful.[Tom Kyte: Update Cascade Package|http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html]

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

  • Primary key violation when inserting records with insert_identity and ident_current+x

    My problem is this: I have data in a couple of temporary tables including
    relations (one table referencing records from another table by using
    temporary keys).
    Next, I would like to insert the data from the temp tables to my productive tables which have the same structure but their own identity keys. Hence, I need to translate the 'temporary' keys to regular identity keys in my productive tables.
    This is even more difficult because the system is highly concurrent, i.e. multiple sessions may try to insert
    data that way simultaneously.
    So far we were running the following solution, using a combination of
    identity_insert and ident_current:
    create table doc(id int identity primary key, number varchar(100))
    create table pos (id int identity primary key, docid int references doc(id), qty int)
    create table #doc(idx int, number varchar(100))
    create table #pos (docidx int, qty int)
    insert #doc select 1, 'D1'
    insert #doc select 2, 'D2'
    insert #pos select 1, 10
    insert #pos select 1, 12
    insert #pos select 2, 32
    insert #pos select 2, 9
    declare @docids table(ID int)
    set identity_insert doc on
    insert doc (id,number)
    output inserted.ID into @docids
    select ident_current('doc')+idx,number from #doc
    set identity_insert doc off
    -- Since scope_identity() is not reliable, we get the inserted identity values this way:
    declare @docID int = (select min(ID) from @docids)
    insert pos (docid,qty) select @docID+docidx-1, qty from #pos
    Since the request to ident_current() is located directly in the insert statement, we always have an implicit transaction which should be thread safe to a certain extend.
    We never had a problem with this solution for years until recently when we were running in occasional primary key violations. After some reasearch it turned out, that there were concurrent sessions trying to insert records in this way.
    Does anybody have an explanation for the primary key violations or an alternative solution for the problem?
    Thank you
    David

    >> My problem is this: I have data in a couple of temporary tables including relations (one table referencing records [sic] from another table by using temporary keys [sic]). <<
    NO, your problem is that you have no idea how RDBMS and SQL work. 
    1. Rows are not anything like records; this is a basic concept.
    2. Temp tables are how old magnetic tape file mimic scratch tapes. SQL programmers use CTEs, views, derived tables, etc. 
    3. Keys are a subset of attributes of an entity, fundamental characteristics of them! A key cannot be temporary by definition. 
    >> Next, I would like to insert the data from the temp tables to my production tables which have the same structure but their own IDENTITY keys. Hence, I need to translate the 'temporary' keys to regular IDENTITY keys in my productive tables. <<
    NO, you just get worse. IDENTITY is a 1970's Sybase/UNIX dialect, based on the sequential file structure used on 16-bit mini computers back then. It counts the physical insertion attempts (not even successes!) and has nothing to with a logical data model. This
    is a mag tape drive model of 1960's EDP, and not RDBMS.
    >> This is even more difficult because the system is highly concurrent, i.e. multiple sessions may try to insert data that way simultaneously. <<
    Gee, that is how magnetic tapes work, with queues. This is one of many reasons competent SQL programers do not use IDENTITY. 
    >> So far we were running the following solution, using a combination of IDENTITY_INSERT and IDENT_CURRENT: <<
    This is a kludge, not a solution. 
    There is no such thing as a generic “id” in RDBMS; it has to be “<something in particular>_id” to be valid. You have no idea what the ISO-11179 rules are. Even worse, your generic “id” changes names from table to table! By magic, it starts as a “Doc”,
    then becomes a “Pos” in the next table! Does it wind up as a “doc-id”? Can it become a automobile? A squid? Lady Gaga? 
    This is the first principle of any data model; it is based on the Law of Identity; remember that from Freshman Logic 101? A data element has one and only one name in a model. 
    And finally, you do not know the correct syntax for INSERT INTO, so you use the 1970's Sybase/UNIX dialect! The ANSI/ISO Standard uses a table consrtuctor: 
    INSERT INTO Doc VALUES (1, 'D1'), (2, 'D2'); 
    >> We never had a problem with this solution for years until recently when we were running in occasional PRIMARY KEY violations. After some research it turned out, that there were concurrent sessions trying to insert records [sic] in this way. <<
    “No matter how far you have gone down the wrong road, turn around.” -- Turkish proverb. 
    You have been mimicking a mag tape file system and have not written correct SQL. It has caught up with you in a way you can see. Throw out this garbage and do it right. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

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

  • Chaging primary key value when child record present

    Please tell me how to change the primary key value when it's child record is present.

    Well something like INSERT new primary key, UPDATE child records, DELETE old primary key would seem fairly obvious.
    Using deferrable constraints may make the process rather simpler.
    If on the other hand you are looking for UPDATE cascade functionality, Tom Kyte has an article regarding implementing this.
    http://asktom.oracle.com/tkyte/update_cascade/index.html

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

    Here is my scenario:
    I have inserted 5 rows in mobile client with primary key 1, 2, 3, 4,5
    Then deleted 3rd row and updated the primary key for 4th and 5th row to make the primary key sequential.
    But when I sync, I get conflict error with DML Operation update
    Does oracle lite support primary key update or primary key reshuffle?
    Thanks

    Hi,
    can you apply this one-off patch 7337669 Oracle Database Lite: Patch
    CONSOLIDATED ONEOFF FOR CUSTOMER BUGS TILL AUG 19 2008 10.3.0.1.0 19-AUG-2008
    and tell me if you are still able to reproduce this behavior?
    The Patch is available on metakink.oracle.com
    Regards.
    Marc

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

  • Primary key deletion in custom database table

    hi,
        I've created a ztable.In which i've have created primary key and foreign key.
    I've activated the table.Now my requirement is that i want to delete the primary key as i've shoosen wrong field as primary key.
    For that first of all i deleted the foreign key relationship than i have unchecked the primary key but it is giving me an error.
    even though if i deleted that field having primary key it is also giving me error as "Primary key change not permitted for value table.
    Please help me out if possible.
    waiting for ur response.
    Thanks,
    Dheeraj

    Make sure u dont have any data in the table..u have to delete all the data..Also make sure you are not using that table any where else.do a where used list in the table.

Maybe you are looking for

  • Problems with Auto Pay Activation

    Three months ago, I spoke to a Verizon person (English speaking) and signed up for Auto Bill Pay and was told it could take two billing cycles to activate. Apparently, this has never happened because I received a third bill indicated I have a past du

  • How can I use my synced bookmarks on another computer without merge?

    I don't want end up with all the bookmarks from each PC that I use Firefox on. If I go to another computer that has Firefox, I don't want the bookmarks, history, etc. from this computer. In other words, absolutely no merging of my stuff with their st

  • Custom Metadata View

    Is there any way to change the display size of custom metadata labels in the inspector? I have created a bunch of custom metadata labels and given them names that are truncated when displayed in the metadata inspector. When I resize the inspector pan

  • Disk size early 2009

    When the XServe 2009 was released, 2TB drives were new. Will the hardware support any of the new larger drives currently available?

  • JRCMD

    Hi, By default, the jrcmd start_management_server and -Xmanagement java option both start up the listener on all IPs instead of limiting it to the IP that is hosting the JVM. We have a large number of app server instances running on each physical Lin