One-to-many/foreign keys howto

I have a situation where I'm trying to assemble a web of data and not make
JDO mad.
Here's the structure. I load this data in in batches, with an entry being
placed in a load history table (H) to track each batch. Each batch
contains primary (M) records associated with 0..n qualifying records from
one or both of two auxiliary tables (D, L). All liberally salted with
primary and foreign keys for referential integrity, and most of the keys
are compounds, since the "M" identifiers get recycled for each batch load.
Or, in other words, the "M" key is an ID plus an H key.
Here's a map:
H--0..n-->M
__ +--0..n-->D
__ +--0..n-->L
Just to make things even more fun, the primary key of the H table is an
Oracle 8 "DATE" type, which -- unlike ANSI SQL -- is actually a
"TIMESTAMP". Kodo is rendering this as a java.util.Date rather than
java.sql.Date, which is fine, because I need the full millisecond
resolution. Interestingly, when I altered the schema type to "timestamp",
load operations failed because the "column value was too big" according to
Oracle.
Also adding to the enjoyment, the actual order of presentation of data is
that ALL M records come in, then ALL D records and finally ALL L records.
I create my own H when the load starts.
Now ideally, the whole shooting match will be a single pass/fail
transaction, but that's for later.
Right now, I can create the H, no problem and M's no problem. However,
adding the D's and L's fails come commit time:
kodo.util.FatalUserException: Attempt to set column "OFAC_ADDRESS.LOAD_ID"
to two different values: "7/9/04 9:41 AM", "7/9/04 9:41 AM" This can occur
when you fail to set both sides of a two-sided relation between objects,
or when you map different fields to the same column, but you do not keep
the values of these fields in synch.
The granularity of the timestamps printed isn't making life easier, so I
double-checked the D record's actual local value
ADDRESS SERIALIZER A: Fri Jul 09 09:41:58 EDT 2004/Fri Jul 09 09:41:58 EDT
2004
ADDRESS SERIALIZER B: 1089380518179/1089380518179 (getTime())
ADDRESS SERIALIZER C: -1541174818/-1541174818 (hashCode())
I think I need a clearer understanding of what actions cascade here and
what actions require manual help. For M, I did a setH(H) plus a
setHid(H.getId()) and that made it happy. For D's and L's, of course, I
also have to do a setM(M), where, because of the way the data comes in, I
had to lookup the previously-stored M based on the M.id plus H.id.
Now the parent objects contain sets of child objects. In theory, I should
be adding the newly-created children to those sets, but in practice,
setting the parent on the child appears to cause this to happen
automagically, though I still have to manually set the local value of the
foreign key field.
I really need to find a good FM to RT. Online (and in the Kodo docs), I
find a lot about setting up the XML for one-to-many mapping, but the Java
coding side is taken as obvious. Regrettably, few things are obvious to me.
I suppose I should excerpt the schema just for completeness:
<table name="LOAD_HISTORY">
<pk name="SYS_C0036906" column="LOAD_ID"/>
<column name="LIST_TYPE" type="varchar" size="32"/>
<column name="LOADED_BY" type="varchar" size="32"/>
<column name="LOAD_ID" type="date" not-null="true" size="7"/>
<column name="SUNSET_TIME" type="date" size="7"/>
<fk name="FK_LOADHISTORY_LOADEDBY" delete-action="exception"
to-table="OFAC.OFAC_USERS" column="LOADED_BY"/>
</table>
<table name="OFAC_ADDRESS">
<pk name="SYS_C0036904">
<on column="ADDRESS_ID"/>
<on column="LOAD_ID"/>
</pk>
<column name="ADDRESS" type="varchar" size="125"/>
<column name="ADDRESS_ID" type="decimal" not-null="true"
size="22"/>
<column name="CITY" type="varchar" size="64"/>
<column name="COUNTRY" type="varchar" size="64"/>
<column name="ID" type="decimal" size="22"/>
<column name="LOAD_ID" type="date" not-null="true" size="7"/>
<column name="REMARKS" type="varchar" size="300"/>
<fk name="FK_OFACADDRESS_ID" delete-action="exception"
to-table="OFAC.OFAC_MAIN">
<join column="ID" to-column="ID"/>
<join column="LOAD_ID" to-column="LOAD_ID"/>
</fk>
<fk name="FK_OFACADDRESS_LOADID" delete-action="exception"
to-table="OFAC.LOAD_HISTORY" column="LOAD_ID"/>
</table>
<table name="OFAC_ALIAS">
<pk name="SYS_C0036905">
<on column="ALIAS_ID"/>
<on column="LOAD_ID"/>
</pk>
<column name="ALIAS" type="varchar" size="200"/>
<column name="ALIAS_ID" type="decimal" not-null="true"
size="22"/>
<column name="ID" type="decimal" size="22"/>
<column name="LOAD_ID" type="date" not-null="true" size="7"/>
<column name="REMARKS" type="varchar" size="300"/>
<fk name="FK_OFACALIAS_ID" delete-action="exception"
to-table="OFAC.OFAC_MAIN">
<join column="ID" to-column="ID"/>
<join column="LOAD_ID" to-column="LOAD_ID"/>
</fk>
<fk name="FK_OFACALIAS_LOADID" delete-action="exception"
to-table="OFAC.LOAD_HISTORY" column="LOAD_ID"/>
</table>
<table name="OFAC_MAIN">
<pk name="SYS_C0036903">
<on column="ID"/>
<on column="LOAD_ID"/>
</pk>
<column name="ID" type="decimal" not-null="true" size="22"/>
<column name="LOAD_ID" type="date" not-null="true" size="7"/>
<column name="REMARKS" type="varchar" size="300"/>
<fk name="FK_OFACMAIN_LOADID" delete-action="exception"
to-table="OFAC.LOAD_HISTORY" column="LOAD_ID"/>
</table>

Hi Greg,
I'm just using the unmodified schema-to-mapping output. The one thing that
might be unusual is that I've broken up related tables into discrete
packages, but I believe all the items in question are in this package.
Here it is:
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<package name="com.everbank.finance.ofac3.jdo.sdn">
<class name="LoadHistory">
<jdbc-class-map type="base" table="OFAC.LOAD_HISTORY"/>
<jdbc-version-ind type="state-image"/>
<field name="listType">
<jdbc-field-map type="value" column="LIST_TYPE"/>
</field>
<field name="loadId">
<jdbc-field-map type="value" column="LOAD_ID"/>
</field>
<field name="ofacAddress">
<jdbc-field-map type="one-many"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.OFAC_ADDRESS"/>
</field>
<field name="ofacAlias">
<jdbc-field-map type="one-many"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.OFAC_ALIAS"/>
</field>
<field name="ofacMains">
<jdbc-field-map type="one-many"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.OFAC_MAIN"/>
</field>
<field name="ofacUsers">
<jdbc-field-map type="one-one" column.USER_ID="LOADED_BY"/>
</field>
<field name="runInfoLoadIdents">
<jdbc-field-map type="one-many"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.RUN_INFO_LOAD_ID"/>
</field>
<field name="sunsetTime">
<jdbc-field-map type="value" column="SUNSET_TIME"/>
</field>
</class>
<class name="OfacAddress">
<jdbc-class-map type="base" table="OFAC.OFAC_ADDRESS"/>
<jdbc-version-ind type="state-image"/>
<field name="address">
<jdbc-field-map type="value" column="ADDRESS"/>
</field>
<field name="addressId">
<jdbc-field-map type="value" column="ADDRESS_ID"/>
</field>
<field name="addressMp1">
<jdbc-field-map type="value" column="ADDRESS_MP1"/>
</field>
<field name="addressMp2">
<jdbc-field-map type="value" column="ADDRESS_MP2"/>
</field>
<field name="city">
<jdbc-field-map type="value" column="CITY"/>
</field>
<field name="cityMp1">
<jdbc-field-map type="value" column="CITY_MP1"/>
</field>
<field name="cityMp2">
<jdbc-field-map type="value" column="CITY_MP2"/>
</field>
<field name="country">
<jdbc-field-map type="value" column="COUNTRY"/>
</field>
<field name="countryMp1">
<jdbc-field-map type="value" column="COUNTRY_MP1"/>
</field>
<field name="countryMp2">
<jdbc-field-map type="value" column="COUNTRY_MP2"/>
</field>
<field name="loadHistory">
<jdbc-field-map type="one-one" column.LOAD_ID="LOAD_ID"/>
</field>
<field name="loadId">
<jdbc-field-map type="value" column="LOAD_ID"/>
</field>
<field name="ofacMain">
<jdbc-field-map type="one-one" column.ID="ID"
column.LOAD_ID="LOAD_ID"/>
</field>
<field name="remarks">
<jdbc-field-map type="value" column="REMARKS"/>
</field>
</class>
<class name="OfacAlias">
<jdbc-class-map type="base" table="OFAC.OFAC_ALIAS"/>
<jdbc-version-ind type="state-image"/>
<field name="alias">
<jdbc-field-map type="value" column="ALIAS"/>
</field>
<field name="aliasId">
<jdbc-field-map type="value" column="ALIAS_ID"/>
</field>
<field name="aliasMp1">
<jdbc-field-map type="value" column="ALIAS_MP1"/>
</field>
<field name="aliasMp2">
<jdbc-field-map type="value" column="ALIAS_MP2"/>
</field>
<field name="aliasType">
<jdbc-field-map type="value" column="ALIAS_TYPE"/>
</field>
<field name="loadHistory">
<jdbc-field-map type="one-one" column.LOAD_ID="LOAD_ID"/>
</field>
<field name="loadId">
<jdbc-field-map type="value" column="LOAD_ID"/>
</field>
<field name="ofacMain">
<jdbc-field-map type="one-one" column.ID="ID"
column.LOAD_ID="LOAD_ID"/>
</field>
<field name="remarks">
<jdbc-field-map type="value" column="REMARKS"/>
</field>
</class>
<class name="OfacMain">
<jdbc-class-map type="base" table="OFAC.OFAC_MAIN"/>
<jdbc-version-ind type="state-image"/>
<field name="callSign">
<jdbc-field-map type="value" column="CALL_SIGN"/>
</field>
<field name="callSignMp1">
<jdbc-field-map type="value" column="CALL_SIGN_MP1"/>
</field>
<field name="callSignMp2">
<jdbc-field-map type="value" column="CALL_SIGN_MP2"/>
</field>
<field name="grt">
<jdbc-field-map type="value" column="GRT"/>
</field>
<field name="id">
<jdbc-field-map type="value" column="ID"/>
</field>
<field name="loadHistory">
<jdbc-field-map type="one-one" column.LOAD_ID="LOAD_ID"/>
</field>
<field name="loadId">
<jdbc-field-map type="value" column="LOAD_ID"/>
</field>
<field name="matchInfos">
<jdbc-field-map type="one-many" ref-column.ID="ID"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.MATCH_INFO"/>
</field>
<field name="ofacAddress">
<jdbc-field-map type="one-many" ref-column.ID="ID"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.OFAC_ADDRESS"/>
</field>
<field name="ofacAlias">
<jdbc-field-map type="one-many" ref-column.ID="ID"
ref-column.LOAD_ID="LOAD_ID" table="OFAC.OFAC_ALIAS"/>
</field>
<field name="pName">
<jdbc-field-map type="value" column="P_NAME"/>
</field>
<field name="pNameMp1">
<jdbc-field-map type="value" column="P_NAME_MP1"/>
</field>
<field name="pNameMp2">
<jdbc-field-map type="value" column="P_NAME_MP2"/>
</field>
<field name="program">
<jdbc-field-map type="value" column="PROGRAM"/>
</field>
<field name="remarks">
<jdbc-field-map type="value" column="REMARKS"/>
</field>
<field name="sName">
<jdbc-field-map type="value" column="S_NAME"/>
</field>
<field name="sNameMp1">
<jdbc-field-map type="value" column="S_NAME_MP1"/>
</field>
<field name="sNameMp2">
<jdbc-field-map type="value" column="S_NAME_MP2"/>
</field>
<field name="sdnType">
<jdbc-field-map type="value" column="SDN_TYPE"/>
</field>
<field name="ssn">
<jdbc-field-map type="value" column="SSN"/>
</field>
<field name="title">
<jdbc-field-map type="value" column="TITLE"/>
</field>
<field name="tonnage">
<jdbc-field-map type="value" column="TONNAGE"/>
</field>
<field name="vesselFlag">
<jdbc-field-map type="value" column="VESSEL_FLAG"/>
</field>
<field name="vesselOwner">
<jdbc-field-map type="value" column="VESSEL_OWNER"/>
</field>
<field name="vesselOwnerMp1">
<jdbc-field-map type="value" column="VESSEL_OWNER_MP1"/>
</field>
<field name="vesselOwnerMp2">
<jdbc-field-map type="value" column="VESSEL_OWNER_MP2"/>
</field>
<field name="vesselType">
<jdbc-field-map type="value" column="VESSEL_TYPE"/>
</field>
</class>
</package>
</mapping>
Als, just for giggles, the JDO file
<?xml version="1.0" encoding="UTF-8"?>
<jdo>
<package name="com.everbank.finance.ofac3.jdo.sdn">
<class name="LoadHistory"
objectid-class="com.everbank.finance.ofac3.jdo.LoadHistoryId">
<field name="loadId" primary-key="true"/>
<field name="ofacAddress">
<collection element-type="OfacAddress"/>
<extension vendor-name="kodo" key="inverse-owner"
value="loadHistory"/>
</field>
<field name="ofacAlias">
<collection element-type="OfacAlias"/>
<extension vendor-name="kodo" key="inverse-owner"
value="loadHistory"/>
</field>
<field name="ofacMains">
<collection element-type="OfacMain"/>
<extension vendor-name="kodo" key="inverse-owner"
value="loadHistory"/>
</field>
<field name="runInfoLoadIdents">
<collection
element-type="com.everbank.finance.ofac3.jdo.match.RunInfoLoadIdent"/>
<extension vendor-name="kodo" key="inverse-owner"
value="loadHistory"/>
</field>
</class>
<class name="OfacAddress"
objectid-class="com.everbank.finance.ofac3.jdo.OfacAddressId">
<field name="addressId" primary-key="true"/>
<field name="loadId" primary-key="true"/>
</class>
<class name="OfacAlias"
objectid-class="com.everbank.finance.ofac3.jdo.OfacAliasId">
<field name="aliasId" primary-key="true"/>
<field name="loadId" primary-key="true"/>
</class>
<class name="OfacMain"
objectid-class="com.everbank.finance.ofac3.jdo.OfacMainId">
<field name="id" primary-key="true"/>
<field name="loadId" primary-key="true"/>
<field name="matchInfos">
<collection
element-type="com.everbank.finance.ofac3.jdo.match.MatchInfo"/>
<extension vendor-name="kodo" key="inverse-owner"
value="ofacMain"/>
</field>
<field name="ofacAddress">
<collection element-type="OfacAddress"/>
<extension vendor-name="kodo" key="inverse-owner"
value="ofacMain"/>
</field>
<field name="ofacAlias">
<collection element-type="OfacAlias"/>
<extension vendor-name="kodo" key="inverse-owner"
value="ofacMain"/>
</field>
</class>
</package>
Greg Campbell wrote:
Hi Tim,
Could you send the your mapping information corresponding to the class that
maps to table: OFAC_ADDRESS. In particular, I'm looking for what fields
map to OFAC_ADDRESS.LOAD_ID. This will hopefully give us a hint as to
what's going wrong.
Thanks,
Greg
"Tim Holloway" <[email protected]> wrote in message
news:[email protected]...
I have a situation where I'm trying to assemble a web of data and not make
JDO mad.
<snipped>

Similar Messages

  • Data services and one to many foreign keys?

    Hi,
    I am playing with the data features in Flash4.  I can get a flex app to work that will query and update a J2EE entity that has no foreign keys, but when I try the same with an entity that has many to one foreign keys I get a null pointer error coming from the server.  I created a servlet to do the same update and that worked.
    I'm wondering if the issue is a casting issue.  I know I have to do the configure data types thing to some of the generated service calls to let it know what action script class is appropriate (instead of a generic object ) e.g. findAll().  Do I have to do a similar thing for one to many foreign key situations?  i.e. lets say i have a department - employees model.  On the java side the department entity has a variable that is a set of employees.  In the department.as class I'd have a employees field thats an ArrayCollection.  Do I have to let it know somewhere that this is an ArrayCollection of employee.as objects?
    FYI, I've already changed to eager fetching of the fk values.
    Does anyone have this working?
    Thanks,
    Bill

    Hi,
    Thanks for your feedback!
    This looks like an issue.
    Can you please log a bug in http://bugs.adobe.com/flex with your sample.
    Thanks,
    Balaji
    http://balajisridhar.wordpress.com

  • How to khow how many foreign key are referenceing the primary key

    sir i would like to know that how many foreign key are referencing the primary key of a table
    is any command to know the table name
    please suggest
    thanks in advance..

    In the query below, table_name is the name of the table whose primary key is referenced.
    SELECT b.owner owner
    ,b.constraint_name foreign_key
    ,b.constraint_type constraint_type
    ,b.table_name referencing_table
    ,b.r_constraint_name primary_key
    ,c.table_name referenced_table
    ,c.column_name referenced_column
    FROM all_constraints a
    ,all_constraints b
    ,all_cons_columns c
    WHERE a.owner = b.owner
    AND a.table_name = :table_name
    AND a.constraint_type = 'P'
    AND b.r_constraint_name = a.constraint_name
    AND b.constraint_type = 'R'
    AND c.constraint_name = a.constraint_name
    AND c.owner = a.owner;
    If the name of the primary key is known, the query below
    select * from all_constraints where constraint_type = 'R' and r_constraint_name = :pk_name

  • How to get multiple row data in single row for one value of foreign key

    i want to get data from a table in a way that all the emp_id corresponding to one manager_id
    in employee table come in one row. (emp_id is the primary key and manager_id is the foreign key) Since there are different emp_id whose manager_id is same
    so how can i get all the emp_id in one row .

    One way with 10g:
    select mgr,
           rtrim(xmlagg(xmlelement(empno,empno||',').extract('//text()')),',')  emps
    from emp
    where mgr is not null
    group by mgr;10g:
    -- define this function:
    create or replace
    function concatenate(c Sys_refcursor, sep varchar2 default null) return varchar2
    as
      val varchar2(100);
      return_value varchar2(4000);
    begin
    --  open c;
      loop
      fetch c into val;
      exit when c%notfound;
      if return_value is null then
        return_value:=val;
      else
        return_value:=return_value||sep||val;
      end if;
      end loop;
      return return_value;
    end;
    select mgr,
           concatenate(cursor(select empno from emp e where e.mgr=emp.mgr order by empno),',')
    from emp
    where mgr is not null
    group by mgr;With 11g:
    select mgr,
           listagg(empno,',') within group (order by empno) emps
    from emp
    where mgr is not null
    group by mgr;

  • Foreign key primary relationship

    Can we create foreign key on composite primary key? how?
    Edited by: 854624 on Apr 25, 2011 8:30 PM

    Hi,
    854624 wrote:
    But can we relate one column as foreign key with composite primary key ?Sorry, I don't understand what you're asking. Please post a concrete example, like
    "If I have these tables: CREATE TABLE ...
    and they contain this data: ...
    can I have a foreign key constraint that would allow these statements: INSERT ...
    but not allow these: INSERT ...
    because ...?"
    In the referenced table, the column for a foreign key must be unique. If column_a is unique, then it can be part of a composite primary key, but it doesn't make any sense. if column_a is unique, why isn't column_a, all by itself, the primary key?

  • EMIGALL : problem of foreign key

    Hi everybody
    I m working on a EMIGALL migration and i met one problem with foreign key…
    My field VKONT is defined (in specific table ZR006SAT) like a foreign key with check table FKKVK (Contract Account Header). It means that an entry can be inserted in ZR006SAT , only if VKONT value exists in FKKVK table.
    But, during the migration of my migration object ZR006SAT (same name as the table), I have no error when I put any value for my VKONT field in the input file. EMIGALL does not control the foreign key constraint and I don’t understand why (???).
    However,
    1.     when i try to insert manually (by SE11) an entry in ZR006SAT, SAP forces me to input correct value (existing in FKKVK table) for VKONT field.
    2.     for standards migration objects, there is no problem… foreign key check is working well
    3 days I ve been on this problem.
    Please help me

    Hai
    You can find detailed documentation of EMIGALL in SAP itself. Use Transaction EQ81 to display it. It provides all the concepts and procedures to work with EMIGALL. I will also prepare a document and send it to u later. Meanwhile just for ur info here are some points about EMIGALL :
    1. It Migrates data Business Object wise
    2. It uses Direct Input Technique
    3. It has more than 100 objects of IS-U
    and the steps for implementation goes like this:
    1)You have to create a user specially for migration which will have all the authorizations related to migration workbench, BASIS and IS-U
    2)You have to create your own company in EMIGALL. There is a default company called SAP.
    3)Company SAP contains all the Business Objects
    4)You have to figure out what business objects u need and then u have to copy those business objects to ur company from Standard Company SAP
    5)Each objects contains more than one structure and each structure can contain more than one fields. The relation goes like this
    Object ---> Structure ---> Field
    6)You have to define field rules for each required field of the object. You have to mark "Not required" for fields u don't need
    7)After field rules for a given object is set u have to generate load report i.e. actual Direct Input Program which will migrate data. This program is generated on basis of field rules set by u.
    8)After the load report is generated u have to prepare an input file (import File) for migration. The import file should be according to structure provided by SAP and must be in binary format. SAP Provides the structure of file according to your configurations. You have to write ur own Data conversion program(in any language) for this task.
    9)You take import file as input and migrate the data using generated load program
    10)Finally u can check the Migration Statistics and Error Log
    Regards
    Sreeni

  • ADF Toplink Create Row with foreign key

    Hello,
    I'd like to create a new row in a table that has a required one-to-one mapping relationship (foreign key). I've created an edit form by dragging the return node of a readQuery. The columns holding foreign key values aren't added to the page(because of 1-1 relationship) and when I commit the changes errors are raised.
    How do I pass the foreign key to the ADF Toplink framwork to include in the insert statement.

    TopLink's persistence model is based around relationships, which represent FKs. To have these FKs properly populated you need to create the corresponding object relationships.
    To do this you need to create a data action that will invoke a method on your class passing in the object you want to create the relationship with. There is currently an issue where ADF will not allow you to easily drop a set method onto an action. You will need to create a method on your bean that does not start with 'set'.
    In the demos I have done I have added a method like 'populateAddress(Address)' that internally calls the set method. This populate method will show up on the data control palette and you can then drop it on the action in your page flow.
    One additional tip: To access the object you want to pass in you will need to extend the default expression that reflects the current selected row in the iterator to have '.dataProvider' appended. This accesses the persistent object within row container.
    If you are still struggling please contact me through the email address in my forum profile.
    Doug

  • Foreign key in many to one relationship causes redunency

    Hi
    suppose I have two tables
    A and B
    Table A has a primary key A_PK number
    Table B has a primary key B_PK number
    The relation between two tables is M to One
    if B_PK is foreign key on table A
    then it causes redunency
    A_PK B_PK
    1 1
    2 1
    so what the best way to solve like this problem?
    thanks

    VANPERSIE wrote:
    Hi
    suppose I have two tables
    A and B
    Table A has a primary key A_PK number
    Table B has a primary key B_PK number
    The relation between two tables is M to One
    if B_PK is foreign key on table A
    then it causes redunency
    A_PK B_PK
    1 1
    2 1
    so what the best way to solve like this problem?
    thanksIt's not sure from your description exactly how/where/what columns are involved in the FK relationship. You either have no problem, or your problem is that you have two tables with the same PK, meaning you haven't normalized your design.
    As suggested, post actual table defs, not just a vague description.

  • How many foreign trade orgnisation we can assign to one company code

    Dear Experts,
    How many Foreign Trade Organisation can assign to one company code .
    I would like to understand concept of creating FTO's .
    Thank you
    Krishna

    Hi Krishna,
    Regarding the concept; documents and transactions in GTS are carried out within a single Foreign Trade Organisation.  Together with the Legal Unit (more-or-less corresponding to "Plant"), they provide the key to many of the attributes and characteristics for the transaction, and for reporting purposes.
    For technical convenience, the FTO takes the form of a Business Partner, so has Name & Address attributes and is tied to a single country.  It is reasonable to consider the FTO as representing the Legal Entity, so mapping to Company Code is provided.
    Hope that helps.
    Regards,
    Dave

  • Transfer data from one database to another without identities but keep the relation b/w PK and Foreign key

    Hi,
    I need to transfer data from one database to another database (both are identical databases). 
    1. Not transferring identity columns (primary keys). the destination table might have the same key.
    2. keep the PK's and FK's relation b/w parent and child table
    3. I have 4 levels 
    Example: tableA (col1 int identity(1,1) , col2, col3)
    tableB (col1 int identity(1,1) ,
    col2 , col3) -- col2 has the foreign key relation with tableA.col1
    tableC (col1 int identity(1,1) ,
    col2, col3) -- col2  has the foreign key relation with tableB.col1
    tableD (col1 int identity(1,1) , col2, col3) -- col2  has the foreign key relation with tableC.col1
    please advise me.
    Thanks in advance

    Try the below:
    /********************************SAMPLE TARGET***************************************************************/
    Use MSDNSamples
    create table TableA(LevelValueId int identity(1,1) primary key, name varchar(100))
    Insert into TableA(name) Select 'R1'
    Insert into TableA(name) Select 'R2'
    create Table TableB(ChildId int identity(100,1),name varchar(100), LevelValueID int references TableA(LevelValueId))
    Insert into TableB(name,LevelValueID) Select 'Childname1',1
    /********************************SAMPLE TARGET***************************************************************/
    /********************************SAMPLE SOURCE***************************************************************/
    Use Sample
    create table TableA(LevelValueId int identity(1,1) primary key, name varchar(100))
    Insert into TableA(name) Select 'C1'
    Insert into TableA(name) Select 'C2'
    create Table TableB(ChildId int identity(100,1),name varchar(100), LevelValueID int references TableA(LevelValueId))
    Insert into TableB(name,LevelValueID) Select 'Kidname1',1
    /********************************SAMPLE SOURCE***************************************************************/
    USe MSDNSamples
    /********************************MIGRATION INTERMEDIATE TABLE***************************************************************/
    --Migration table
    Create table Mg_TableA(LevelValueId int, NewValueId int)
    /********************************MIGRATION INTERMEDIATE TABLE***************************************************************/
    /********************************ACTUAL MIGRATION FOR MASTER TABLE***************************************************************/
    MERGE INTO TableA
    USING sample.dbo.TableA AS tv
    ON 1 = 0
    WHEN NOT MATCHED THEN
    INSERT(name) Values(tv.name)
    Output tv.levelValueId ,inserted.LevelValueid INTO
    Mg_TableA;
    /********************************ACTUAL MIGRATION FOR MASTER TABLE***************************************************************/
    /********************************ACTUAL MIGRATION FOR CHILD TABLE***************************************************************/
    Insert into TableB (name,LevelValueID)
    Select A.name,B.NewValueId From sample.dbo.TableB A
    Inner join Mg_TableA B on A.LevelValueID = B.LevelValueId
    /********************************ACTUAL MIGRATION FOR CHILD TABLE***************************************************************/
    /********************************TEST THE VALUES***************************************************************/
    Select * From TableA
    Select * From Mg_TableA
    Select * From TableB
    /********************************TEST THE VALUES***************************************************************/
    Drop table TableB,Tablea,Mg_TableA
    Use Sample
    Drop Table TableB,Tablea

  • Using one user account table across multiple databases but account used as a foreign key

    I want to use one user account table from one database and use it across a couple other databases. The problem is that I want some tables to use the primary key from the user account table as a foreign key to access the data when the user logs in. Is this
    the right way of going about it? Do I have to create a user account table in all my databases? What is the best practice to handle this problem? Thanks in advance.

    You can use Triggers or using replication. 
    more info: 
    Add Foreign Key relationship between two Databases
    SQL Server Replication
    Saeid Hasani [sqldevelop]

  • HT204387 Hello!  Can anyone teach me how to connect iphone 4S to ipad retina using bluetooth. I tried, but the devices won't discover each other at all. I just wanted to connect to share contact details so I don't have to key in one by one.   Many thanks

    Hello!
    Can anyone teach me how to connect iphone 4S to ipad retina using bluetooth. I tried, but the devices won't discover each other at all. I just wanted to connect to share contact details so I don't have to key in one by one.
    Many thanks
    Injin

    You need to go to the App stores and find compatible apps. iOS device do not natively have to ability to transfer info to a computer via BT to a computer.
    With iOS 8 you can use AirDrop

  • Two foreign keys in one single table

    i have a table 'customers' with a 'customerid' as its primary key and another  table 'drinks' with a primary key 'drinkid'.now i have to create a table 'customerdrink' with its primary key as 'customerdrinkid' , and 'customerid' & 'drinkid' as
    its two foreign keys.how can i write the script in sql server to connect the first two tables with the last one ie how can i relate one primary key(customerdrinkid) with two foreign keys(customerid and drinkid) in a single query?pls help me out!!!

    >> I have a table 'customers' ...<< 
    How nice for you, but where is the DDL? Oh, I see you are the Great and Wonderful “shubha2323” who does not have to follow Netiquette or treat anyone with minimal respect when he demands they do his work him! 
    Where is what you already tried? Oh, the Great and Wonderful “shubha2323” does not try anything! His forum slaves should serve him! 
    Do you understand how rude you are when you ignore the basic forum rules? 
    CREATE TABLE Customers
    (customer_id CHAR(16) NOT NULL PRIMARY KEY,
    CREATE TABLE Drinks
    (drink_id CHAR(10) NOT NULL PRIMARY KEY,
    >> with a customer_id as its primary key and another table Drinks with a primary key drink_id. now I have to create a table 'customer_drink' with its primary key as 'customer_drink_id' <<
    You do not know what single quotes mean in ANSI/ISO Standard SQL, or how to follow ISO-11179 Standards for data element names. And this is a stupid idea. You have a key!! Do you know what a key is? 
    CREATE TABLE Drink_Preferences
    (customer_id CHAR(16) NOT NULL 
      REFERENCES Customers(customer_id)
      ON DELETE CASCADE,
     drink_id CHAR(10) NOT NULL 
      REFERENCES Drinks(drink_id)
      ON DELETE CASCADE,
     PRIMARY KEY (customer_id, drink_id),
     etc
    Stop posting until you have manners and you have read a book on RDBMS. 
    --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

  • Interactive alv report......with one table having more than 2 foreign key

    *& Report  ZRAHUL_ALV_SFLIGHT2
    REPORT  zrahul_alv_sflight2 NO STANDARD PAGE HEADING.
    TYPE-POOLS slis.
    DATA: fcat TYPE slis_fieldcat_alv,
          it_fcat TYPE slis_t_fieldcat_alv,
          fcat1 TYPE slis_fieldcat_alv,
          it_fcat1 TYPE slis_t_fieldcat_alv,
          fcat2 TYPE slis_fieldcat_alv,
          it_fcat2 TYPE slis_t_fieldcat_alv,
          lout TYPE slis_layout_alv,
          head TYPE slis_listheader,
          it_head TYPE slis_t_listheader.
    TABLES: SFLIGHT, SCURX, SAPLANE.
    DATA: BEGIN OF it_sflight OCCURS 0,
            carrid TYPE sflight-carrid,      "PK
            connid TYPE sflight-connid,
            fldate TYPE sflight-fldate,
            currency TYPE sflight-currency,        "FK 1
            planetype TYPE sflight-planetype,      "FK 2
          END OF it_sflight.
    DATA: BEGIN OF it_scurx OCCURS 0,
            currkey TYPE scurx-currkey,        "PK 1
            currdec TYPE scurx-currdec,
          END OF it_scurx.
    DATA: BEGIN OF it_saplane OCCURS 0,
            planetype TYPE saplane-planetype,      "PK 2
            seatsmax TYPE saplane-seatsmax,
            tankcap TYPE saplane-tankcap,
            weight TYPE saplane-weight,
          END OF it_saplane.
    SELECTION-SCREEN: BEGIN OF BLOCK blk WITH FRAME TITLE t.
    SELECT-OPTIONS: id FOR it_sflight-carrid,
                    id2 FOR it_sflight-connid.
    SELECTION-SCREEN END OF BLOCK blk.
    INITIALIZATION.
      t = 'enter required criterias'.
      lout-zebra = 'X'.
    START-OF-SELECTION.
      SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE it_sflight
                WHERE carrid IN id AND connid IN id2.
    END-OF-SELECTION.
    ****FCAT F0R IT_SFLIGHT
      fcat-col_pos = 1.
      fcat-tabname = 'IT_SFLIGHT'.
      fcat-fieldname = 'CARRID'.
      fcat-outputlen = 10.
      fcat-seltext_m = 'PLANE ID'.
      APPEND fcat TO it_fcat.
      CLEAR fcat.
      fcat-col_pos = 2.
      fcat-tabname = 'IT_SFLIGHT'.
      fcat-fieldname = 'CONNID'.
      fcat-outputlen = 10.
      fcat-seltext_m = 'CONN ID'.
      APPEND fcat TO it_fcat.
      CLEAR fcat.
      fcat-col_pos = 3.
      fcat-tabname = 'IT_SFLIGHT'.
      fcat-fieldname = 'FLDATE'.
      fcat-outputlen = 10.
      fcat-seltext_m = 'FLDATE'.
      APPEND fcat TO it_fcat.
      CLEAR fcat.
      fcat-col_pos = 4.
      fcat-tabname = 'IT_SFLIGHT'.
      fcat-fieldname = 'CURRENCY'.
      fcat-outputlen = 10.
      fcat-seltext_m = 'CURRENCY'.
      APPEND fcat TO it_fcat.
      CLEAR fcat.
      fcat-col_pos = 5.
      fcat-tabname = 'IT_SFLIGHT'.
      fcat-fieldname = ';PLANETYPE'.
      fcat-outputlen = 10.
      fcat-seltext_m = 'PLANETYPE'.
      APPEND fcat TO it_fcat.
      CLEAR fcat.
    *****GRID DISPLAY
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
       EXPORTING
        I_INTERFACE_CHECK                 = ' '
        I_BYPASSING_BUFFER                = ' '
        I_BUFFER_ACTIVE                   = ' '
        i_callback_program                = 'ZRAHUL_ALV_SFLIGHT2'
        I_CALLBACK_PF_STATUS_SET          = ' '
         i_callback_user_command           = 'CLICK'
         i_callback_top_of_page            = 'HEADER'
        I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
        I_CALLBACK_HTML_END_OF_LIST       = ' '
        I_STRUCTURE_NAME                  =
        I_BACKGROUND_ID                   = ' '
         i_grid_title                      = 'GRID 1'
        I_GRID_SETTINGS                   =
         is_layout                         = lout
         it_fieldcat                       = it_fcat
        IT_EXCLUDING                      =
        IT_SPECIAL_GROUPS                 =
        IT_SORT                           =
        IT_FILTER                         =
        IS_SEL_HIDE                       =
        I_DEFAULT                         = 'X'
        I_SAVE                            = ' '
        IS_VARIANT                        =
        IT_EVENTS                         =
        IT_EVENT_EXIT                     =
        IS_PRINT                          =
        IS_REPREP_ID                      =
        I_SCREEN_START_COLUMN             = 0
        I_SCREEN_START_LINE               = 0
        I_SCREEN_END_COLUMN               = 0
        I_SCREEN_END_LINE                 = 0
        I_HTML_HEIGHT_TOP                 = 0
        I_HTML_HEIGHT_END                 = 0
        IT_ALV_GRAPHICS                   =
        IT_HYPERLINK                      =
        IT_ADD_FIELDCAT                   =
        IT_EXCEPT_QINFO                   =
        IR_SALV_FULLSCREEN_ADAPTER        =
      IMPORTING
        E_EXIT_CAUSED_BY_CALLER           =
        ES_EXIT_CAUSED_BY_USER            =
        TABLES
          t_outtab                          = it_sflight
      EXCEPTIONS
        PROGRAM_ERROR                     = 1
        OTHERS                            = 2
      IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    *&      Form  header
          text
    FORM header.
      CLEAR it_head.
      head-typ = 'H'.
      head-info = 'KINGFISHER'.
      APPEND head TO it_head.
      head-typ = 'S'.
      head-key = 'KEY'.
      head-info = 'AIRLINES'.
      APPEND head TO it_head.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          it_list_commentary       = it_head
         i_logo                   = 'KING_LOGO'
      I_END_OF_LIST_GRID       =
      I_ALV_FORM               =
    ENDFORM.                    "header
    *&      Form  CLICK
          text
         -->OK         text
         -->SEL        text
    FORM click USING ok TYPE sy-ucomm
                     sel TYPE slis_selfield.
      CLEAR it_fcat.
      CASE ok.
        WHEN '&IC1'.
    ********SAPLANE RELATION
          READ TABLE it_sflight INDEX sel-tabindex.
          SELECT * FROM saplane INTO CORRESPONDING FIELDS OF TABLE it_saplane WHERE planetype = it_sflight-planetype.
          fcat-col_pos = 1.
          fcat-tabname = 'IT_SAPLANE'.
          fcat-fieldname = 'PLANETYPE'.
          fcat-outputlen = 10.
          fcat-seltext_m = 'TYPE OF PLANE'.
          APPEND fcat TO it_FCAT.
          CLEAR fcat.
          fcat-col_pos = 2.
          fcat-tabname = 'IT_SAPLANE'.
          fcat-fieldname = 'SEATSMAX'.
          fcat-outputlen = 10.
          fcat-seltext_m = 'MAX SEATS'.
          APPEND fcat TO it_FCAT.
          CLEAR fcat.
          fcat-col_pos = 3.
          fcat-tabname = 'IT_SAPLANE'.
          fcat-fieldname = 'TANKCAP'.
          fcat-outputlen = 10.
          fcat-seltext_m = 'FUEL TANK CAPACITY'.
          APPEND fcat TO it_FCAT.
          CLEAR fcat.
          fcat-col_pos = 4.
          fcat-tabname = 'IT_SAPLANE'.
          fcat-fieldname = 'WEIGHT'.
          fcat-outputlen = 10.
          fcat-seltext_m = 'WEIGHT F PLANE'.
          APPEND fcat TO it_FCAT.
          CLEAR fcat.
          CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
           EXPORTING
      I_INTERFACE_CHECK                 = ' '
      I_BYPASSING_BUFFER                = ' '
      I_BUFFER_ACTIVE                   = ' '
             i_callback_program                = 'ZRAHUL_ALV_SFLIGHT2'
      I_CALLBACK_PF_STATUS_SET          = ' '
      I_CALLBACK_USER_COMMAND           = ' '
             i_callback_top_of_page            = 'HEADER'
      I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
      I_CALLBACK_HTML_END_OF_LIST       = ' '
      I_STRUCTURE_NAME                  =
      I_BACKGROUND_ID                   = ' '
             i_grid_title                      = 'GRID 2'
      I_GRID_SETTINGS                   =
             is_layout                         = LOUT
             it_fieldcat                       = IT_FCAT
      IT_EXCLUDING                      =
      IT_SPECIAL_GROUPS                 =
      IT_SORT                           =
      IT_FILTER                         =
      IS_SEL_HIDE                       =
      I_DEFAULT                         = 'X'
      I_SAVE                            = ' '
      IS_VARIANT                        =
      IT_EVENTS                         =
      IT_EVENT_EXIT                     =
      IS_PRINT                          =
      IS_REPREP_ID                      =
      I_SCREEN_START_COLUMN             = 0
      I_SCREEN_START_LINE               = 0
      I_SCREEN_END_COLUMN               = 0
      I_SCREEN_END_LINE                 = 0
      I_HTML_HEIGHT_TOP                 = 0
      I_HTML_HEIGHT_END                 = 0
      IT_ALV_GRAPHICS                   =
      IT_HYPERLINK                      =
      IT_ADD_FIELDCAT                   =
      IT_EXCEPT_QINFO                   =
      IR_SALV_FULLSCREEN_ADAPTER        =
    IMPORTING
      E_EXIT_CAUSED_BY_CALLER           =
      ES_EXIT_CAUSED_BY_USER            =
            tables
              t_outtab                          = IT_SAPLANE
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2
          IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
    *******SCURX RELATION
                    CLEAR: IT_FCAT2,IT_FCAT.
                    READ TABLE IT_SFLIGHT INDEX SEL-TABINDEX.
                      SELECT * FROM SCURX INTO CORRESPONDING FIELDS OF TABLE IT_SCURX WHERE CURRKEY = IT_SFLIGHT-CURRENCY.
                      FCAT2-COL_POS = 1.
                      FCAT2-TABNAME = 'IT_SCURX'.
                      FCAT2-FIELDNAME = 'CURRKEY'.
                      FCAT2-OUTPUTLEN = 15.
                      FCAT2-SELTEXT_M = 'CURRENCY'.
                      APPEND FCAT2 TO IT_FCAT2.
                      CLEAR FCAT2.
                      FCAT2-COL_POS = 2.
                      FCAT2-TABNAME = 'IT_SCURX'.
                      FCAT2-FIELDNAME = 'CURRDEC'.
                      FCAT2-OUTPUTLEN = 15.
                      FCAT2-SELTEXT_M = 'CURRENCY DEC'.
                      APPEND FCAT2 TO IT_FCAT2.
                      CLEAR FCAT2.
                      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
                       EXPORTING
                        I_INTERFACE_CHECK                 = ' '
                        I_BYPASSING_BUFFER                = ' '
                        I_BUFFER_ACTIVE                   = ' '
                         I_CALLBACK_PROGRAM                = 'ZRAHUL_ALV_SFLIGHT2'
                        I_CALLBACK_PF_STATUS_SET          = ' '
                        I_CALLBACK_USER_COMMAND           = ' '
                         I_CALLBACK_TOP_OF_PAGE            = 'HEADER'
                        I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
                        I_CALLBACK_HTML_END_OF_LIST       = ' '
                        I_STRUCTURE_NAME                  =
                        I_BACKGROUND_ID                   = ' '
                         I_GRID_TITLE                      = 'GRID 3'
                        I_GRID_SETTINGS                   =
                         IS_LAYOUT                         = LOUT
                         IT_FIELDCAT                       = IT_FCAT2
                        IT_EXCLUDING                      =
                        IT_SPECIAL_GROUPS                 =
                        IT_SORT                           =
                        IT_FILTER                         =
                        IS_SEL_HIDE                       =
                        I_DEFAULT                         = 'X'
                        I_SAVE                            = ' '
                        IS_VARIANT                        =
                        IT_EVENTS                         =
                        IT_EVENT_EXIT                     =
                        IS_PRINT                          =
                        IS_REPREP_ID                      =
                        I_SCREEN_START_COLUMN             = 0
                        I_SCREEN_START_LINE               = 0
                        I_SCREEN_END_COLUMN               = 0
                        I_SCREEN_END_LINE                 = 0
                        I_HTML_HEIGHT_TOP                 = 0
                        I_HTML_HEIGHT_END                 = 0
                        IT_ALV_GRAPHICS                   =
                        IT_HYPERLINK                      =
                        IT_ADD_FIELDCAT                   =
                        IT_EXCEPT_QINFO                   =
                        IR_SALV_FULLSCREEN_ADAPTER        =
                      IMPORTING
                        E_EXIT_CAUSED_BY_CALLER           =
                        ES_EXIT_CAUSED_BY_USER            =
                        TABLES
                          t_outtab                          = IT_SCURX
                      EXCEPTIONS
                        PROGRAM_ERROR                     = 1
                        OTHERS                            = 2
                      IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
                      ENDIF.
      ENDCASE.
    ENDFORM.                    "click
    this program runs fine when one navigation is used......but when both the foreign keys come in picture then its shows sequencialy...cant we jump directly to the third one which i require....
    plz guide me.thank u.

    in short i want help on table having multiple foreign keys....and how to navigate in this when working on ALV reports.....
    its easy when using interactive reports by using sy-cucol and sy-curow.....
    reply asap.
    thank u in advance.

  • BC4J models foreign key associations as 1-to-1 instead of 1-to-many

    I've been using JDeveloper to generate a BC4J layer against a schema for several months as the schema has evolved. It has always modeled foreign key associations as 1-to-many, but now it is suddenly modeling foreign keys as 1-to-1, which is nonsense. A lot of other associations are also wrong.
    This happened to a schema that we've been using JDeveloper/BC4J on for months with no such problems as the schema has evolved. The database has changed slightly, but there was no change made to the particular constraints that are now modeled incorrectly.
    If I point JDeveloper to an older version of the same schema it generates the correct 1-to-many relationship. Something in the latest schema must be throwing JDeveloper/BC4J off.
    What do I have our data modelers look for in this case?
    Why would BC4J ever model a foreign key association as 1-to-1?

    I've been using JDeveloper to generate a BC4J layer against a schema for several months as the schema has evolved. It has always modeled foreign key associations as 1-to-many, but now it is suddenly modeling foreign keys as 1-to-1, which is nonsense. A lot of other associations are also wrong.
    This happened to a schema that we've been using JDeveloper/BC4J on for months with no such problems as the schema has evolved. The database has changed slightly, but there was no change made to the particular constraints that are now modeled incorrectly.
    If I point JDeveloper to an older version of the same schema it generates the correct 1-to-many relationship. Something in the latest schema must be throwing JDeveloper/BC4J off.
    What do I have our data modelers look for in this case?
    Why would BC4J ever model a foreign key association as 1-to-1?

Maybe you are looking for

  • One Variable 2 Restricted key figures on 2 different Dimensions

    Hi all, Here is my problem: I have a cube based on SD sales items. I want to calculate 2 retricted key figures on the 0net_value retricted by a date. The first key would be restricted by the installation date, and the second one by the cancellation d

  • VA01 - not updated in SAP - urgent

    When i create the sales order, The order number (XXXXX) was created but it's not present in the VBAK, VBAP tables.

  • Services in the Tux 9.1 to ALSB?

    Can anyone pls gimme a Sample programme where i can call a Service in Tux 9.1 through ALSB n vice versa Pls help me out. Edited by: user11119905 on 10-May-2009 22:23

  • Customise Parser error messages

    Hi, The XML Parser(Xerces) throws its error messages such as "cvc-enumeration-valid....value....". Is it possible to provide a custom message based on the tag or attribute that the xml is currently parsing ? Regards, Syed Niaz

  • Long running processes using a lot of CPU

    Hi all, We have a clustered environment and on both the DB nodes there are processes that is taking a lot of CPU and runs a really long time. The load of the servers get really high and performance degrades drastically. I've traced the processes and