Results 1 to 4 of 4

Thread: Help: Auto increment Alphanumeric code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    20

    Help: Auto increment Alphanumeric code

    Good day guys!
    I am doing a small function in my project, I have a Transactions Table in my database...the Table include: StudentID TransactionNumber Description Date
    I already know that there is an auto-increment function using mysql db. By the way may primary key column is the StudentID, but I want to make an auto increment Alphanumeric function for my TransactionNumber like, OR #-0001 then when someone makes a transaction it will automatically get the current value in the database, and increment it by one which will become OR #-0002, etc.

    How could I do this while auto increment is not a primary key column? anyhelp is really appreciated

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Help: Auto increment Alphanumeric code

    you could query your db first to get the highest TransactionNumber, then parse the string + extract the number to get your next number.

    you should note, the way you have it at the moment will be interpreted as a negative number:

    OR #-0001

    will be selected as being greater than:

    OR #-0010

    to select the greatest minus number:

    Code:
    "SELECT TOP 1 TransactionNumber FROM Transactions ORDER BY LEN(TransactionNumber) ASC, TransactionNumber ASC"
    to select the smallest minus number:

    Code:
    "SELECT TOP 1 TransactionNumber FROM Transactions ORDER BY LEN(TransactionNumber) DESC, TransactionNumber DESC"

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help: Auto increment Alphanumeric code

    I'm confused. Presumably it is possible for one student to be involved in more than one transaction so how can Student ID be your primary key?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Help: Auto increment Alphanumeric code

    Why go to all the trouble? Does the DB care whether the ID number is 1, 01, 0001, or #-0001? No. Only you really care about that. So how about just leaving the autoincrement alone as an integer and formatting it for display? The only reason to go through the contortions to use some formatted string as a primary key would be if you already have that key as a foreign key in a different table, and it sounds like that's not the case.
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width