|
-
Jan 19th, 2013, 01:42 PM
#1
Thread Starter
Junior Member
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
-
Jan 19th, 2013, 02:23 PM
#2
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"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 19th, 2013, 04:39 PM
#3
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!
-
Jan 19th, 2013, 05:58 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|