Building relationships that have no foreign key

Hi,
I have a situation where the database team have provided me a schema
something like that shown below:
PERSON
|========================================|
| PK | NAME | SSNUM | MGR |
|========================================|
| 1 | john | 123-45-6789 | 3 |
| 2 | fred | 987-65-4321 | 3 |
| 3 | alice | 111-22-3333 | NULL |
| ... |
|========================================|
COMPANY
|=================|
| PK | NAME |
|=================|
| 1 | acme |
| 2 | bcme |
| 3 | ccme |
| |
|=================|
ATTRIBUTE
|========================================|
| PK | SOURCE_ID | SOURCE_TYPE | VALUE |
|========================================|
| 1 | 1 | P | one |
| 2 | 1 | C | two |
| ... |
|========================================|
PERSON and COMPANY are application objects. A PERSON or a COMPANY can
have an ATTRIBUTE tagged to it. Now, there is not a foreign key
relationship from PERSON/COMPANY to ATTRIBUTE. The relationship can be
derived by understanding that ATTRIBUTE.SOURCE_ID is the PK value for
either PERSON or COMPANY, and ATTRIBUTE.SOURCE_TYPE states whether its a
PERSON (P) or COMPANY (C).
The database guys say this is a common approach to modelling data that
is not a "first order" property of a class, which I guess may be up for
debate. In any case....
I can't model this using objects and relationships using KODO. So, my
question is, how would you approach this?
Thanks,
Mike.
PS As an aside, if there was a way in the JDO metadata to "hardcode" a
value for one or more of the columns in a foreign key relationship, it
would help a lot. Then I could build a navigable link from PERSON to
ATTRIBUTE, where the SOURCE_ID data column is PERSON.PK and the
SOURCE_TYPE is the string literal 'P'.

That certainly is a unique problem... and certainly not a sort of model I
would encourage...
however, you should be able to accomplish this in Kodo with a non-direct
relationship... i.e. Attributes has a reference-id not a reference. So
Attribute may say look like this:
private int referenceId = -1;
private char type;
public void getSource ()
Object oid = null;
if (referenceId < 0)
     return null;
switch (type)
case 'P':
     oid = new PersonId (referenceId);
     break;
case 'C':
     oid = new CompanyId (referenceId);
     break:
default: return null;
return JDOHelper.getPeristenceManager (this).getObjectById (oid, true);
public void setSource (Person p)
PersonId pid = (PersonId) JDOHelper.getObjectId (p);
refernceId = pid.id;
type = 'P';
Now this would get a little more complicated for objects which
haven't been made persistent, but can simply be accomplished by using
javax.jdo.InstanceCallbacks and preStore () and using a transient field
Object source. Using non-application identity, would require casting oids to
our ObjectIds.Id (in our JavaDoc). Actually, as I think about it, it may
be better to do this all in a transient state:
public Attribute
     private transient Object src;
     private int referenceId;
     private char type;
     public void setSource (Person p)
          src = p;
     public Object getSource ()
          if (src != null || JDOHelper.getPersistenceManager (this) == null)
               return src;
          /// same as previous example
          return JDOHelper.getPersistenceManager (this).getObjectById (oid);
     public void preStore () // instancecallbacks
          PersonId id = (PersonId) JDOHelkper.getObjectId (src);
          referenceId = id.id;
          type = 'P';
Again this isn't 100% solid code but a suggestion to get you started on
your problem.
While 3.0 will support more complex mappings, it won't get around the
multiple types per field problem... but this could be solved via
interfaces (e.g. public interface AttributeSource), though I'd have to
defer to our 3.0 architect on that one.
On Fri, 14 Feb 2003 16:15:04 +0200, Mike Hogan wrote:
Hi,
I have a situation where the database team have provided me a schema
something like that shown below:
PERSON
|========================================|
| PK | NAME | SSNUM | MGR |
|========================================|
| 1 | john | 123-45-6789 | 3 |
| 2 | fred | 987-65-4321 | 3 |
| 3 | alice | 111-22-3333 | NULL |
| ... |
|========================================|
COMPANY
|=================|
| PK | NAME |
|=================|
| 1 | acme |
| 2 | bcme |
| 3 | ccme |
| |
|=================|
ATTRIBUTE
|========================================|
| PK | SOURCE_ID | SOURCE_TYPE | VALUE |
|========================================|
| 1 | 1 | P | one |
| 2 | 1 | C | two |
| ... |
|========================================|
PERSON and COMPANY are application objects. A PERSON or a COMPANY can
have an ATTRIBUTE tagged to it. Now, there is not a foreign key
relationship from PERSON/COMPANY to ATTRIBUTE. The relationship can be
derived by understanding that ATTRIBUTE.SOURCE_ID is the PK value for
either PERSON or COMPANY, and ATTRIBUTE.SOURCE_TYPE states whether its a
PERSON (P) or COMPANY (C).
The database guys say this is a common approach to modelling data that
is not a "first order" property of a class, which I guess may be up for
debate. In any case....
I can't model this using objects and relationships using KODO. So, my
question is, how would you approach this?
Thanks,
Mike.
PS As an aside, if there was a way in the JDO metadata to "hardcode" a
value for one or more of the columns in a foreign key relationship, it
would help a lot. Then I could build a navigable link from PERSON to
ATTRIBUTE, where the SOURCE_ID data column is PERSON.PK and the
SOURCE_TYPE is the string literal 'P'.--
Stephen Kim
[email protected]
SolarMetric, Inc.
http://www.solarmetric.com

Similar Messages

  • Field MATNR does not have a foreign key; as a result no intervals are allow

    Hello,
    We are having ROH and FERT material , in that we were entered the wrong Item category group , we need to change the
    Item category group through mass posting through MM17. we are getting below given error
    "Field MATNR does not have a foreign key; as a result no intervals are allowed"
    Message no. M&132
    Diagnosis
    You have either specified an interval such as from 1 to 10 or M* for a field, or you have left the field blank, corresponding to the interval *. However, since there is no foreign key for this field, the system cannot resolve the interval into valid single values.
    Procedure
    Enter only single values for this field.
    We have checked the the structure MPGD_MASS in SE11, and here foreign key  mark is tciked in "entry help/ check.
    How to solve this error.
    Regards
    sapman man

    how did you then proceed, on the selection screen:
    did you just use the change tab or did you work as well or only at the create tab?
    What did you enter in the selection?
    (...because I just tried a mass change without specying a material number and do not get such message)

  • I need to have multiple foreign keys on one column, can Designer do this?..

    Basically I've a table that needs to link to three other tables. As far as I can tell Designer will only let me have the foreign keys on seperate columns;
    Column_1 - FK1
    Column_2 - FK2
    Column_3 - FK3
    I think it's a much better database design is to have all three link to one column and have a Type column denoting which table to references
    Column_1 - FK1, FK2, FK3
    Column_2 - Type (FK1, FK2, FK3)

    Can you tell us more about the problem you're trying to solve?
    Thanks, George

  • For all entries from 2 tables that have no common key

    Hello
    I have the next querry that i want to write in abap
    AUFK -
    >aufk-aufnr = afko- aufnr----> AFKO        
    AUFK---->aufk-pspel = afvc-projn -
    >AFVC      
    AFKO-----> afko-rsnum = resb-rsnum   -
    > RESB
    AFVC-----> afvc-aufpl = resb-aufpl       -
    > RESB
    AFVC-----> afvc-vornr = resb-vornr      -
    > RESB
    So that i have to start with AUFK and from there i read AFKO and AFVC  and with the entries from AFKO and AFVC i have to do a select on RESB.
    How can i do this
    select from RESB
    for all entries in AFKO
       where ...
    for all entries in AFVC
       where...  ?
    I do not have any common key between AFVC and AFKO. How can i select the correct entries from RESB?
    I have to mention that i have 1-1 entries for AUFK and AFKO
                                           and 1-N entries for AUFK and AFVC
    Thank you

    Why don't you use afko-aufpl = afvc-aufpl ?
    Regards,
    Raymond

  • Hiding foreign keys

    Is there an option to hide foreign keys in Relational model? I have more then 1000 keys in model and all those lines in diagrams slows program.
    In the part of the model where are no foreign keys the transitions and scrolling are very smooth.
    Thanks!

    The problem is that you may want to include tables (or entities in ER diagrams) that have multiple foreign keys, and you only want to include some of the foreign keys on the diagram.
    Example:
    If the diagram contains tables A, B and C that all have foreign keys between them, you may want to show all foreign keys to and from table B but not the ones between A and C.
    / Marc de Oliveira

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

  • Foreign Key Relationship.

    Hi,
    I asked a question in the link below(which I solved).
    https://social.msdn.microsoft.com/Forums/en-US/514a4f48-b1fa-4fe7-bc2b-4c252b34cf50/insertion-of-null-value?forum=vbgeneral
    Here my question is that:
    We make relation of foreign keys to enforce user for accurate data insertion, but sometime we do need null values to be passed to attribute that is used as
    Foreign key. We studied and know that Foreign keys are meant to except null values, which it doesn't(In simple way. I mean when we select an attribute as foreign key and check it to except nulls, then it should.)
    As I explained in the upper link how I solved the problem, if similar problems have to be solved like that then,
    Why we define Foreign Key over there, i mean it could be done without defining the Foreign key?
    If it is necessary in any meaning then why we Enforce Foreign
    Key Constraint as No,
    to except nulls from user?
    What effects does Enforce Foreign Key Constraint as No
    gives to Foreign Key Constraints?
    Thnx in advance.
    Habib Ur Rehman

    Hello Habib,
    I do not think what you are seeing is normal and without having the exact DDL it will be hard to comment.
    I suspect,  you may had "NOT NULL" constraint on the foreign key column. thats why it is not allowing NULLs.
    By definition, Foreign Key maintains(enforces) referential integrity and does allows NULLs. So,  you should be able to enter nulls without having to disable the foreign key enforcement.  
    by setting
    Enforce Foreign Key Constraint to NO, you are effectively disabling the enforcement. you are invalidating the whole purpose of the constraint. 
    The purpose of having that option(disabling foreign key) is to fasten the data load process bypassing the check(extra work) to make sure the value exists in the primary key table when importing large amounts of data.  But to maintain the referential
    integrity you should have that enabled.
    I am not sure without having the ddl, where you did wrong.PLEASE POST YOUR DDL and it should be clear where it went wrong for you. but i hope this script below should help you understand...
    CREATE TABLE [dbo].[Make]([sid] [int] NULL unique,[sname] [varchar](50) NULL)
    go
    CREATE TABLE [dbo].[Make_FK]([sno] [int] NULL unique,[sname] [varchar](50) NULL,[sid] [int] NULL
    CONSTRAINT FK_MAKE FOREIGN KEY (sid) REFERENCES dbo.make (SID))
    insert into make(sid,sname)
    values(1,'sam')
    insert into dbo.Make_FK(sno,sname,sid)
    values(100,'somename',null)
    select * from Make
    --sid is null. SID is the foreig n key referring to sid in MAKE table.
    select * from Make_FK
    Hope it Helps!!

  • Binding for table produces list for other tables using foreign key and crea

    Using
    software Jdev 11G, WLS 11G, Oracle DB 11G, Windows Vista platform
    technology EJB 3.0, jspx, backing beans, session bean
    I cannot create a namedquery on my secondary table. The method for the column uses the entity object rather than the name and value of the column.
    For instance,
    (Coketruck) table has inventory records(Products) table
    Coketruck has one to many to the Products table
    Products has a many to one to the Coketruck
    I need to return the products from the product table based on the CokeTruck but I cannot create a namedQuery because the method in the Product table is an entity object type instead of a long that I can use to look up all the products based off the column truck_id.
    This is what I was expecting…
    Private Long truckId;
    public Long getTruckId() {
    return truckId;
    public void setTruckId (Long truckId) {
    this. truckId = truckId;
    Instead this is what I have…
    @ManyToOne
    @JoinColumn(name = "TRUCK_ID")
    private Coketruck coketruck;
    this. coketruck = coketruck
    public Coketruck getCoketruck() {
    return coketruck;
    public void set Coketruck (Coketruck coketruck) {
    this. coketruck = coketruck;
    How do I do a query on the Product table to return all the products that are in the coketruck?
    If I do the following it expects for me to pass the Entity Object which I cannot use as search criteria for my find method.
    @NamedQuery(name = "Products.findById", query = "select o from Products o where o.truckId = :truckId")
    On a different note but the same song…
    I noticed that when I look at my Session Bean Data Contols that the coketruck already has a list of the products. I have created a jsp page with a backing bean and have been able to use the namedquery on the coketruck entity to retrieve the productList. Unfortunately I need to sort the products by type and was also not able to find where to perform the work to be able to iterate through the productList to get my desired display. Therefore I started looking at doing another namedquery that would only retrieve the product_type ordering by the truckId.
    Seems I have come full circle… I don’t care what method I have to use to get the info back.
    Any help is greatly appreciated!

    user9005175 wrote:
    Hi!
    I work on an application wich uses a shopping cart stored in a database. The shopping cart uses two tables:
    CART: Holds information common for one shopping cart: the user it is connected to etc.
    - Primary key: CART_ID
    CART_ROW: One row in the cart, e.g. one new product to buy.
    - Primary key: ROW_ID
    - Foreign key: CART_ROW.CART_ID references CART.CART_ID
    From the code the rows in the cart are collected per cart, as is modelled by the foreign key. There exists one more relationship, which we use in the code, but which is not modelled by a foreign key in the database. One row can be dependent on another row, which makes the other row a parent.
    CART_ROW has a column PARENT_ID which references CART_ROW.ROW_ID.
    Should we add a foreign key for PARENT_ID? Or are there any questions to consider when it is a foreign key to the same table?
    I suggest to add foreign key it wont harm the performance (except while on insert when there would be validation for the foreign key). But it would prevent users to insert wrong/corrupt data either through code or directly by loggin in the database.
    A while ago we added indexes, both on ROW_ID and on PARENT_ID. Could the index on PARENT_ID have been harmful, since there is no foreign key?
    Index on parent_id would only be harmful if you do not make use of index after creating it (i.e. there is no query which make use of this index).
    And if you decide to have a foreign key on parent_id then I suggest to have index too on parent_id as it would be helpful atleast when you delete any record in this table.
    Best regards!

  • 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

  • Foreign Key To More Than One Column

    Hi Guys,
    I have a requirement like below
    I have 3 tables (Prospect,customer,user) and I have a contact_dtls table .
    In prospect table Prospect_id is the primary like this customer_id in customer table and user_id in the user table
    in the contact_dtls table I have a column contact_id and other cols.The contact_id can be from prospect_id,customer_id and user_id in other words we can tell the prospect can be a contact or the customer can be a contact and the user also can be a contact also.Now the question is "is it possible to maintain primary key and foreign key relationship with this design that means the prospect_id,customer_id and user_id would be the foreign keys to the contact_dtls table
    I am using oracle11g Version

    Hello,
    as it has been said in the previous reply, you cannot have a foreign key on multiple tables. Maybe you can create a new table contacts containing at least an id column. This table will be parent to prospect, customer and user. Now contact_dtls.customer_id can reference the contacts.id column.
    This will also make sure, that the ids from prospect, customer and user are unique. It even will be possible that a contact is user and customer at the same time.
    Regards
    Marcus
    Edited by: Marwim on 16.11.2012 10:59

  • Can we associate index with foreign key?

    hello
    i have searched and could not find the answer to the above;
    i have a foreign key constraint on the tables; i added an index on that column as well;
    however when i query the all_constraints, under index_name for this foreign constraint there is nothing; only when i have PK/UK i case see indexes associated with them;
    will then oracle still associate my index with the FK constrained column? or i need to excplicity associate it with the foreign key column? if so, how to do that?
    thx
    rgds

    Hi,
    UserMB wrote:
    i have a foreign key constraint on the tables; i added an index on that column as well;It helps if you give a specific example, such as:
    "I have a foreign key constraint, where emp.deptno references dept.deptno. (Deptno is the primary key of dept.) I created an index called emp_deptno_idx on emp.deptno as well."
    however when i query the all_constraints, under index_name for this foreign constraint there is nothing; only when i have PK/UK i case see indexes associated with them;Not all indexes are associated with a constraint. In the example above, you wouldn't expect to see anything about the index emp_demptno_idx in all_constraints or in all_cons_columns.
    will then oracle still associate my index with the FK constrained column? or i need to excplicity associate it with the foreign key column? if so, how to do that?In the situation above, Oracle will still use the index when the optimizer thinks it will help. You don't have to do anything else.

  • Composite foreign key issue

    Hi,
    I am trying to create a composite foriegn key. I have created my primary key as a composite key, made up of three fields.
    Table 1
    PatientID - Made from 3 fields(a,b,c)(primry key)
    Table B
    PatiendID- (should be the foreign key which references table1)
    However i am finding that when i try to create my constraint i get the following messages
    error 02270 - no matching unique or primarty key for this column list.
    below is the sample code i am using to create my foreign key)
    (ALTER TABLE XD_PatientSedation ADD CONSTRAINT fk_Sedation_PatientID FOREIGN KEY (PatientID)REFERENCES XD_Patient_Demographics(PatientID)
    I am really stuck on this and dont quite know what to do.
    Hope you can help.
    Thanks
    Jagdish

    Just speaking about design, when you want to use a foreign key, it already means that the referenced columns represent something important enough to be a primary key of some other table. I don't understand the business logic of your example (using real table and column names would help), but the solution is probably one of these two :
    a) your foreign key should in fact refer to (a,b,c), the complete primary key of the first table (what was suggested by the others).
    b) your foreign key should stay as 'a' (patientid?), you should have a third table (patients?) whose primary key is 'a', and your "Table 1" should have a foreign key on 'a', referencing the third table. In this case you may find it silly to create a new table with only one column, but I would bet that you will very soon think of interesting attributes which could be added to this table.
    In any case, I think that Oracle did a good job by forcing an improvement of your design !

  • How to choose the right foreign key to be used when processing a dimension

    Hi,
    here is the problem I'm facing : I need to create a dimension in my cube from the table dimDocumentVente.
    Here are the relations between my tables in SQLServer.
    dimDocumentVente
        ID
        ID Article
        ID GroupeComptaProduitTVA
    dimArticle
       ID
       ID GroupeComptaProduitTVA
    dimGroupeComptaProduit
       ID
       Code
    with Foreign keys between :
    - IDGroupeComptaProduit  in dimDocumentVente and ID in dimGroupeComptaProduit
    - IDGroupeComptaProduit in dimArticle and ID in dimGroupeComptaProduit
    - IDArticle in dimDocumentVente and ID in dimArticle
    In this dimension (based on dimDocumentVente), I add the information ID from dimGroupeComptaProduit.
    When I process the dimension, I see in SQL Profiler that the relation between the 2 tables is done using dimArticle, which is not what I want to do. I want to use the relation which is between dimGroupeComptaProduit and dimDocumentVente.
    Is there a way to tell SSAS, that I want to use the foreign key between dimDocumentVente and dimGroupeComptaProduit to calculate this attribute ?
    From the user point of view, the information contained in the table dimGroupeComptaProduit contains information linked the the VAT rate. This information is linked to the items in our ERP (Dynamics NAV), and when we create a new order, this information is
    copied in the order line and the user has the possibility to override it. So in the cube, it's interesting to both analyse from the value stored in the item and the value stored in the order. That's the reason why !
    Thanks in advance for your help.

    Thanks for your answer but unfortunately, that's not exactly what I'm looking for.
    I mean, I want to create a dimension based on table dimDocumentVente in which I want to include the information "code" coming from dimGroupeComptaProduitTVA table.
    Please see below.
    As you may notice, I have several foreign keys.
    When I process the dimension, I see in SQL profiler that SQLServer is using the relation between dimDocumentVente and dimArticle plus between dimArticle and dimGroupeComptaProduitTVA to process my dimension. It's necessary for me that instead, it uses directly the
    relation between dimDocumentVente and dimGroupeComptaProduitTVA. Is there a way to change this  ?
    Thanks.

  • Publish: dropping index when it is used for foreign key constraint enforcement

    Hi,
    I'm trying to update a target schema from a reference database via Publish. Among the changes to apply, there's an index that needs to be dropped. Since it's linked to a foreign key constraint, it cannot be deleted unless the foreign key is temporarily dropped
    (I saw somewhere that disabling the foreign key should be enough but it doesn't seem to work either).
    Since there are other changes to be made on the same table, this foreign key also has to be dropped before the script can delete the table and re-create it. This part of the script is correctly generated. The problem is that this part appears after the 'DROP
    INDEX' instruction.
    So when generating the update script, SSDT tries to drop the index BEFORE dropping the foreign key. And I can't drop the foreign key in my custom pre-deployment script, otherwise the update script would fail when trying to delete it again.
    Shouldn't SSDT be smart enough to drop the constraint before the index? Is it a bug or did I forget to set an option? If it's not a bug, what can I do apart from doing it manually?
    Thank you for your help

    Hi Elsa,
    That sounds like a bug. Could you please file a Connect issue for this at
    https://connect.microsoft.com/SQLServer/feedback/CreateFeedback.aspx using the category "Developer Tools (SSDT, BIDS, etc.)"? We're trying to track all bugs through
    Connect so that you can tell when we have fixed the issue and we can request more information.
    A workaround for this issue might be to write a pre-deployment script for your project to drop the foreign key prior to deployment. A pre-deployment script can be added to your project by right-clicking on the
    project in solution explorer and then clicking on Add > Script... and selecting Pre-Deployment Script from the list.
    Thanks!

  • ADF-Master detail-Foreign key generated By Trigger won't propaget to Detail

    Hi All,
    We have Master-Detail Table where Master table key is generated by DB.
    And this key act as Foreign key in detail table.When we Creates Master Table records and commit it .It gets saved correctly But when we navigate to Create Detail record (Cretae button is taken from Detail) It opens in Create mode but does n't poppulate Foreign Key.
    Can anybody plz explain..
    Regards
    Yogesh

    Hi Frank,
    Thanks for your reply.I don't know "the ADF Business Components tester "
    what is this ?
    I was able to solve my problem by making "Refresh after insert " Check box selected in Entity object.Now it is generating Master Key and also i am getting it in Detail.
    But now problem is, Can we make details record in insert mode by 'Create opration' under Master View.My requirment is insert two records in detail table in background while inserting record in master.Where relationship is maintained by foreign key which generated by trigger.
    I want master and detail create form on same page where i will make details from Rendering False i.e both should be in Create mode (Accesser should not retrive record).
    In Short
    ADF-BC Master Detail - Detail Record insert in Back ground on the same page
    Thanks in Adv
    RHY

Maybe you are looking for