MySQL auto-increment based on group

后端 未结 3 847
予麋鹿
予麋鹿 2021-01-29 05:07

The problem is related to autoincrement with mysql. What I\'m trying to achieve is to increment an ID value based on the customer number. So basically i insert data sets without

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-29 05:43

    What you probably need is to have different values for ID for each customer. The easiest way to achieve this is to use an AUTO_INCREMENT column as PK of your table.

    It is an implementation detail that for consecutively inserted rows an AUTO_INCREMENT column has consecutive values. And the previous statement is not even true. It just happens some times, it is not guaranteed. If an INSERT statement is enclosed in a transaction that is rolled back, the value generated by that insert is skipped. Also, if an INSERT statements that use ON DUPLICATE KEYS UPDATE tries to insert many rows but some of them already exist in the table then the IDs generated for the duplicate keys are skipped.

    What I want to stress out is that there is no point trying to get consecutive values using an AUTO_INCREMENT column and it is not even possible.

    Back to your problem, if the column ID is the PK of the table and its type is INT AUTO_INCREMENT then MySQL guarantees there won't be two rows having the same value in the ID column and this also satisfies your need to have different values for ID for all the rows with the same value in customer.

提交回复
热议问题