簡體   English   中英

復合鍵中的auto_increment用法

[英]auto_increment usage in composite key

我正在使用復合鍵的表

emp_tbl(
companyId int not null,
empId int not null auto_increment,
name varchar2,
....
...
primary key(companyId,empId)
);

在mysql中,當我開始插入數據時發生了什么

Emp_tbl

companyId    empId
1             1
1             2
1             3
2             1
2             2

請注意,當companyId更改時,auto_increament值將再次重置為1。 我想禁用它。 我的意思是我不想重置auto_increament。 我期待這樣的結果。

companyId    empId
1             1
1             2
1             3
2             4
2             5

有可能嗎? 謝謝

這是包含auto_increment的復合主鍵所發生的情況。 重新創建主鍵,使其純粹是你的auto_increment字段(empId),然后在companyId和empId上創建一個唯一索引

編輯

請注意,這僅適用於MyISAM和BDB表。 如果您使用InnoDB作為您的表,那么它也可以按您的需要工作

如果您不希望empId重置,則只需顛倒主要定義的順序

primary key(companyId,empId)

請注意,復合鍵順序很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM