Untitled

 avatar
unknown
plain_text
17 days ago
1.3 kB
7
Indexable
-- Step 1: Temporarily disable foreign key checks
    SET FOREIGN_KEY_CHECKS = 0;

    -- Step 2: Backup the `salog_id` column to a temporary column
    ALTER TABLE fp_stock_adjust_log ADD COLUMN temp_salog_id BIGINT;

    -- Step 3: Reset the `temp_salog_id` values in ascending order of `salog_created_date`
    SET @new_id = 0;
    UPDATE fp_stock_adjust_log
    SET temp_salog_id = (@new_id := @new_id + 1)
    ORDER BY salog_created_date;

    -- Step 4: Drop the existing `salog_id` column
    ALTER TABLE fp_stock_adjust_log DROP COLUMN salog_id;

    -- Step 5: Rename the temporary column to `salog_id` and set it as the PRIMARY KEY
    ALTER TABLE fp_stock_adjust_log CHANGE COLUMN temp_salog_id salog_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY;

    -- Step 6: Set the AUTO_INCREMENT value to continue from the maximum ID
    SELECT MAX(salog_id) INTO @max_id FROM fp_stock_adjust_log;
    SET @sql = CONCAT('ALTER TABLE fp_stock_adjust_log AUTO_INCREMENT = ', @max_id + 1);
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    -- Step 7: Re-enable foreign key checks
    SET FOREIGN_KEY_CHECKS = 1;   

    -- step 8 - modify column order 
    ALTER TABLE fp_stock_adjust_log 
    MODIFY COLUMN salog_id BIGINT NOT NULL AUTO_INCREMENT FIRST;
Leave a Comment