How to change a massive amount of foreign keys?

Hi to all!
I'm a newbie here, and I hope I'll find a solution to my problem...
Well, I have to create a copy of row within the same table, with same values except of primary key. That part is ok. Then, I have to change values of that primary key (from old value, to new value) in all foreign keys which references to that column/primary key in parent table. Let's say that isn't problem too, using system tables and informations about constraints and indexes...
But, tricky part comes. Some of those foreign keys are primary keys for their tables, and there are other foreign keys which references to these primary keys, including a column which is primary key in very first (parent) table. And also, that line of primary key-foreign key can go on...
I don't know how deep to check for those keys. Any help with some piece of code (in PL/SQL)? Also, how to start to change value of that, particular column, in line of primary key-foreign key?

Sounds like you need to add a copy function to your application. This will allow you to create a copy of an existing import/export but each will be unique. This would provide you with the ability to change the import/export and retain the old and new details.
A small example using a header and detail table;
select * from orders;
  ORDER_ID ORDER_DATE
         1 27/05/2010
select * from order_lines;
   LINE_ID   ORDER_ID
         1          1
         2          1
         3          1A function that makes a copy of a certain order;
create or replace function copy_order(p_order_id orders.order_id%type)
  return number is
  l_new_order_id orders.order_id%type;
begin
  select orders_seq.nextval
  into l_new_order_id
  from dual;
  insert into orders
    (order_id, order_date)
    select l_new_order_id, sysdate
    from orders
    where order_id = p_order_id;
  insert into order_lines
    (line_id, order_id)
    select order_lines_seq.nextval, l_new_order_id
    from order_lines
    where order_id = p_order_id;
  return l_new_order_id;
end;
Function createdCall the function with order_id 1;
var n number
exec :n := copy_order(1);
PL/SQL procedure successfully completed
print n
n
2There are now 2 similar orders;
select * from orders;
  ORDER_ID ORDER_DATE
         1 27/05/2010
         2 28/05/2010
select * from order_lines;
   LINE_ID   ORDER_ID
         1          1
         2          1
         3          1
         4          2
         5          2
         6          2
6 rows selectedThe first order can be canceled (using a status column or similar) and the second order can be updated, ie add additional lines, etc. or left as is.

Similar Messages

  • How to change Indirect to Direct in Foreign Exchange posting in General Settings

    Hi
    If above mentioned tittle , help me out
    How to change Indirect to Direct in Foreign Exchange posting in General Settings
    Transaction are posting in database now  this field is read only
    Prasad

    Hi Prasad,
    If transaction are posted in Database then you will not be able to change Exchange Rate Posting from Indirect to Direct.
    This is a very initial setting while setting up New Database.
    Regards::::
    Atul Chakraborty

  • How to avoid shared locks when validating foreign keys?

    I have a table with a FK and I want to update a row in that table without being blocked by another transaction which is updating the parent row at the same time. Here is an example:
    CREATE TABLE dbo.ParentTable
    PARENT_ID int NOT NULL,
    VALUE varchar(128) NULL,
    CONSTRAINT PK_ParentTable PRIMARY KEY (PARENT_ID)
    GO
    CREATE TABLE dbo.ChildTable
    CHILD_ID int NOT NULL,
    PARENT_ID INT NULL,
    VALUE varchar(128) NULL,
    CONSTRAINT PK_ChildTable PRIMARY KEY (CHILD_ID),
    CONSTRAINT FK_ChildTable__ParentTable FOREIGN KEY (PARENT_ID)
    REFERENCES dbo.ParentTable (PARENT_ID)
    GO
    INSERT INTO ParentTable(PARENT_ID, VALUE)
    VALUES (1, 'Some value');
    INSERT INTO ChildTable(CHILD_ID, PARENT_ID, VALUE)
    VALUES (1, 1, 'Some value');
    GO
    Now I have 2 transactions running at the same time:
    The first transaction:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;BEGIN TRAN
    UPDATE ParentTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    The second transaction:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;BEGIN TRAN
    UPDATE ChildTable
    SET VALUE = 'Test',
    PARENT_ID = 1
    WHERE CHILD_ID = 1;
    If 'UPDATE ParentTable' statement runs a bit earlier, then 'UPDATE ChildTable' statement is blocked untill the first transaction is committed or rollbacked. It happens because SQL Server acquires shared locks when validating foreign keys, even
    if the transaction is using read uncommitted, read committed snapshot (read committed using row versioning) or snapshot isolation level. I cannot see why change in the ParentTable.VALUE should prevent me from updating ChildTable. Please note that ParentTable.PARENT_ID
    is not changed by the first transaction, which means that from FK's point of view whatevere is set to the ParentTable.VALUE is never a problem for referential integrity. So, such blocking behavior seems to me not logical. Furthermore, it contradicts to the
    MSDN:
    Transactions running at the READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction. READ UNCOMMITTED transactions are also not blocked by exclusive locks that would prevent the
    current transaction from reading rows that have been modified but not committed by other transactions. 
    Does anybody know how to workaround the issue? In other words, are there any tricks to avoid shared locks when validating foreign keys? (Disabling FK is not an option.) Thank you.
    Alexey

    If you change the primary key of the parent table to be nonclustered, there is no blocking.
    Indeed, when I update ParentTable.VALUE, then:
    in case of PK_ParentTable is clustered, a particular row in the clustered index is locked (request_mode:X, resource_type: KEY)
    in case of PK_ParentTable is non-clustered, a particular physical row in the heap is locked (request_mode:X, resource_type: RID).
    and when I update ChildTable.PARENT_ID, then:
    in case of PK_ParentTable is clustered, this index is used to verify referential integrity:
    in case of PK_ParentTable is non-clustered, again this index is used to verify referential integrity, but this time it is not locked:
    It is important to note that in both cases SQL Server acquires shared locks when validating foreign keys. The principal difference is that in case of clustered PK_ParentTable the request is blocked, while for non-clustered index it is granted.
    Thank you, Erland for the idea and explanations.
    The only thing that upsets me is that this solution cannot be applied, because I don't want to convert PK_ParentTable from clustered to non-clustered just to avoid blocking issues. It is a pity that SQL Server is not smart enough to realize that:
    ParentTable.PARENT_ID is not changed and, as a result, should not be locked
    ChildTable.PARENT_ID is not actually changed either (old value in my example is 1 and the new value is also 1) and, as a result, there is no need at all for validating the foreign key.
    In fact, the problem I described is just a tip of the iceberg. The real challenge is that I have deadlocks because of the FK validation. In reality, the first transaction has an additional statement which updates ChildTable:
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    BEGIN TRAN
    UPDATE ParentTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    UPDATE ChildTable
    SET VALUE = 'Test'
    WHERE PARENT_ID = 1;
    The result is famous message:
    Msg 1205, Level 13, State 51, Line xx
    Transaction (Process ID xx) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
    I know that if I change the order of the two statements, it will solve the deadlock issue. But let's imagine I cannot do it. What are the other options?
    Alexey

  • SQL data modeller -- how to create 1 to 1 relationship foreign key ?

    hi guys...
    i got 2 tables..
    table 1 - CFR
    CFR_ID = primary key
    table 2 - USER_PLAN
    USER_ID = primary key
    PLAN_ID,
    CFR_ID = foreign key reference (table 1)
    The business flows go like this..
    insdie CFR table, it contain all records / transactions of a particular user/plan. everytime a new transaction occurs for a plan/user, a new CFR_ID / row will be generated.
    after which, the newly generated CFR_ID for the new row , will be updated to the CFR_ID in USER_PLAN
    Thus, there is always a 1 to 1 relationship between the 2 table regardless how many CFR_ID is generated for a particular USER_PLAN. as the CFR_ID in the USEr_plan table will always be the latest one generated inside the CFR table.
    However, in the data modeller, i am unable to create such foreign key relationship... ANY idea how do i create a 1 on 1 foreign key relationship ? or there is no such way..
    Thanks and Best Regards,
    Noob

    Hi philips,
    Thanks for the wonderful reply..
    Just to double comfirm with you,
    even if i had set a unique constraint on CFR_ID(foreign key column), inside the relationship model, the relationship between the foreign key is still showing as a 1:m relationship right ?
    just that a character 'U' will appear beside the CFR_ID column.
    However the diagraphm is still showing a 1:M relationship.
    is this correct ?
    Regards,
    Noob

  • How to save a record with null foreign key?

    Dear all,
    Most articles show non-nullable foreign keys; but now I've got a problem with nullable foreign keys.
    With this simple example: an employee has zero or one manager.
    in manager object:
    @OneToMany(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, mappedBy="manager")
    private Collection<Employee> employees;
    in employee object:
    @ManyToOne
    @JoinColumn(name="manager_id")
    private Manager manager;
    when updating an employee record, i may use a selectOneMenu to pass a manager id of "0", I expect toplink to save a null manager_id in the employee record; but i tries to create a new manager object of id="0" (i.e. insert a new manager record instead)
    the code i use to save an employee is like this:
    em.getTransaction().begin();
    em.merge(employee);
    em.getTransaction().commit();
    What's the correct way to save the employee record with a null manager_id column?
    Could someone help?
    Thanks a lot!
    AK

    But could you give me some pointers on how could I do
    that from the web UI level?Got it. You want to select "nothing" and then set the object reference to that. I can't help you on JSF but how do you lookup the selected object behind the scenes? If you use entityManager.(Employee.class, 0) then you should get back null if there is no Employee with id=0.
    --Shaun                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to identify tables use currency as foreign keys?

    Currently we have identified some currencies that needed to updated to ISO values and others as obsolete(eg. some EURO countiries set to EUR) in ECC
    So my question is how to efficiently locate those foreign keys tables in ECC?
    Thx and Best Grds
    David

    I do not think I fully understand your question but I will give it a shot.
    If you are trying to locate a foreign key, then proceed as follows.
    1.  Transaction Code: SE11
    2.  Enter the table in question.  For example BSEG
    3.  Select a table. For example BUKRS or KUNNR
    4.  Click on Foreign Keys.  The symbol or icon is a key with an arrow pointing down. It is before Srch Help.
    5. A screen will pop-up with the foreign key fields and tables where you can you the CHECKTABLE.
    Or enter table using SE11: GLPCT
    2.  Select RTCUR
    3.  Click on Foreign Keys
    3.  In the pop-up screen,  double click on TCURC in the check table field.
    Hopes the above provides you with a clue.
    Elias
    Edited by: Elias Akorli on Dec 7, 2009 2:29 PM

  • How to change the Text of Dropdown by Key Dynamically thats coming from RFC

    HI All,
    I need an urgent help . I have a parameter coming from RFC -> YYTEXT with a value Range and I am binding this parameter in my DROPDOWN BY KEY .Now I have a requirement that two values coming from this parameter i need to change it
    .Can you please tell me how to get the values from this YYTEXT attribute which is in NODE1 and then set the two new texts to this attribute so that it shows different text in the droodown .
    Thank you in Advance .
    Naaz

    Will appreciate any comments on the question ?

  • How to change Alt text for the Popup Key LOV Image in Apex  3.2.1.00.10

    we are using Application Express version is 3.2.1.00.10
    There is an icon to click on to popup the lov search box, the alt text for that image is currently "popup Lov"
    would it be possible to change the text to something more meaningfull e.g. "Lookup Person name" or "search Directory for Person names" .
    I have tried by updaing them
    from
    Shared Components>Templates> Popup List of Values Template > Popup Icon Attr --> width="13" height="13" alt="Popup Lov"
    (under Popup List of Values)
    to
    alt="#CURRENT_ITEM_NAME#"
    it didn't work.
    your respone will help getting accessability sign off

    Venu,
    Try adding title = "Lookup Person name" to the Image Attributes of your icon or button.
    Jeff

  • How to change the Purchase requisition when control key is PP02

    Hi Exports:
      I want to change the  purchase requisition which was created after  production order was  released . The  control key is  pp02.
      I  changed the purchase requisition in OPJP .but I got an error .
      the error message is
      " Acct. assign. cat. "Q" for purchase req. Project Stock does not exist. "
      I don't know what should I do . would you like to give me some advice? thank you  very much.

    Hi, Umesh:
         Picture1 is  my OPJP initial screen ,I want to change to be Picture B . but I got an error .
         In fact , I  don't have project stock . but I have sales order stock.
         I try to change 'Q' to other option . but I got the same error.
    picture 1
    picture 2

  • How can i gert a tree of foreign keys

    I have a big db in hands and it's like this Table1.x is referenced by Table2,a_x,Table3,a_x,Table4,a_x, and so on ... then Table2,a_x is referenced by Table5,a_x,Table6,a_x,Table7,a_x and so goes on (i'm not quite sure if i also have a backreferece somewhere)
    So there must be a way to avoid nightmare 'Child record found' or 'Parent not found' -- i only wan to uptate some rows.
    Thanks for help. Florin

    may be this?
    SELECT   c.OWNER,
             c.TABLE_NAME,
             c.CONSTRAINT_NAME,
             cc.COLUMN_NAME,
             r.TABLE_NAME refrence_table,
             rc.COLUMN_NAME refrence_column,
             cc.POSITION
        FROM user_constraints c,
             user_constraints r,
             user_cons_columns cc,
             user_cons_columns rc
       WHERE c.CONSTRAINT_TYPE = 'R'
         AND c.OWNER NOT IN ('SYS', 'SYSTEM')
         AND c.R_OWNER = r.OWNER
         AND c.R_CONSTRAINT_NAME = r.CONSTRAINT_NAME
         AND c.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
         AND c.OWNER = cc.OWNER
         AND r.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
         AND r.OWNER = rc.OWNER
         AND cc.POSITION = rc.POSITION
    ORDER BY c.OWNER,
             c.TABLE_NAME,
             c.CONSTRAINT_NAME,
             cc.POSITION

  • How to get a Value based on Foreign Key item

    Hi,
    I have two tables as below
    TABLE 1
    T1_COL1 NUMBER ---primary_key
    T1_COL2 NUMBER ---foreign_key to TABLE2.T2_COL1
    TABLE 2
    T2_COL1 NUMBER ---primary_key
    T2_COL2 VARCHAR2(40)
    Table 2 is my reference table. I have created a form for table 1, however I want to show user the value of T2_COL2 all the time . My user is not interested in seeing primary key value data.
    Can some please advise what is the best way to do this in APEX.
    Thanks
    Aali
    Edited by: aalishan on 31-Jul-2009 10:40

    Hello:
    One way to do what you want done is to
    1) On the form make the page-item corresponding to T1_COL2 a hidden item
    2) Create a new page-item of type Display Only.
    3) Set the Source Type for this item to be 'Sql Query'
    4) In the Source Value or Expression enter the query select T1_COL2 from table_2 where t2_col1=:pxx_t1_col2Varad

  • How to change dot to comma on numeric pad in Numbers?

    Hi,
    I have bought new iMac with Yosemite system and switched from Excel to Numbers.
    I have Apple's numeric pad keybord. Unfortunately, I dont know how to change the input of the "dot" key on the number pad, so that I get a comma in Numbers sheet, which would allow me to use decimals when working with numbers.
    Cheers
    Mike

    Hi Mike,
    Lucky you with a numeric keypad. I wish I had one .
    Are there any settings in System Preferences > Keyboard?
    Perhaps the Input Sources Tab may help.
    You can also get there from System Preferences > Language & Region > (set your preferences, then click on the Keyboard Preferences button).
    Regards,
    Ian.

  • Foreign keys at the table partition level

    Anyone know how to create and / or disable a foreign key at the table partition level? I am using Oracle 11.1.0.7.0. Any help is greatly appreciated.

    Hmmm. I was under the impression that Oracle usually ignores indices on columns with mostly unique and semi-unique values and prefers to do full-table scans instead on the (questionable) theory that it takes almost as much time to find one or more semi-unique entries in an index with a billion unique values as it does to just scan through three billion fields. Though I tend to classify that design choice in the same category as Microsoft's design decision to start swapping ram out to virtual memory on a PC with a gig of ram and 400 megs of unused physical ram on the twisted theory that it's better to make the user wait while it needlessly thrashes the swapfile NOW than to risk being unable to do it later (apparently, a decision that has its roots in the 4-meg win3.1 era and somehow survived all the way to XP).

  • Foreign Keys on a Table

    Hello,
    On a MySQL Database this Table is given:
    CREATE TABLE Device (
    DeviceID int(11) NOT NULL auto_increment,
    TypeID int(11) default NULL,
    Type ID is a foreign Key that references to another Table which maps the number to a VARCHAR fpr example
    1 --> Printer
    2 --> Screen
    3 --> Notebook
    I want to visualize the MySQL Table in a JTable, but of course I don't want to show the TypeID but the real Name.
    On the other hand.... When the user updates the JTable (i.e deletes printer in a cell and types in Notebook) we don't have to store Notebook, but 3 in MySQL Table "Device".
    So, do you have a simple and small Solution of how to show the reference of the Foreign key, but to update the foreign Key????

    Don't cross-post:
    http://forum.java.sun.com/thread.jsp?forum=48&thread=463134&tstart=0&trange=15

  • SQl*Modeler - foreign key amendment error

    SQl*Modeler 2.0.0 Build 584 (Windows)
    Oracle 10.2.0.4 Solaris
    If I change an existing foreign key to simply change the delete rule (from say CASCADE to RESTRICT) it changes in memory just fine. If I generate the DDL the change has happened and I get the expected DDL.
    However, I then save the model, exit SQL*Modeler, start SQL*Modeler and check the foreign key the change has been lost.
    I can workaround this by changing the name of the foreign key and the change is saved correctly. Feature ?
    Ian

    Hi Ian,
    I logged bug for that.
    You can use foreign key dialog to do that change without need to change the name - just double-click on FK line or presentation in the browser.
    Philip

Maybe you are looking for