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,
...
]);
Why not a simple SQL command :
DB::statement( 'ALTER TABLE orders AUTO_INCREMENT=....' );
Most start from #1000 or you could total up the orders and add 5/10 to it?
@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
Why modifying this at the database level ? Just add N to the total order count where you display it.
@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 sign in or create an account to participate in this conversation.