change auto_increment within same table using subquery mysql

后端 未结 4 1206
青春惊慌失措
青春惊慌失措 2021-01-14 15:56

I am using mysql. I have a database table with auto_increment counter set. Now because of a requirement I need to leave starting 100 ids free and move all existing records s

4条回答
  •  耶瑟儿~
    2021-01-14 16:27

    The parser does not support a subquery in the place you are trying to use it.

    Here's the excerpt from the MySQL source, from sql/sql_yacc.yy:

    create_table_option:
        . . .
        | AUTO_INC opt_equal ulonglong_num
    

    What you should read there is that the AUTO_INCREMENT table option accepts only a single literal number, not an expression or a subquery or a variable or anything else. So you simply can't set the AUTO_INCREMENT in the same statement in which you do SELECT MAX(id)+1.

    But you don't have to.

    MySQL will never allocate an auto-increment id less than the largest value currently in the table. So if you have a table with id value 102, the next value allocated will be at least 103.

    You can even try to set AUTO_INCREMENT=50 explicitly, but that will be increased automatically to MAX(id)+1.

提交回复
热议问题