如何创建复合主键,是否已经定义了主键。 MySQL的

时间:2023-01-24 11:28:57

I have a table with primary key auto_increment and I am having a duplicate data problem. To solve this I tried to create a composite primary key but it's not possible: #1068 - Multiple primary key defined.

我有一个主键auto_increment的表,我有一个重复的数据问题。为了解决这个问题,我试图创建一个复合主键,但这是不可能的:#1068 - 定义了多个主键。

I can't drop the primary key because there are relationship with other tables and I can't just put Unique constraint in only one column because the rule is to be composite.

我不能删除主键,因为与其他表有关系,我不能只将Unique约束放在一列中,因为规则是复合的。

What can I do? I am using MySQL.

我能做什么?我正在使用MySQL。

1 个解决方案

#1


Modifying the primary key on a table is a tricky exercise. This is doubly true when the existing key is defined auto-increment.

修改表上的主键是一项棘手的练习。当现有密钥定义为自动增量时,这是双重的。

You can create a composite unique key, though.

但是,您可以创建复合唯一键。

ALTER TABLE ADD UNIQUE KEY (`column1`, `column2');

#1


Modifying the primary key on a table is a tricky exercise. This is doubly true when the existing key is defined auto-increment.

修改表上的主键是一项棘手的练习。当现有密钥定义为自动增量时,这是双重的。

You can create a composite unique key, though.

但是,您可以创建复合唯一键。

ALTER TABLE ADD UNIQUE KEY (`column1`, `column2');