更改活动表以使键不唯一

发布于 2024-10-02 03:03:13 字数 104 浏览 4 评论 0原文

我看到了一些与此相关的其他问题,但它们不是MySQL。

该数据库是实时数据库,因此我不想删除并重新创建表。我只是想让一个列不再是唯一的,这本质上不太宽松,所以它不会引起任何问题。

I saw some other questions related to this, but they were not MySQL.

The database is a live database, so I don't want to delete and recreate the table. I simply want to make a column no longer unique, which is less permissive in nature so it shouldn't cause any problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

酷炫老祖宗 2024-10-09 03:03:13

如果您的列使用 UNIQUE 子句定义为唯一,则使用:

ALTER TABLE mytable DROP INDEX constraint_name

或者,如果您的约束是隐式命名的,

ALTER TABLE mytable DROP INDEX column_name

如果使用 PRIMARY KEY 子句定义为唯一,则使用:

ALTER TABLE mytable DROP PRIMARY KEY

, 但是,如果您的表是 InnoDB,则删除 PRIMARY KEY 将导致隐式重新创建表并重建所有索引,这将锁定表并可能使其无法访问很长一段时间。

If your column was defined unique using UNIQUE clause, then use:

ALTER TABLE mytable DROP INDEX constraint_name

, or, if your constraint was implicitly named,

ALTER TABLE mytable DROP INDEX column_name

If it was defined unique using PRIMARY KEY clause, use:

ALTER TABLE mytable DROP PRIMARY KEY

Note, however, that if your table is InnoDB, dropping PRIMARY KEY will result in implicit recreation of your table and rebuilding all indexes, which will lock the table and may make it inaccessible for quite a long time.

朮生 2024-10-09 03:03:13

这些是 phpmyadmin 应用程序的说明(如果您正在使用 phpMyAdmin) ::

在某些情况下,开发人员(您)可能不想删除它,而只是将“唯一性”修改为“非唯一”。

步骤:

  1. 转到上下文中要进行修改的表

  2. 单击“结构”选项卡(主要是下一个)浏览)

  3. 查找列下方的“+索引”链接。是的...现在单击它
  4. 现在您可以看到所有“索引”,您现在可以单击“DROP”按钮或链接进行修改。

在这里找到答案:
来源: https://forums.phpfreaks.com /topic/164827-phpmyadmin-how-to-make-not-unique/

These are instructions for phpmyadmin app (if you are using phpMyAdmin) ::

In a some cases, the developer (you) may not want to drop it but rather just modify the "uniqueness" to "not-unique".

Steps :

  1. Go to the table in context, where you want to make the modification

  2. Click on the "Structure" tab (mostly next to Browse)

  3. Look for the "+Indexes" link, just under the columns. Yeah... now click it
  4. Now you can see all the "Indexes" and you can now click on the "DROP" button or link to modify.

Answer was found here :
Source : https://forums.phpfreaks.com/topic/164827-phpmyadmin-how-to-make-not-unique/

微凉徒眸意 2024-10-09 03:03:13

尽管接受的答案不正确(请参阅评论),但建议的解决方法是可能的。但这也不正确,至少对于“实时表”来说是这样。

为了降低影响,您应该首先创建一个新索引,然后删除旧索引:

ALTER TABLE mytable ADD INDEX idx_new (column);
ALTER TABLE mytable DROP INDEX idx_old;

这可以避免使用根本没有索引的表(列),这对于客户端和 MySQL 本身来说都是非常痛苦的。

Although the accepted answer is incorrect (see comments), the suggested workaround is possible. But it is not correct too, at least for a "live table", as asked.

To lower the impact you should create a new index at first, and then delete the old one:

ALTER TABLE mytable ADD INDEX idx_new (column);
ALTER TABLE mytable DROP INDEX idx_old;

This avoids using the table (column) without index at all, which can be quite painful for clients and the MySQL itself.

吃素的狼 2024-10-09 03:03:13

只需DROP唯一索引即可。它是一个实时数据库,不应该有问题。如果它是一个非常大的表,您可能会在删除索引时暂时阻止一些查询。但这种情况应该只有在您添加索引时才会发生。

ALTER TABLE table_name DROP INDEX index_name;

Just DROP the unique index. There shouldn't be a problem with the fact that it is a live DB. If it is a really large table, you may block some queries temporarily while the index is removed. But that should only happen if you were adding an index.

ALTER TABLE table_name DROP INDEX index_name;
半枫 2024-10-09 03:03:13

MySQL 需要外键和引用键上的索引,以便外键检查可以快速 (MySQL 手册)。

如果您想要使其非唯一的唯一键被外键约束使用,那么您将收到错误当放下它时。您必须在同一行重新创建它:

alter table mytable drop KEY myUniqueKey, add key myUniqueKey (myColumn);

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast (MySQL Manual).

If the unique key that you want to make non-unique is used by a foreign key constraint, then you'll get an error when dropping it. You will have to recreate it on the same line:

alter table mytable drop KEY myUniqueKey, add key myUniqueKey (myColumn);
晨敛清荷 2024-10-09 03:03:13

您需要删除与该列名关联的约束或索引。要删除它,您需要删除它。操作方法如下:

首先,找到唯一索引或唯一约束的名称。您可以使用以下查询来查找索引名称:

SELECT DISTINCT
    TABLE_NAME,
    COLUMN_NAME,
    CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
    TABLE_SCHEMA = 'your_database_name' AND
    TABLE_NAME = 'your_table_name' AND
    COLUMN_NAME = 'the_column_that_is_unique';

举例来说,我们在数据库 Animals 中有表 pets,并且每个宠物都有一个幸运数字。我们错误地将 lucky_number 列设为唯一,但宠物可以拥有相同的幸运数字。以下是我们如何找到与该列关联的约束:

SELECT DISTINCT
    TABLE_NAME,
    COLUMN_NAME,
    CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
    TABLE_SCHEMA = 'animals' AND
    TABLE_NAME = 'pets' AND
    COLUMN_NAME = 'lucky_number';

并且我们必须获得以下输出:
输出

之后我们需要删除它。我们可以通过运行以下命令来做到这一点:

ALTER TABLE table_name DROP INDEX constraint_name;

在我们的示例中,上面的命令变为:

ALTER TABLE pets DROP INDEX lucky_number;

You need to remove the constraint or the index that is associated to that column name. To remove the it, you need to drop it. Here is how you do it:

First, find the name of the unique index or unique constraint. You can use the following query to find the index name:

SELECT DISTINCT
    TABLE_NAME,
    COLUMN_NAME,
    CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
    TABLE_SCHEMA = 'your_database_name' AND
    TABLE_NAME = 'your_table_name' AND
    COLUMN_NAME = 'the_column_that_is_unique';

Say for example we have the table pets in the database animals and each of them pets have a lucky number. By mistake we made the lucky_number column unique but the pets can have the same lucky number. Here is how we find the constraint associated with that column:

SELECT DISTINCT
    TABLE_NAME,
    COLUMN_NAME,
    CONSTRAINT_NAME
FROM
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
    TABLE_SCHEMA = 'animals' AND
    TABLE_NAME = 'pets' AND
    COLUMN_NAME = 'lucky_number';

And we must get the following output:
output

After that we need to drop it. We can do it by running the following command:

ALTER TABLE table_name DROP INDEX constraint_name;

In our example the above command becomes:

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