简体   繁体   English

MySQL更新唯一索引

[英]MySQL update unique index

Is there a way to say UPDATE items SET qty=10 WHERE **unique key** instead of saying UPDATE items SET qty=10 WHERE userID=1 AND listID=10 and itemID=100 on the following table? 有没有办法在下表中说UPDATE items SET qty=10 WHERE **unique key**而不是说UPDATE items SET qty=10 WHERE userID=1 AND listID=10 and itemID=100

CREATE TABLE IF NOT EXISTS `items` (
  `userID` int(20) NOT NULL,
  `listID` int(20) NOT NULL,
  `itemID` int(20) NOT NULL,
  `qty` int(10) NOT NULL,
  UNIQUE KEY `unique` (`userID`,`listID`,`itemID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I'm not 100% sure what you're asking, but if you want to know if you can name the unique index in the WHERE clause and supply some kind of single combined value to the find the row you want, the answer is no. 我不是100%知道您要问的是什么,但是如果您想知道是否可以在WHERE子句中命名唯一索引并提供某种单一组合值来查找所需的行,答案是否定的。 If you want to use the index, you must specify the three separate column values as in your example and let MySQL figure out whether that is the best index to use (in this case, the answer will be yes). 如果要使用索引,则必须像示例中那样指定三个单独的列值,并让MySQL找出这是否是最佳索引(在这种情况下,答案是肯定的)。

The only way I know is: 我知道的唯一方法是:

UPDATE items
SET qty=10
WHERE (userID, listID, itemID) = (1, 10, 100)

Is that what you seek? 那是你要找的吗?

The syntax is equivalent to the one using AND , just a bit more compact. 语法与使用AND的语法等效,只是紧凑一点。 It doesn't guarantee that the index will be used although that is probable using either this or your syntax. 尽管使用此语法或您的语法可能会使用索引,但不能保证会使用该索引。

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

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