Relationships between tables

  • Hi Peeps,

    I have two tables that have a PK and FK relationship.

    Now the idea is that the parent table (with the PK) is first loaded with data.

    When I want to insert into the other table (with the FK), i expect the database server to check to ensure the the new record in the FK table is not an 'orphan'. which implies that if i have (say) a product_id = 5 in the first table and i try to enter data in the second table with FK = 5 , there should be no problem.

    The problem is this, that it is not working that way.

    It keeps giving me an error message that PK-FK relationship ...

    I hope you've got my question.

    Thanks

  • i guess more information is needed...

    nothing gets handled automatically for you as far as PK/FK's go....you might not be doing things in the right order of operation.

    did you add new product 5 in VB, and then add the FK details, also in VB? show us the code that is adding to the db.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I guess i'll take it from another point cos the code is spread around and quite complex.

    Do i have to build a relationship between the tables in the database (i.e. explicitly stating PK and FK relationships) or is it just OK for me to know the kind of relationship I have with my tables?

    Thanks

  • you must create the tables with the PK's and FK's...otherwise what you stated....inserting 5 into the child table without checking that the 5 exists in the parent table could occur...that's what you want.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell,

    I've discovered the problem. The variable i was using to enter ID was being changed in another subroutine and so was conflicting with what i already had in the PK table.

    Thanks for your comments and advise (i was really going to remove the relationship between the tables)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply