tuurbo's avatar
Level 11

Mysql auto increment multiple ids

I have an e-commerce store i manage and i want to make it look like we get more orders than we do (at least until we start to bring in more business).

The below code works and i don't see any problems with it, but i wanted to know if anyone knows of any problems it could create. Thanks!

// create 3 blank orders and then delete them
DB::beginTransaction();
    Order::create([]);
    Order::create([]);
    Order::create([]);
DB::rollback();

// Create order as usual
Order::create([
    'email' => '...',
    'type' => 1,
    ...
]);
0 likes
5 replies
bestmomo's avatar

Why not a simple SQL command :

DB::statement( 'ALTER TABLE orders AUTO_INCREMENT=....' );
bashy's avatar

Most start from #1000 or you could total up the orders and add 5/10 to it?

tuurbo's avatar
Level 11

@bestmomo I don't think that would work, because i would have to first look up the latest order id and then add 3 to it which could cause duplicate ids if two orders were placed simultaneously

@bashy similar issue as described above

I'm going to stick with what i have already, so far it hasn't caused a problem. Thanks

pmall's avatar

Why modifying this at the database level ? Just add N to the total order count where you display it.

bestmomo's avatar

@tuurbo you can use a stored procedure to set this id :

DROP PROCEDURE IF EXISTS reset_autoincrement;
DELIMITER //
CREATE PROCEDURE reset_autoincrement()
BEGIN
      SELECT @max := MAX(id) + 4 FROM orders; 
      SET @alter_statement = concat('ALTER TABLE orders AUTO_INCREMENT = ', @max);
      PREPARE stmt FROM @alter_statement;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;
END//
CALL reset_autoincrement();

Please or to participate in this conversation.