Professional Web Applications Themes

Generating Unique UserID - Coldfusion - Getting Started

I've been trying for some time now to get ColdFusion to make a unique userID everytime a user is added to the database. First I tried getting the maximum number of users in the users table by using the RecordCount function, and then setting the new userID to increment by one after that. But then I ran into a problem when I deleted a user as it created duplicate values the next time someone was added. Then I tried using the MAX function and I came up with this: <cfquery name="Users" datasource="#datasource#"> SELECT MAX(userID) AS LastUser FROM users </cfquery> <cfset ...

  1. #1

    Default Generating Unique UserID

    I've been trying for some time now to get ColdFusion to make a unique userID
    everytime a user is added to the database.
    First I tried getting the maximum number of users in the users table by using
    the RecordCount function, and then setting the new userID to increment by one
    after that. But then I ran into a problem when I deleted a user as it created
    duplicate values the next time someone was added.
    Then I tried using the MAX function and I came up with this:

    <cfquery name="Users" datasource="#datasource#">
    SELECT MAX(userID) AS LastUser
    FROM users
    </cfquery>


    <cfset userID = Users.LastUser +1>
    INSERT INTO Users
    (#userID#, #username#, etc.)

    This solved the duplication problem but the IDs just kept getting bigger and
    bigger. Anyway, I was hoping there was a way to generate a unique userID
    everytime a new user was added to the database, but if a user was removed, then
    the rest of the IDs would decrement in value. Or maybe there is a way of
    telling ColdFusion to generate a random string as the userID everytime a new
    user was added.
    I would really appreciate help with this.

    Robbie101 Guest

  2. #2

    Default Re: Generating Unique UserID

    If you can, get your database to do it instead of Cold Fusion. Various
    databases have various numbers.

    Also, don't worry about how big the number is, it simply doesn't matter.
    Also, don't worry about what happens to an id if the record is deleted. It
    doesn't matter either.

    Don't use strings unless you are using the person's email address or something
    like that.

    Dan Guest

  3. #3

    Default Re: Generating Unique UserID

    If you have ever tried using the RecordCount function to add a new user ID,
    then eventualy you WILL get a duplicate value after entering another value into
    the database. Also, not all databases have an autonumber function. Besides the
    autonumber just finds the largest number in the recordset and adds 1 to it. If
    you had 5 users in your database, with userIDs 1-5, and you deleted userID
    number 1, then you have a gap, as the autonumber will add '6' as the next ID,
    not 1. So there must be another way of doing it.

    Robbie101 Guest

  4. #4

    Default Re: Generating Unique UserID

    > you had 5 users in your database, with userIDs 1-5, and you deleted userID 

    The previous respondent's point was that it DOESN'T MATTER that there are
    "gaps". And they're quite right. A record's key should stick with the
    record for its lifetime, and be "retired" with the record (ie: when the
    record is deleted). You should NOT be attempting to re-arrange the IDs
    when record deletions are made, simply so they are continuous.

    So if you want to use integers, use an autonumber or your DB's equivalent
    thereof. If you don't want an integer, you could use a GUID, in which case
    it's still perhaps best done on the DB server, rather than in coding logic.

    --
    Adam
    Adam Guest

Similar Threads

  1. Unique Form inserting into many tables using unique id
    By Gabo Navarro in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 0
    Last Post: September 16th, 06:25 PM
  2. UserId
    By tank180 in forum Dreamweaver AppDev
    Replies: 3
    Last Post: March 24th, 08:07 PM
  3. Generating unique values for TEXT columns
    By Frank D. Engel, Jr. in forum PostgreSQL / PGSQL
    Replies: 6
    Last Post: January 3rd, 06:39 PM
  4. TELNET and USERId's
    By needhelper in forum PERL Modules
    Replies: 3
    Last Post: September 4th, 04:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139