ALTER TABLE 添加复合主键

发布于 2024-12-27 02:46:46 字数 192 浏览 1 评论 0原文

我有一个名为 provider 的表。我有三列,分别为 personplacething。可以有重复的人、重复的地点和重复的事物,但永远不会有重复的人-地-物组合。

我将如何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只怪假的太真实 2025-01-03 02:46:46
ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);

如果主键已经存在那么你想要这样做

ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing);
ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);

If a primary key already exists then you want to do this

ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing);
不必在意 2025-01-03 02:46:46

@Adrian Cornish 的答案是正确的。但是,删除现有主键还有另一个警告。如果该主键被另一个表用作外键,则在尝试删除它时会收到错误消息。在某些版本的 mysql 中,错误消息格式错误(从 5.5.17 开始,此错误消息仍然是“

alter table parent  drop column id;
ERROR 1025 (HY000): Error on rename of
'./test/#sql-a04_b' to './test/parent' (errno: 150).

如果要删除另一个表引用的主键,则必须删除该其他表中的外键”)首先,如果在重新创建主键后仍需要该外键,则可以重新创建该外键。

此外,在使用复合键时,顺序很重要。
这些

1) ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);
and
2) ALTER TABLE provider ADD PRIMARY KEY(person,thing,place);

不是同一件事。
它们都强制该组三个字段的唯一性,但是从索引的角度来看存在差异。这些字段从左到右索引。
例如,考虑以下查询:

A) SELECT person, place, thing FROM provider WHERE person = 'foo' AND thing = 'bar';
B) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz';
C) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz' AND thing = 'bar';
D) SELECT person, place, thing FROM provider WHERE place = 'baz' AND thing = 'bar';

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

alter table parent  drop column id;
ERROR 1025 (HY000): Error on rename of
'./test/#sql-a04_b' to './test/parent' (errno: 150).

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

1) ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);
and
2) ALTER TABLE provider ADD PRIMARY KEY(person,thing,place);

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:

A) SELECT person, place, thing FROM provider WHERE person = 'foo' AND thing = 'bar';
B) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz';
C) SELECT person, place, thing FROM provider WHERE person = 'foo' AND place = 'baz' AND thing = 'bar';
D) SELECT person, place, thing FROM provider WHERE place = 'baz' AND thing = 'bar';

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.

森林很绿却致人迷途 2025-01-03 02:46:46

您可能只是想要一个唯一的约束。特别是如果您已经有代理键。
(已经存在的代理键的示例是一个 AUTO_INCRMENT 列)

下面是唯一约束的 sql 代码

ALTER TABLE `MyDatabase`.`Provider`
    ADD CONSTRAINT CK_Per_Place_Thing_Unique UNIQUE (person,place,thing)
;

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 `MyDatabase`.`Provider`
    ADD CONSTRAINT CK_Per_Place_Thing_Unique UNIQUE (person,place,thing)
;
转瞬即逝 2025-01-03 02:46:46
alter table table_name add primary key (col_name1, col_name2);
alter table table_name add primary key (col_name1, col_name2);
裂开嘴轻声笑有多痛 2025-01-03 02:46:46

ALTER TABLE table_name 删除主键,添加主键(col_name1,col_name2);

ALTER TABLE table_name DROP PRIMARY KEY,ADD PRIMARY KEY (col_name1, col_name2);

掌心的温暖 2025-01-03 02:46:46

使用复合唯一键肯定更好,正如 @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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文