CREATE TRIGGER test1 AFTER INSERT ON `course_metadata_31`
FOR EACH ROW BEGIN
update `course_metadata_31`set `articleID`=`articleID`+1
END;
I am
Q: What should I do?
A: Review the reasons you think you need a second AUTO_INCREMENT columns, carefully consider what it is you are trying to achieve.
Come up with an alternate design that doesn't require you to add two AUTO_INCREMENT
columns to a MySQL table.
If you do really need to have a second column with "auto increment" type behavior, one way to get that is to add a second dummy table with an auto_increment column, and use a BEFORE INSERT trigger to do an insert into the dummy table, and retrieve the id value that was inserted.
Something like this:
CREATE TABLE course_metadata_31_ai
( i INT UNSIGNED PRIMARY KEY AUTO_INCREMENT
);
DELIMITER $$
CREATE TRIGGER course_metadata_31_bi
BEFORE INSERT ON course_metadata_31
FOR EACH ROW
BEGIN
DECLARE lii INT;
IF ( NEW.article_id IS NULL OR NEW.article_id < 1 ) THEN
-- set article_id to auto_increment from dummy table
INSERT INTO course_metadata_31_ai (i) VALUES (NULL);
SELECT LAST_INSERT_ID() INTO lii;
SET NEW.article_id = lii;
-- DELETE FROM course_metadata_31_ai WHERE i < lii;
ELSE
-- set auto_increment col in dummy table to match a larger article_id
UPDATE course_metadata_31_ai t
JOIN ( SELECT MAX(r.i) AS i
FROM course_metadata_31_ai r
) s
ON s.i = t.i
SET t.i = GREATEST(t.i,NEW.article_id);
END IF;
END;
$$
DELIMITER ;
NOTE
You wouldn't necessarily have to delete rows from the dummy table, you wouldn't have to do it in the trigger, but there's no point in keeping them. You'd probably only really need to keep the row that has the largest auto_increment value, just as a prevention against the AUTO_INCREMENT from inadvertently being set lower with an ALTER TABLE statement.)
The IF ELSE
in the trigger body is designed to emulate auto_increment behavior... if a value is supplied for article_id, use that, AND if it's larger than the current max value of auto_increment column in the dummy table, update that row in the dummy table to have the larger value (so the next insert that needs an auto_increment value will get the next higher value).