ALTER TABLE 添加复合主键
我有一个名为 provider
的表。我有三列,分别为 person
、place
、thing
。可以有重复的人、重复的地点和重复的事物,但永远不会有重复的人-地-物组合。
我将如何 ALTER TABLE 为 MySQL 中的该表添加这三列的复合主键?
I have a table called provider
. I have three columns called person
, place
, thing
. There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination.
How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果主键已经存在那么你想要这样做
If a primary key already exists then you want to do this
@Adrian Cornish 的答案是正确的。但是,删除现有主键还有另一个警告。如果该主键被另一个表用作外键,则在尝试删除它时会收到错误消息。在某些版本的 mysql 中,错误消息格式错误(从 5.5.17 开始,此错误消息仍然是“
如果要删除另一个表引用的主键,则必须删除该其他表中的外键”)首先,如果在重新创建主键后仍需要该外键,则可以重新创建该外键。
此外,在使用复合键时,顺序很重要。
这些
不是同一件事。
它们都强制该组三个字段的唯一性,但是从索引的角度来看存在差异。这些字段从左到右索引。
例如,考虑以下查询:
B 可以在 ALTER 语句 1 中使用主键索引
A可以在ALTER语句2中使用主键索引
C 可以使用任一索引
D 不能使用任一索引
A 使用索引 2 中的前两个字段作为部分索引。 A 不能使用索引 1,因为它不知道索引的中间位置部分。不过,它仍然可以仅对人员使用部分索引。
D 不能使用任何一个索引,因为它不认识人。
有关详细信息,请参阅此处的 mysql 文档。
@Adrian Cornish's answer is correct. However, there is another caveat to dropping an existing primary key. If that primary key is being used as a foreign key by another table you will get an error when trying to drop it. In some versions of mysql the error message there was malformed (as of 5.5.17, this error message is still
If you want to drop a primary key that's being referenced by another table, you will have to drop the foreign key in that other table first. You can recreate that foreign key if you still want it after you recreate the primary key.
Also, when using composite keys, order is important.
These
are not the the same thing.
They both enforce uniqueness on that set of three fields, however from an indexing standpoint there is a difference. The fields are indexed from left to right.
For example, consider the following queries:
B can use the primary key index in ALTER statement 1
A can use the primary key index in ALTER statement 2
C can use either index
D can't use either index
A uses the first two fields in index 2 as a partial index. A can't use index 1 because it doesn't know the intermediate place portion of the index. It might still be able to use a partial index on just person though.
D can't use either index because it doesn't know person.
See the mysql docs here for more information.
您可能只是想要一个唯一的约束。特别是如果您已经有代理键。
(已经存在的代理键的示例是一个 AUTO_INCRMENT 列)
下面是唯一约束的 sql 代码
You may simply want a UNIQUE CONSTRAINT. Especially if you already have a surrogate key.
(example of an already existing surrogate key would be a single column that is an AUTO_INCREMENT )
Below is the sql code for a Unique Constraint
ALTER TABLE table_name 删除主键,添加主键(col_name1,col_name2);
ALTER TABLE table_name DROP PRIMARY KEY,ADD PRIMARY KEY (col_name1, col_name2);
使用复合唯一键肯定更好,正如 @GranadaCoder 提供的那样,但示例有点棘手:
ALTER IGNORE TABLE table_name ADD UNIQUES INDEX idx_name(some_id, another_id, one_more_id);
It`s definitely better to use COMPOSITE UNIQUE KEY, as @GranadaCoder offered, a little bit tricky example though:
ALTER IGNORE TABLE table_name ADD UNIQUES INDEX idx_name(some_id, another_id, one_more_id);