Can I explicitly set Magento 1.9 Order Increment ID on Creation - magento-1.9

Is it possible to change / set the order increment ID "on the fly" while creating orders?
This Inchoo post shows me how to create an order https://inchoo.net/magento/programmatically-create-order-in-magento/
But I cannot seem to find how to change / set the number programmatically. ( I don't just want to set a sequence )
thanks for your time.

Related

Mysql : How can i set number of gap 5 on each increment number in auto increment column?

I need to set each increment id +5 from the previous one generated id table wise. It means each auto-increment have to 5 number of the gap. Like this series 2,7,12,17 etc..
Any idea please share.
Please check the link:
https://dev.mysql.com/doc/refman/8.0/en/replication-options-master.html#sysvar_auto_increment_offset
SET ##auto_increment_offset=5;
SHOW VARIABLES LIKE 'auto_inc%';
CREATE TABLE autoinc2 (col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
You should not be demanding this requirement from an auto increment column. The contract of an auto increment column only guarantees that each generated value would be unique. The values would tend to generally be increasing, but if the counter were reset, and certain earlier records were deleted, there could be a value generated smaller than one which already exists. Instead, consider maintaining some sort of timestamp column which keeps track of when each record were added to your table. Then, using ROW_NUMBER, it is easy to generate the sequence you expect, e.g.
SELECT
5*ROW_NUMBER() OVER (ORDER BY ts_column) seq
FROM yourTable;
ORDER BY
ts_column;
If you need to increment by 5, you can set auto_increment_increment=5.
This will apply to all tables, so you should set it as a session variable, not a global variable. The session variable will only apply to INSERT statements made in your current session, not to other sessions.
Then change the option back to 1 before you INSERT to another table.
There is no feature in MySQL to make the auto_increment_increment option apply permanently and for all sessions, but only for a specific table.

Reset AUTO INCREMENT by the order of another column

I'm looking find a way to reset the AUTO_INCREMENT on my events table by the ORDER of start ASC
I've looked everywhere but currently not found an answer
The issue is events get deleted all the time and it causes massive gaps in the Auto Incement for example we skip form ID 200 to 768 because that list of events was deleted
ALTER TABLE table_name AUTO_INCREMENT = [Your Incremental Value Here];
query start auto-increment from given incremental value.

Change AutoIndex number Mysql

In my table I have auto increment id which is having its number like 1 to 66,440 and next number is 88846790 + increment from here till 88846900.
There is no records in between 66440 to 88846790
I want my next auto increment number to be 66441 but its taking 88846791, can you help me in setting next auto increment to 66441,
alter table tablename AUTO_INCREMENT=664441
should do
You can use ALTER TABLE to change the auto_increment value:
ALTER TABLE my_table AUTO_INCREMENT = 66441;
See the MySQL reference for more details.
Remember to reinsert all rows with an id higher than 88846790
If you don't remove rows with a higher ID than 66441,
the change in autoincrement does nothing.
Example:
ID Value
---- --------
1 C
2 A
4 D
50 A
51 C
If I want to change auto increment to 5 I need to remove or re-insert the 50 and 51 first. Because otherwise the next auto increment is gonna be 52 anyway.
Depending on how much and how important the data is, often the best thing to do is: Nothing
Because those primary ID's have relations to other rows, and maybe even web- urls based opn those IDs. This will all fail, unless you create some sort of script.
I had trouble getting this to work in phpMyAdmin, even inputting the query directly. If you browse the table you want to reset the index of, then click Operations, you should be able to set the AUTO_INCREMENT directly under Table options. Then just click Go and you're all set!
Rebutal to all those recommending ALTER:
You cannot reset the counter to a value less than or equal to the
value that is currently in use. For both InnoDB and MyISAM, if the
value is less than or equal to the maximum value currently in the
AUTO_INCREMENT column, the value is reset to the current maximum
AUTO_INCREMENT column value plus one.
-- https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
(That is from the 8.0 manual, but I believe that it has always applied; I think I discovered it in 4.0 days.)
Better than deleting and re-inserting:
Renumber the higher values:
UPDATE t SET id = id - 88846790 + 66440 + 1 WHERE id >= 88846790;
But then comes the hassle of renumbering references to this id. They can use a similar update:
UPDATE other_table SET t_id = t_id - 88846790 + 66440 + 1 WHERE t_id >= 88846790;

Preventing duplicate data with locking or transactions?

In our application, when a user creates an order we get the next order # as follows:
SELECT MAX(CAST(REPLACE(orderNum, 'SO', '') AS SIGNED)) + 1 FROM orders
The problem is that because the customer is getting busier, we are starting to see orders that are created at exactly the same time which results in duplicate order #'s.
What is the best way to handle this? Should we lock the whole orders table or just the row? Or should we be doing transactions?
You could create an extra table with just one field which has the auto_increment attribute.
Now every time you need a new order number you call a function that will create a field in this table and return the result of last_insert_id(), which you then can use as an order number (just make sure to set the auto_increment counter of the table higher than your greatest order number).

Increment autoincrement id field by one

I have a MySQL 5 server and a table in it with an autoincrement on an id field (primary key). Now I want to add a record in between and so I have to increase all other ids by one. This is what I tried:
UPDATE myTable SET id=id+1 WHERE id >= 53
This doesn't work because for example a record with id = 52 already exists. How can I do this? If he would start at the last entry and makes the updates it should work I think. But how?
I see no good reason for this. Only problems. Before running the folowing statement, check if you have FOREIGN keys defined, that reference this id. Are they set to ON UPDATE CASCADE? Also, do you have any triggers that are related to this table?
But first consider, why you (think you) need this. Is it going to be used for ordering the table? In that case, as #Mark pointed, you should use a separate column to specify your desired order.
If, however, you decide you really want this, use:
UPDATE myTable
SET id = id + 1
WHERE id >= 53
ORDER BY id DESC ;
Quick and dirty you do it in 2 steps.
increase the id to a number higher that all others
decrease all ids to the number you want
Like that
UPDATE myTable SET id=id+10000 WHERE id >= 53
UPDATE myTable SET id=id-9999 WHERE id >= 53
I had faced same problem. And also tried with query by OP and #Romain Hoog . But not succeeded.
Finally exported data of whole table in excel and done it in excel (not one by one but using trick that makes it very fast).
then taken backup of original table recreated new table and imported data from updated excel .
I guess something like that works :
UPDATE myTable SET id=id+1 WHERE id >= (select id from myTable order by id DESC)