I've made a bit of a mistake and I cannot correct it. I have a table Email
and I've used the email address as the Primary Key. This is a mistake and I want to add an Auto Increment ID to the table instead.
However, the table is System Versioned and there is historical data. For this reason, MariaDB won't allow me to add an Auto Increment column to the table.
This demonstrates the problem:
CREATE OR REPLACE TABLE `Email_Test` (
`email` VARCHAR(100) NOT NULL,
`modifiedBy` INT(10) UNSIGNED NOT NULL,
`modifyReason` VARCHAR(200) NULL DEFAULT NULL,
`Person_idPerson` INT(10) UNSIGNED NULL DEFAULT NULL,
`Account_idAccount` INT(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (email, Account_idAccount),
INDEX `fk_Email_Person1_idx` (`Person_idPerson` ASC),
UNIQUE INDEX `UNIQUE` (`email` ASC, `Account_idAccount` ASC)
)
ENGINE = Aria
DEFAULT CHARACTER SET = utf8mb4
WITH SYSTEM VERSIONING
PARTITION BY SYSTEM_TIME (
PARTITION part_history HISTORY,
PARTITION part_current CURRENT
);
INSERT INTO Email_Test SET email = 'test@example.com', ModifiedBy = 3, Person_idPerson = 2, Account_idAccount = 1;
UPDATE Email_Test SET email = 'newemail@example.com' WHERE Person_idPerson = 2;
SET @@system_versioning_alter_history = 'keep';
ALTER TABLE `Email_Test`
DROP PRIMARY KEY,
ADD COLUMN `idEmail` INT(10) UNSIGNED NOT NULL PRIMARY KEY FIRST;
ALTER TABLE `Email_Test`
MODIFY COLUMN `idEmail` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;
If the UPDATE
statement is omitted, it works fine. However, with the UPDATE
statement, is generates Error 1112 - "Table Email_Test uses an extension that doesn't exist in this MariaDB version". The error comes from the second ALTER TABLE
statement where we add the Auto Increment.
The same error occurs if we try to add the Primary Key and Auto Increment in one atomic operation:
ALTER TABLE `Email_Test`
DROP PRIMARY KEY,
ADD COLUMN `idEmail` INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;
Is there a way of adding an Auto Increment column to an existing system versioned table with historical data?
If there isn't a way, can you suggest a way of moving the existing data, including the historical data, to another table temporarily so that the Email
table can be dropped and created properly before copying the data back into it?