简体   繁体   English

在不删除MySQL db的情况下禁用单个约束

[英]Disabling single constraint in MySQL db without dropping it

I want to disable a single unique key constraint out of many without dropping it in MySQL db. 我想禁用一个唯一的唯一键约束,而不将其删除到MySQL数据库中。 Can someone please let me know. 有人可以让我知道。

There are two ways to answer this question..... 有两种方法可以回答这个问题.....

if your goal is to store data that's unique (except that one case, which is an exception), you're out of luck. 如果您的目标是存储唯一的数据(除了一种情况,这是一个例外),那么您就不走运了。 A unique key constraint for a table is exactly that.... a unique key constraint for a table. 一个表的唯一键约束就是...。表的唯一键约束。 And storing duplicate keys in a unique key column just isn't gonna work. 并且将重复的键存储在唯一的键列中是行不通的。

Now, if you're trying speed up data inserting because you already know your data are unique, that's a different matter. 现在,如果因为已经知道数据是唯一的而试图加快数据插入速度,那就是另一回事了。 Under those circumstances, you can set unique_checks = 0, insert your data, then turn them back on again 在这种情况下,您可以设置unique_checks = 0,插入数据,然后再次打开它们

This describes the process: http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_unique_checks 它描述了该过程: http : //dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_unique_checks

Quoting from that: 引述如下:

If set to 1 (the default), uniqueness checks for secondary indexes in InnoDB tables are performed. 如果设置为1(默认值),则对InnoDB表中的二级索引执行唯一性检查。 If set to 0, storage engines are permitted to assume that duplicate keys are not present in input data. 如果设置为0,则允许存储引擎假定输入数据中不存在重复的键。 If you know for certain that your data does not contain uniqueness violations, you can set this to 0 to speed up large table imports to InnoDB. 如果您确定您的数据不包含唯一性违规,则可以将其设置为0,以加快将大表导入InnoDB的速度。

Note that setting this variable to 0 does not require storage engines to ignore duplicate keys. 请注意,将此变量设置为0不需要存储引擎忽略重复的键。 An engine is still permitted to check for them and issue duplicate-key errors if it detects them. 仍然允许引擎检查它们并在检测到它们时发出重复密钥错误。

In other words, you can tell your database to trust you that you're not inserting data with duplicate keys... it may still check anyway, and you certainly cannot store duplicate keys in the table while there's a unique key constraint. 换句话说,您可以让数据库信任您,您不会使用重复的键插入数据...它仍然可以进行检查,并且在存在唯一键约束的情况下,您当然不能在表中存储重复的键。 But you might be able to speed up inserts wit h this. 但是,您可以以此来加快插入速度。

Btw, I am not aware of any way that you could disable unique key checks for a specific column, although I can't say for certain that it is impossible. 顺便说一句,我不知道您可以禁用特定列的唯一键检查的任何方式,尽管我不能肯定地说这是不可能的。 That is, I am not aware of (and neither do I believe there is) any way to ignore checks for one specific column out of many... but I cannot say for certain that it's strictly speaking impossible. 也就是说,我不知道(而且我也不相信有任何方法)忽略许多特定列中的特定检查...但是我不能肯定地说严格地说这是不可能的。

Depending on what your actual needs are (beyond disabling the key without deleting it, but rather why you need to disable that key without deleting it), you might also be able to get away with using ON DUPLICATE KEY ( https://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html ). 根据您的实际需求(除了禁用密钥而不删除它,而是为什么需要禁用该密钥而不删除它),您也许还可以使用ON DUPLICATE KEY( https:// dev。 mysql.com/doc/refman/5.6/en/insert-on-duplicate.html )。 Alternatively INSERT IGNORE might accomplish what you want, but I'd be extremely careful with that. 另外,INSERT IGNORE可能会完成您想要的,但是我会非常小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM