Database
  Home arrow Database arrow Page 4 - Portable Database Functions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Portable Database Functions
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 40
    2003-12-29

    Table of Contents:
  • Portable Database Functions
  • Tool #1: Quick Execution
  • Tool #2: How Fast Can You Count?
  • Tool#3: I'll Use My Own ID, Thank You
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Portable Database Functions - Tool#3: I'll Use My Own ID, Thank You


    (Page 4 of 5 )

    Necessity was very much the mother is this little invention. My conundrum was this: the application I was designing wrote the same data to two databases; one on our intranet and the other on the World Wide Web. I was using auto-increment to set my unique identifier for each record. But what if one of the writes failed to either of the databases? Would that not mess up the index of IDs? Would the subsequently written records be inconsistently identified?

     

    I had neither the time nor the desire to reference the records by anything other than their unique identifiers (that’s what they’re there for after all!). So I decided to forego the whole auto-increment feature, and write my own IDs. So now I just had to work out some efficient way of getting/setting the highest ‘id’ within a table. Here’s the function I came up with:

     

    '===============================

    Function maxID( strTable, pk )

    '===============================

       Dim intMax

       If pk = "" Then pk = "id"

          

       strSQL = "SELECT MAX(" & pk & ") FROM " & strTable

       intMax = getCount( "", "", strSQL )

       If IsNull( intMax ) Then

              intMax = 0

       Else

              intMax = cInt( intMax )

       End If

       maxID = intMax

          

    End Function

     

    Explanation

    All this little function accomplishes is a retrieval of the highest primary key within a table. The primary key (pk) defaults to ‘id’, the column name used by probably 99% of all DBA’s.  You’ll notice that I hotwired the getCount() function to retrieve the MAX id for us, which it does quite happily. Maybe you’re now thinking that I should change the name of getCount() to reflect the fact that it can get other things besides just counts. Well, I suppose the only reason that I haven’t, is because getCount() is easier to remember and faster to type then getCountOrPossiblySomeOtherRequiredValue().

     

    Anyhow, we can easily use this maxID() to set our unique identifiers like this:

     

    Dim intUser

    intUser = maxID( “users”, “” ) + 1

     

    ‘=== first enter the user, assuming we’ve checked

    ‘=== to make sure they don’t already exist

    strSQL = “INSERT INTO users ( id, email, name )

    VALUES ( “ & _ intUser & “, ‘me@here.com’, ‘Me ’ )”

     

    Call commitToDB( strSQL, “” )

     

    ‘=== now subscribe to the newsletter

    strSQL = “INSERT INTO subscribed_to

    (user_id, newsletter_id )

    VALUES ( “ & _ intUser & “, 3 )”

     

    Call commitToDB( strSQL, “” )

     

    There we have it, we can our own unique ID, and easily keep it consistent across countless tables and databases. I would also recommend the use of foreign key constraints to ensure referential integrity, but that’s beyond this article.

     

    It was interesting to discover after creating this technique that I had inadvertently complied with a ‘best-practice’ of the DBA world. It turns out that many people are highly critical of using auto-increment, no matter what the application/database requirements. In fact, you could read up on this raging debate if you like.

     

    The other reason I highly recommend this method, is that down the road you may wish to upgrade/switch your database engine. It will be much easier to do when we don’t have to worry about the new system using auto-increment in exactly the same manner. That’s because we never used it, our SQL statements never depended on it, for all we care the database could suffer severe frostbite and have to undergo amputation of it’s auto-increment appendage. We simply refuse to have our great applications brought to their knees by a simple ID.

     

    More Database Articles
    More By Justin Cook


     

    DATABASE ARTICLES

    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005
    - Manipulating ADO Recordsets





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway