gpt4 book ai didi

MySQL Composite 主键唯一性

转载 作者:行者123 更新时间:2023-11-30 22:50:14 27 4
gpt4 key购买 nike

如何在 mySQL 中创建复合主键。在 table1 中,我需要将 id1、id2 设置为复合主键。我使用了这个查询。但它使每个成为主键。它检查每个条目的重复项

ALTER TABLE `table1`
DROP PRIMARY KEY,
ADD PRIMARY KEY(
`id1`,
`id2`);

当我插入两行时

insert "a","b" 
insert "a","c"

1062 - Duplicate entry '1408181771009' for key 'pk_sentiments'

但是

insert "a","c" 
insert "b","c"

工作正常。我无法理解这种行为。如何解决?

最佳答案

也许是这样的:

ALTER TABLE `table1` ADD UNIQUE KEY `id1_id2` (`id1`, `id2`);

关于MySQL Composite 主键唯一性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28375094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com