Automatically update foreign key

Hello,
I am new into Application Express and currently developing an application to handle amongst other things databases with instances and users. The databases have an internal 10-digit number (ID) as primary key. This number can be found in the instances and user table as a foreign key. However, it is possible that the databases unique 10-digit ID changes and I wan't to know if it's possible to automatically update the foreign key in the instances and in the user table when the database ID is changed or if it is possible at all to change a primary key?
I have created a report of existing databases with a link on the name of each database directing to another page where you can change every value for the selected database. This works properly and I wan't to enable for the database ID to be changed here as well (and to automatically update all tables where the ID is a foreign key) if it's possible.
Thank you in advance
Fabian

Hi Fabian,
you can also use a trigger to do the Foreign key update on your child tables.
In my example the trigger will only fire if the primary key (JOB_ID) of the JOBS table gets updated. In that case it will update all records in the detail table EMPLOYEES to the new primary key value.
CREATE OR REPLACE TRIGGER TRG_JOBS_PK_UPDATE
AFTER UPDATE
OF JOB_ID
ON JOBS
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
    UPDATE EMPLOYEES
       SET JOB_ID = :NEW.JOB_ID
     WHERE JOB_ID = :OLD.JOB_ID
END;Hope that helps
Patrick
Check out my APEX-blog: http://inside-apex.blogspot.com
Check out the ApexLib Framework: http://apexlib.sourceforge.net

Similar Messages

  • LINQ to SQL - UPDATE Foreign Key

    Hi,
    I'm doing a project with LINQ to SQL. I'm mapping the database in code.
    I have 2 tables: Cargo(CodCargo PK, Cargo)
                            Utilizador(CodUtilizador PK, Utilizador, CodCargo FK. 
    Cargo:
     1 - Client
     2 - Provider
     3 - Administrator
    Utilizador:
     1 - User1 - 2
     2 - User2 - 3
    Code in Class Utilizador:
    private EntityRef<Cargo> _Cargo = new EntityRef<Cargo>();
    [Association(IsForeignKey = true, Storage = "_Cargo", ThisKey = "CodCargo")]
    public Cargo Cargo
    get { return this._Cargo.Entity; }
    set
    Cargo oldCargo = _Cargo.Entity;
    Cargo newCargo = value;
    if (newCargo != oldCargo)
    _Cargo.Entity = null;
    if (oldCargo != null)
    oldCargo.Utilizador.Remove(this);
    _Cargo.Entity = newCargo;
    if (newCargo != null)
    newCargo.Utilizador.Add(this);
    The problem is when I change for example in User1 this: User1.Cargo.Cargo = "Client"; Table "Cargo" change to this:
     1 - Client
     2 - Client
     3 - Administrator
    It shouldn't change, only the CodCargo on Utilizador should change.
    How can I solve this without change Cargo , only updating the CodCargo on Utilizador if the field exists or create a new Cargo if don't.
    Best regards,
    ADAE.

    Hello,
    >>//Is is possible to do this:newUser.Cargo.Cargo = "Client"
    This is not changed the foreign key constraint, it only performs what you see: change the
     2 – Provider to  1 – Client, this is not controlled by Entity Framework, it is a rule of database. It is called foreign key constraint:
    https://technet.microsoft.com/en-us/library/ms175464%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396, since the Entity Framework is an ORM framework, it will follow this rule.
    My suggestion is that you could do what I provide, to set it foreign key property.
    Regards.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • .SubmitChanges does not update foreign key to the database

    My problem occurs in a Windows Phone 8.1 Silverlight VB application. The CatDataContext defines a table Books with items Title and a foreign key _seriesID, with belongs to a table Series.
    Updating of the field Title alone, or the fields Title and _seriesID works fine. However when I only change the _seriesID then no update of the underlying database is done. In this case .GetModifiedMembers shows no modifications.
    A demo project with this problem is available.

    Your demo project url is missing, check it.

  • Update foreign key in target

    Hello,
    My interface has a Sql Server source datastore and an XML target datastore.
    I woud like to create a sequence of values (0,1,2,3...) for the foreign key in the target. This foreign key is missing in the source datastore, hence, it cannot be mapped.
    I'm unsure about the correct way of solving this issue.
    Any help will be much appreciated...

    Ned Epstein wrote:
    It makes sense for the trigger logic to assign the foreign key column trans_request_id in the table lsu_trans_resources_t with the value of the primary key from the parent table.Why?
    Personally I disagree, and think this does not make sense. How can a trigger on the child table know under which parent key the child should be inserted? Magic?
    Of course you can make some assumptions, such as: child rows are always inserted inside the same transaction that a parent row was inserted. In which case you could refer to parent_seq.currval.
    But that, to me, sounds like an assumption you would not want to make.

  • Automatic update of Key Mapping

    Hello MDM Experts,
    We have got a legacy system which is the master source of information. Now if my repository has 10 fields for a material,and if 6 fields are coming from the legacy system then the rest of the 4 fields are coming from R/3 systems.so the data has to align together for a material in SAP MDM.
    In the legacy system for a material material number and RState(revision state) is the key where as for R/3 systems only the Material number is the key.There are also no global id's concept in this case.
    We need to investigate the posibility to delete key mapping values from MDM records automatically. I wasn't able to find such a functionality in the standard Import manager functionality. The scenario:
    In R3 side there is a record that is dynamic and always keeps the latest R State(revision state). So when we import that record into MDM we have material number and R State as a key mapping value (ROJ786501/1;R1A)
    When the record in R/3 changes the state (R State is changed) it comes to mdm as a new record with a new key mapping (ROJ786501/1;R1B) and the record which had a key mapping (ROJ786501/1;R1A) do not exist in the R/3 anymore so we need to delete the old key mapping value from the old record in MDM. Is this possible to do in an automated way?
    Would allocate points for helpful solutions
    Best Regards,
    CM

    Hello Michael,
    The material number and the RState in the legacy system are two different fields and RState is the Lookup field as of now. But the combination of both is the key in the legacy system.
    The issue is in the R/3, like there are some material which do not have the Rstate in the RState field but in the basic
    data text for the material there is the Rstate mentioned.And there are few materials where this Rstate is not mentioned even.And in the R/3 side Material number is the key.But We can customize the Idocs by putting one more segement for RState and populate the RState field by pulling the data from the basic data text and.So when there is no RState for a particular material then it would be a NULL and in PI we can replace it by using a '/' or something else.
    Now suppose i have a material ROK12456/1  and RSTATE as Z1A and have imported it to the MDM. Now if the RSTATE for
    ROK12456/1 changes to Z2A and in R3 always keeps the latest R State(revision state). So when we import that record into MDM we have material number and R State as a key mapping value (ROK12456/1;Z1A) When the record in R/3 changes the state (R State is changed) it comes to mdm as a new record with a new key mapping (ROK12456/1;Z2A) and the record which had a key mapping (ROK12456/1/1;R1A) do not exist in the R/3 anymore so we need to delete the old key mapping value from the old record in MDM. Is this possible to have this type of functionality and if yes then can we do in an automated way?
    Best Regards,
    CM
    Edited by: chinmoy mohanty on Feb 7, 2008 10:50 AM
    Edited by: chinmoy mohanty on Feb 7, 2008 11:18 AM

  • How to create foreign key automatically?

    I am writing to seek help, in regards creating foreign key automatically, when I insert data into my ''price'' table.  I have 2 tables, one called prices and names.  the relationship between them, if that one name can have many prices and one price
    can have many names (many-to-many). Hence, i have junction table called  "Name_Prices", as shown below in the sample database schema:
    Names
    name_id [pk]
    name
    type
    UploadDate
    Prices
    Price_id [pk]
    name_id [fk]
    price
    uploadDate
    Name_Prices
    name_id REFERENCE names (name_id)
    price_id REFERENCE prices (price_id)
    PRIMARY KEY (name_id, price_id)
    The  price's data input comes in as CSV file everyday. (please see the example below) :
    name name_type price UploadDate
    ALBA MBS 93.5 17/10/2014
    ALESC Trup 58 17/10/2014
    ALESC Trup 52 17/10/2014
    My desire goal/output is to be able to create a functionality, where I can insert the price's data into the database (''prices''), it will automatically insert foreign key in the price
    table (from the names table), and if there is a new price's name, then the database will create a new name id for it, in the name's table, transferring the name, its type, from the CSV input data. 
    In order to achieve this task, where would I start implementing this logic?(in SQL server or application-side) what steps does this involve and is this task achievable, all in sql server side (i.e. store procedure, functions etc..). 
    Apology in advance, if the question is not clear to understand, i happy to follow up with further questions, if required.  
    Any help would be very appreciated. Many thanks

    As noted above, I modified the design:
    Products
    Product_id [pk]
    Product
    type
    UploadDate
    Prices
    Price_id [pk]
    price
    uploadDate
    Product_Prices
    Product_id REFERENCE Products (Product_id)
    price_id REFERENCE prices (price_id)
    PRIMARY KEY (Product_id, price_id)
    Note
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • Query Builder - foreign key relationships not showing automatically

    Is it standard functionality in APEX that foreign key to primary key relationships are not shown in Query Builder when choosing related tables? Do you always have to join FKs to PKs manually (i.e. using control click) whenever you create joins in queries? In Oracle forms and SQL Developer the foreign key constaints are always draw in automatically. Any reason why this is not part of APEX's query builder as well? It's such a hassel to have to put them in each time when the relationships could easily be read directly from the data dictionary constraints.
    regards
    Paul P

    bump...

  • Dimension foreign key on Fact gets updated with same value

    Hi All,
    I'm hoping someone has at least seen this problem and can provide insight.
    Background:
    - We are on OWB 10.2
    - Fact table is Billing_F
    - Fact table has foreign keys to Customer_Dim, SalesOrg_Dim, Time_Dim, etc.
    - Fact table has update mode Update/Insert
    Once in a while, every row in the Billing Fact gets updated with the SAME SalesOrg_Dim foreign key column value. In other words, the data would look like:
    Before:
    Line ID Amt SalesOrg_Key
    1 100 19241
    2 200 21925
    3 140 08585
    After:
    Line ID Amt SalesOrg_Key
    1 100 12345
    2 200 12345
    3 140 12345
    *** The Source tables contain values in the Before table. ***
    We can't reproduce this on a consistent basis. The only way to "correct" the data is to rerun the mapping from source -> staging -> fact again. No other change.
    Please, has anyone else at least experienced this problem? If so, how were you able to fix it permanently?
    Any help is greatly appreciated!
    irene

    Hi Irene
    Difficult to help here without reviewing your mappings/schedule but does SalesOrgKey 12345 represent a particular value e.g. Unknown, Default?
    I suspect it's something to do with your load order in that the data in the dimension is not available/ready when the fact is loaded and therefore a default value is being inserted. When the fact load is rerun I'm guessing the load of the dimension has finished and therefore the key lookup returns the proper keys from the dimension. Is the dimension truncated and reloaded?
    Regards
    Si

  • Master-Detail Forms (Populating a Foreign Key Automatically)

    Master-Detail Form
    When inserting a MD Form, the Master ID (e.g. Deptno,the Primary Key) is generated.
    How is it possible for this also to be generated in the Detail Form (as the Foreign Key)? The requirement is for the foreign key to be populated automatically.
    Any help would be much appreciated.
    Thanks.

    This item is discussed earlier in this group.
    It's solved in version 3.0.8
    Regards,
    Anna

  • How to update the foreign key in a 1-to-many relation ?

    I have Product mapped to Channel as 1-1, and Channel mapped to Product as 1-M. Product has a nullable channel_id_fk as a foreign key to Channel, (so one can optionally specify or change the channel of a specific product).
    Starting with a specific product with a null channel_id_fk, I
    1) add this product to a channel (with a vector of products via ValueHolderInterface);
    2) set the channel of this product; and
    3) update the channel in a unit-of-work.
    The product.channel_id_fk doesn't get updated in db.
    I tried setting each side of the relation as read-only, but still it doesn't work.
    What am I missing ?
    (I am using toplink 9.0.4)

    More info:
    1) There is only a single uow involved, so there is no nested transaction.
    2) I tried checking the "target foriegn key" in the 1-1 mapping from Product to Channel, but this resulted in a exception thrown during runtime when the channel is accessed from the product:
    Local Exception Stack:
    Exception [TOPLINK-6094] (OracleAS TopLink - 10g (9.0.4) (Build 031126)): oracle.toplink.exceptions.QueryException
    Exception Description: The parameter name [AFFINITY_CHANNEL_ID_FK] in the query's selection criteria does not match any parameter name defined in the query.
    Query: ReadObjectQuery(au.com.agl.application.scorecard.AffinityChannel)
         at oracle.toplink.exceptions.QueryException.parameterNameMismatch(QueryException.java:658)
         at oracle.toplink.internal.expressions.ParameterExpression.getValue(ParameterExpression.java:149)
         at oracle.toplink.queryframework.SQLCall.translate(SQLCall.java:334)
         at oracle.toplink.internal.queryframework.CallQueryMechanism.executeCall(CallQueryMechanism.java:129)
         at oracle.toplink.internal.queryframework.CallQueryMechanism.executeCall(CallQueryMechanism.java:111)
         at oracle.toplink.internal.queryframework.CallQueryMechanism.selectOneRow(CallQueryMechanism.java:584)
         at oracle.toplink.internal.queryframework.ExpressionQueryMechanism.selectOneRowFromTable(ExpressionQueryMechanism.java:792)
         at oracle.toplink.internal.queryframework.ExpressionQueryMechanism.selectOneRow(ExpressionQueryMechanism.java:763)
         at oracle.toplink.queryframework.ReadObjectQuery.execute(ReadObjectQuery.java:340)
         at oracle.toplink.queryframework.DatabaseQuery.execute(DatabaseQuery.java:498)
         at oracle.toplink.queryframework.ReadQuery.execute(ReadQuery.java:111)
         at oracle.toplink.publicinterface.Session.internalExecuteQuery(Session.java:1968)
         at oracle.toplink.threetier.ServerSession.internalExecuteQuery(ServerSession.java:629)
         at oracle.toplink.publicinterface.Session.executeQuery(Session.java:1096)
         at oracle.toplink.internal.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:52)
         at oracle.toplink.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:63)
         at au.com.agl.application.scorecard.ProductDetails.getExclusiveAffinityChannel(ProductDetails.java:69)
    ....

  • Populate the foreign key automatically

    HI All!
    I have a form in which there are two data blocks One is emp_master and other is salary_details.I have a foreign key salary_code in Emp_master and similarly Salary_code as primary key in salary details.I have used two list item in place of Emp_id and Salary_code.I want that when I select the emp_id as 10 from the list automatically salary_code list item should get populated by 1 item in the list item.Similary the rest should follow .Please someone look into this matter it would be a gr8t help to me.
    Thanks

    Abha,
    I would not use list items for this. Instead, use a multi-record text box with a scroll bar. It looks and behaves like a t-list, but is much easier to code and manage.
    Use the datablock wizard to create the master-detail relationship. When you use the wizard, all the code will be generated for you. Then use the layout wizard, and display multiple records with scroll bars.
    BTW, it is customary to have the primary key in the master, and the foreign key in the details. In your case it is backwards.
    Also see
    http://www.oracle.com/technology/obe/obe_as_10g/bi/forms/formsmasterdetailobe.htm

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

  • Update query with multiple foreign key parameters

    I am trying to perform an update query on one field that uses multiple foreign key parameters from one table to update the other table. The data in all three foreign key parameter fields are not constant. Accuracy is absolutely critical. I tied this as well as other various scenarios with no success. Can anyone help?
    Update A_table a
    set a.rate = (
    select b.rate
    from B_table b
    where b.id = a.id
    and b.transdate = a.transdate
    and b.orgnum = a.orgnum
    and b.transdate = to_date('31/12/2007', 'dd/mm/yyyy')
    )

    I would check symbols by a user name and number of posts before calling anyone a hot shot, especially damorgon.<br><br>
    Yes version matters (due to bugs and features in each version).<br>
    DDL matters because of indexes and associations.<br>
    Data matters because it makes a difference on how SQL can be written.<br>
    Reasons why an SQL statement don't work is because we aren't looking over your shoulder at your screen.<br><br>
    damorgon did leave off his list that the best place for this question is on the PL/SQL, where they will ask you similar questions in addition to your explain plan and data volumes.

  • Foreign Keys for a Logical Table

    Hi All,
    I want to know in which scenarios we create the Foreign Keys for a Logical Table. Once we create the foreign key for the logical table, does it mean that it will automatically override if any joins are there.
    Physical Table A, B ( no physical joins)
    Logical Table A, B (logical join B(Dim)-->A(Fact))
    created the foreign key in logical table A with B .(A.1=B.1)
    Will the rpd generate sql with A.1=B.1 join condition for reports.
    I tried to create the logical foreign key for logical table A, I could not see any corresponding table option.Here is its snapshot
    http://tinypic.com/r/jq1gkz/6
    Thanks,
    Virat

    In general when we go complex joins in Physical layer we go for Logical foreign key joins in BMM layer, best examples is SCD Type-II.
    Physical Table A, B ( no physical joins)
    -->You need to have a physical join so that this can be override by BMM layer
    created the foreign key in logical table A with B .(A.1=B.1) Will the rpd generate sql with A.1=B.1 join condition for reports.
    -->Yes you can see this join in BI Physical query.
    I tried to create the logical foreign key for logical table A, I could not see any corresponding table option.Here is its snapshot
    -->2 cases: 1)You need to delete existing logical joins 2) You might not have join in physical layer.
    Hope this helps
    Let me know for issues
    Edited by: Srini VEERAVALLI on Jan 18, 2013 1:29 PM
    BTW: You got very good name, why dont you update in your profile to see your name instead of some number
    Edited by: Srini VEERAVALLI on Jan 18, 2013 1:29 PM

  • JDeveloper 9i, Foreign Keys and LOVs.

    Greetings all,
    I'm using Jdev 9i to try and create some maintenance applications quickly. These applications are pretty standard; insert, update, delete, some validation, including foreign keys.
    The problem is, I can't figure out how to get JDev to generate the JSPs with LOV pages on the foreign keys.
    I've tried setting the validation properties of the entity object to validate from a query, as well as, tried using View objects for the same validation. Neither get me results.
    I've also tried embedding the LOV directly using the LOVInputSelect (or whatever it is called). That works partially by displaying a field and a button, but when I click on it, I get an error with no explanation. Very helpful.
    I can use a InputSelect datatag, but it is not the best choice for foreign keys with hundreds of values.
    Also, since the Wizards are generating the DataEdit tag for the JSP I'm using, it appears that I cannot override them to display the LOV. They display a Calendar page for dates, automatically, but won't display an LOV for referential integrity. At least, I can't figure out how to make them.
    Thanks in advance.
    Ed Dana
    Software Developer.

    It's really beyond me why Oracle haven't provided a fairly complete set of Renderers (and a bit more BC4J oriented than the one's mentioned in the HOWTO).
    Basically, you have two choices:
    1/ develop your own Renderers (make your own conventions with Properties on Entities/ViewObjects) and use InputRender
    2/ generate the JSP's yourself using the usual jbo tags (probably again based on some meta data in the properties). You may have to fiddle around a bit with inputselectlov etc.
    Again, it's hard for me to understand why Oracle haven't substantially improved their default generated BC4J JSP pages, it looks to me that it is not so hard (compared to some of the other things they're trying to pull off) and gives maximum benefit for the customers.
    Alternatively, there is JHeadstart (do a search on otn, you'll find some pointers). It generates lots of screens nicely for you (a la Designer, if you're familiar with that). Some teething problems, but really very productive otherwise. The disadvantage here is that if you want to start to make a few little changes outside what it supports, you're up against a fairly steep learning curve to master UIX and MVC framework. Apparently there's some hope that they will generate to Struts (taglibs & controller, if I understand well) soon.

Maybe you are looking for

  • Portal failed to access remote resource due to network failures

    Hi, We have a portlet that allows users to upload files to a SQL Server database and make it available for other users to access. The portlet code is on our remote servers. Everything works fine in dev environment, but certain files fail in pre-prod

  • I deleted all my events in iPhoto

    Hi some how i have managed to delete all my events in iphoto i do not have time machne set up yet and would really like to get them all back can anyone offer any help, do Mac have a system restore point like windows, am still getting use to the Mac o

  • Can I run two PIDs from the same vi?

    Hi I want to control speed of the rotating shaft and force applied by the pneumatic cylinder using two different PIDs from the same program. I managed to get two PIDs to work separatly using PID control loop VI`s supplied with labview 6.1 (I got Kc,

  • DVD Software for mac

    I know adobe has software for PC, but I have a mac....Have adobe release any software for the mac lately? Is is too expensive? thanks

  • I cant install Itunes 10.7

    I updated my iphone to ios6, and after when i tried to sync it to my computer, it said that i needed to upgrade to itunes 10.7 for me to sync my iphone. I download the itunes setup, but everytime i try to install it, i keep getting a network error sa