This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-15. For the latest, interactive version please visit https://mariadb.com/kb/.

[INSERT .. WITH RECURSIVE] statement generates strange [AUTO_INCREMENT] value

/*
 * The following code is showing
 * [INSERT .. WITH RECURSIVE] statement generates
 * strange [AUTO_INCREMENT] value
 */

-- CREATE TABLE
DROP TABLE IF EXISTS MYDB.XXX;
CREATE TABLE MYDB.XXX (
  SEQ INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,
  VAL VARCHAR(10)                     NULL      DEFAULT NULL,
  PRIMARY KEY (SEQ)
);

-- [INSERT .. WITH RECURSIVE] :: 10 Rows
INSERT INTO MYDB.XXX (VAL)
  WITH RECURSIVE CTE AS (
    SELECT 1 AS VAL
    UNION
    SELECT VAL + 1 AS VALUE FROM CTE
      WHERE VAL < 10
  )
  SELECT * FROM CTE;

-- NORMAL INSERT :: 1 Row [NEXT]
INSERT INTO MYDB.XXX (VAL)
  SELECT 'NEXT';

-- SELECT INSERTED DATA
SELECT * FROM MYDB.XXX;

-- > |SEQ|VAL |
-- > |---|----|
-- > |1  |1   |
-- > |2  |2   |
-- > |3  |3   |
-- > |4  |4   |
-- > |5  |5   |
-- > |6  |6   |
-- > |7  |7   |
-- > |8  |8   |
-- > |9  |9   |
-- > |10 |10  |
-- > |16 |NEXT|

-- SELECT AUTO_INCREMENT VALUE
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'MYDB' AND TABLE_NAME = 'XXX';

-- > |AUTO_INCREMENT|
-- > |--------------|
-- > |17            |

Is this bug?

Answer Answered by Daniel Black in this comment.

Issues like MDEV-17377 where considered a bug for their wasting of auto_increment values so I do see this as a valid bug and might indeed have a simple fix.

Please do create a bug report.

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.