OK, there are two issues here.

One is that you are denormalising your data by adding a surrogate ID. However it is probably too late for you to change this.

One solution would be to use a sequence rather than an auto-increment column. There is a cargo cult that like functions that return the last insert ID - and indeed it may be possible to do this with access - but sequences are your friend. In that approach, you first query the database for the next number from the sequence generator (a very fast query) and then you can insert it. You can forgoe that step by putting the sequence expression directly in the insert query, but then you have the same problem - how do you return it?

It turns out that you are using the table expecting the host column to be the real primary key.

You can guard the table from having duplicate host records by adding a unique index;

CREATE UNIQUE INDEX HOST_IDX ON Apps (Host_ID);

It is possible to further constrain the table by saying that the ID must exist in the other table, this is called a "foreign key".

You sound like you are ready to move onto the next stage with your database exploration. I can highly recommend SQLite if you don't need to continue to use the MS-Access front-end, or PostgreSQL if you still need something you can access via ODBC (I assuming MS-Access can access a real database - however I never tried that).

$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

In reply to Use indexes, sequences by mugwumpjism
in thread SQL statment to insert if none exits and to obtain the primary key of a record with one visit! by blackadder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.