更改活动表以使键不唯一
我看到了一些与此相关的其他问题,但它们不是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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您的列使用
UNIQUE
子句定义为唯一,则使用:或者,如果您的约束是隐式命名的,
如果使用
PRIMARY KEY
子句定义为唯一,则使用:, 但是,如果您的表是 InnoDB,则删除 PRIMARY KEY 将导致隐式重新创建表并重建所有索引,这将锁定表并可能使其无法访问很长一段时间。
If your column was defined unique using
UNIQUE
clause, then use:, or, if your constraint was implicitly named,
If it was defined unique using
PRIMARY KEY
clause, use:Note, however, that if your table is
InnoDB
, droppingPRIMARY 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.这些是 phpmyadmin 应用程序的说明(如果您正在使用 phpMyAdmin) ::
在某些情况下,开发人员(您)可能不想删除它,而只是将“唯一性”修改为“非唯一”。
步骤:
转到上下文中要进行修改的表
单击“结构”选项卡(主要是下一个)浏览)
在这里找到答案:
来源: 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 :
Go to the table in context, where you want to make the modification
Click on the "Structure" tab (mostly next to Browse)
Answer was found here :
Source : https://forums.phpfreaks.com/topic/164827-phpmyadmin-how-to-make-not-unique/
尽管接受的答案不正确(请参阅评论),但建议的解决方法是可能的。但这也不正确,至少对于“实时表”来说是这样。
为了降低影响,您应该首先创建一个新索引,然后删除旧索引:
这可以避免使用根本没有索引的表(列),这对于客户端和 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:
This avoids using the table (column) without index at all, which can be quite painful for clients and the MySQL itself.
只需
DROP
唯一索引即可。它是一个实时数据库,不应该有问题。如果它是一个非常大的表,您可能会在删除索引时暂时阻止一些查询。但这种情况应该只有在您添加索引时才会发生。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.MySQL 需要外键和引用键上的索引,以便外键检查可以快速 (MySQL 手册)。
如果您想要使其非唯一的唯一键被外键约束使用,那么您将收到错误当放下它时。您必须在同一行重新创建它:
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:
您需要删除与该列名关联的约束或索引。要删除它,您需要删除它。操作方法如下:
首先,找到唯一索引或唯一约束的名称。您可以使用以下查询来查找索引名称:
举例来说,我们在数据库 Animals 中有表 pets,并且每个宠物都有一个幸运数字。我们错误地将 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:
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:
And we must get the following output:
output
After that we need to drop it. We can do it by running the following command:
In our example the above command becomes: