复合键中的auto_increment用法

Sof*_*oft 2 mysql auto-increment

我正在使用复合键的表

emp_tbl(
companyId int not null,
empId int not null auto_increment,
name varchar2,
....
...
primary key(companyId,empId)
);
Run Code Online (Sandbox Code Playgroud)

在mysql中,当我开始插入数据时发生了什么

Emp_tbl

companyId    empId
1             1
1             2
1             3
2             1
2             2
Run Code Online (Sandbox Code Playgroud)

请注意,当companyId更改时,auto_increament值将再次重置为1.我想禁用它.我的意思是我不想重置auto_increament.我期待这样的结果.

companyId    empId
1             1
1             2
1             3
2             4
2             5
Run Code Online (Sandbox Code Playgroud)

有可能吗?谢谢

Mar*_*ker 12

这是包含auto_increment的复合主键所发生的情况.重新创建主键,使其纯粹是你的auto_increment字段(empId),然后在companyId和empId上创建一个唯一索引

编辑

请注意,这仅适用于MyISAM和BDB表.如果您使用InnoDB作为您的表,那么它也可以按您的需要工作