MySQL equivalent of Oracle's SEQUENCE.NEXTVAL

前端 未结 7 1378
无人及你
无人及你 2020-12-01 11:13

I need to be able to generate run a query that will return the next value of ID on the following table:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUT         


        
7条回答
  •  悲&欢浪女
    2020-12-01 11:50

    You want the next value on THAT table so that you can make rows which aren't yet inserted without disturbing other processes which are using the auto-increment?

    Some options:

    Go ahead and just insert the rows to "use up the sequence", mark them as pending and then update them later.

    Insert in a transaction and abort the transaction - the auto-number sequence should get used up and make a "gap" in the table normally - that number is now free for you to use.

    With Oracle, the sequence is completely independent of table, so processes can use the sequence or not (and they can also use the same sequence for different tables). In that vein, you could implement a sequence-only table which you access through some kind of function, and for the other processes which need to rely on the auto-increment, remove the auto-increment and use a trigger which assigns from the same sequence function if no id is provided.

提交回复
热议问题