Force auto increment to follow another suequence - mysql

I have a table containing invoices. Those invoices are inserted via a VB.NET program I have written. Using auto increment my invoices get their invoiceid.
I now have invoiceid's like
1055
1056
1057
...
I also have another database (from another bought software program) that generate invoices, and I use a cron job to import certain invoices into my main table, but they get the prefix 99. Those invoices do not have the same sequence. I do not use auto increment to number them, but I use the original ID (from the other database) + the 99 prefix. In my main table I have invoiceid's from the secondary database like this:
992013055
992013064
992013078
So, this makes that my main table looks like this:
992013055
1055
1056
992013064
1057
992013078
...
Now the thing is, I want the auto increment only follow the 'main' sequence. So in the example above, the next auto increment value should be 1058, but using normal auto increment it would be 992013079.
Is there a way to force this? Or should I "reset" the auto increment value after the cron job, something like "ALTER TABLE invoices AUTO_INCREMENT=bla +1". I guess 'bla' should be a new select to get the highest value in the column that does not start with 99, not sure how to do this. Off course when I get to invoice 9900, this would be a problem.
Thank you

i would create another column or table that links in this external id and do not try to layer them in to your id column like you are attempting.
for instance add a column called external_id and simply populate that one with the 99 values - and keep your autoincrement value chugging along properly with your regular numbers.

No you can't. Even if you tried the ALTER TABLE invoices AUTO_INCREMENT=bla +1 command, it would not work, as MySQL makes sure the value of AUTO_INCREMENT is higher than the highest value in the column.
Basically you shouldn't use the value in your autoincremented column for anything else than primary key, so that you don't really need to care about what these values are and what sequence they follow. After all nobody sees them and they're only used by computer.
If however you used these values for something that's visible for the end user... well your users will just need to adjust to the fact, that their invoces start from 99 now. Either that, or you need to renumber them, which is hell of a work and likely to introduce errors to your data.

Related

How to create SQL table with limited entries?

I have a table that is used to store the latest actions the user did (like a ctrl+z for the program), but I want to limit this table to about 200 entries, and after that, every new entry would delete the oldest in the table.
Is there any option to make the table behave this way on SQL or do I need to add some code to the program to do it?
I've seen this kind of idea before, but I've rarely seen a case where it was a good idea.
Your table would need these columns in addition to columns for the normal data.
A column of type integer to hold the row number.
A column of type timestamp (standard SQL timestamp) to hold the time of the last update.
The normal approach to limit this table to 200 rows would be to add a check constraint to the column of row numbers. For example, CHECK (row_num between 1 and 200). MySQL doesn't enforce check constraints, so instead you'll need to use a foreign key reference to a table of row numbers (1 to 200).
All insert statements will need to determine whether the table is full, examine the time of the last update, and either a) insert a new row with a new row number, or b) delete the oldest row or overwrite it.
My advice? Renegotiate this requirement.
Assuming that "200" is not a hard limit, in other words if the number of entries occasionally went over that by a small amount it would be OK...
Don't do the pruning on line, do it as an off line process, run as often as needed to keep the totals per user from not getting "too high".
For example, one such solution would be to fire the SQL that does that query every hour using crontab.

Id field (primary key) is skipping numbers of rows that have been deleted, how to change?

I'm using MS Access and have created a simple table. I have one column as the standard ID primary key (renamed to Number). I deleted a selection of rows, but now when I go to the next row, the Number column counts from the deleted numbers.
E.g. it looks like:
Number Name
1 etc
2 etc
3 etc
6 etc
7 etc
8 etc
Where rows 4 & 5 have been deleted.
I removed all the rows that came after the problem (i.e. 6,7,8 in this case) but then it starts from 9.
Is there any way I can start the count back at 4 (as I have rows 1,2,3 left)?
That is what an autonumber is supposed to so. If you need a counter that means something, you should not use an autonumber.
Access is a relational database, if you could delete a row and then add a new row with the same number, you would throw the relationships out of kilter.
If you need a sequential number see Access VBA: Find max number in column and add 1
If this is a once-off problem, you can delete the current autonumber field from the table and save, then add the autonumber again, but it would be much better to forget about a sequential autonumber. Autonumber should never be shown to the user. It can never be relied upon to be anything but unique, and if you mess about enough, not even that.
The real problem is that Access is pretty stupid. E.g. if a new table is created and data is entered for the first time but the row is incomplete, the number will still skip when you go back to the table and reenter the data. Even if it was a blank database and there would be no conflicts/error in relationships by doing so.
This is particularly frustrating as an instructor because some other behaviours of Access make this very likely to happen.

Fixing auto-increment ID in MySQL table

Hey I was wondering how I could fix my auto increment ID... it seems that everytime I add a row to the table, the ID is 1 off. So here's an example.
My table currently looks like this:
Now, as you can see, the last element jumps from 198 to 200. I want this one to be 199.
So say I manually change that last value(200) to 199, it will obviously work, but the next time my script adds a row to the table, it will be on off again(201). Any idea how I could fix it? Thanks for any help, sh042067.
Jim's right that you should find out the root cause of the 'missing' id.
But if you really just want to re-set the auto-increment counter, you can do this:
ALTER TABLE my_table AUTO_INCREMENT=200;
That should make the next auto-increment id value 200. (Assuming it's MySQL)
This probably happened because you removed a row from your table and then added a new row afterward. Is there a reason that you need syncrosy for this table in your id column? Are you planning on removing, replacing, and/or adding new rows. If so, this column will not remain in sync and you'll find that whatever steps you take to fix this problem (could be as easy as dropping the column and recreating it) will need to be done as often as you are removing and adding data.
In short, we need more information about what you're trying to accomplish.

MySQL - Migrating some ID numbers over from randomly generated to autoincremental

I am in the process of rewriting a company's entire system. The original developer was a bit silly and generated ID numbers for each customer report randomly in his database. Each ID number is up to 7 digits long - but could be anything.
I am migrating over all his old data to our new, far more logically structured database. I obviously want to use a MySQL auto-increment for our ID field. However, it's vital that we keep the old ID numbers as customers still phone up each day with those to reference against.
Ideally, the perfect scenario would be we go live December 1st - everything up to December 1st is all randomly IDed, and from December 1st onwards they automatically increment starting at the highest random ID in the old database.
Is such a thing possible with MySQL without any issues? I am currently using two columns - one, our logical autoincrementing ID, and a second column called old_id which was being used during migration. But we need the call centre staff to only be using one ID or mass confusion will ensue.
Thanks!
If you start numbering from the highest random value, just changing the field to autoincrement should be enough, the normal behaviour is that mysql won't change ids already set, and starts numbering from the highest value+1.
If you want to start from a specific value (say 10,000,000) you can set
ALTER TABLE theTableInQuestion AUTO_INCREMENT=10000000
Of course, be sure to create backups and test, but it should not pose any problems at all. (Note that the old records will be stored in order of the id-field, which is random, and won't reflect the creation order.)
As you need to keep the old IDs, I'm going to assume that you're going to create a new column for autoincrement ID that will become your primary key but keep the existing ID column and rename it (to old_id, maybe?). I'm also going to assume you record when a customer signed up.
If you make your old ID column nullable (allow NULL as a valid value) then you can simply check whether or not the old ID column is NULL. If it's not NULL then treat that as the ID, otherwise use the autoincrement column.
Finding a customer:
SELECT *
FROM customer
WHERE (id = /*Put your ID here*/ AND reg_date >= /*Put the date the new regime starts here*/)
OR (id_old = /*put your ID here*/ AND reg_date < /*Put the date the new regime starts here*/)
This will occasionally return 2 rows so you'll have to use some other criteria to uniquely identify the customer in question.
As for associating an old customer with other tables in the database, you can always use the new ID internally throughout the entire DB once its generated. You will have to update tables that are using the old ID as the foreign key, obviously.
UPDATE target_table
JOIN customers on target_table.cust_id = customers.id_old
SET target_table.cust_id = customers.id;
(Note: The above is just a quick and dirty query that hasn't been tested! I'd suggest testing on a copy of the database before you try it for real!)

mysql prevent insert row auto sorting

I use Codeigniter to perform insert to mysql (not sure if relevant), I have table column and some data like this after I insert:
[invoice_id][product_id][unit_cost][quantity]
[42][1][50][2]
[42][2][100][3]
[42][5][45][1]
The problem is mysql auto sort it by invoice_id first then product_id like the above.
Before I insert them, my invoice item-list position was :
[42][5][45][1]
[42][1][50][2]
[42][2][100][3]
I do not want any of this auto-sort because when I retrieve them, they went like the list in database not as in the invoice. I dont think I can use sort for a particular column because they are all random in the first place.
I can only start thinking to add another column [position] contain number just for the sake of sorting it later, or is there a better way without it?
Thanks in advance for any reply.
You are not guaranteed the order of MySql resultsets unless you specify an ORDER BY clause. The only way to do it if you cannot sort with the parameters you already have would be to add another column to the table. Typically this would be an autoincrement integer field. You would then be able to order by id, and return the rows in the order they were entered.