MYSQL InnoDB唯一键不能正常工作?

时间:2013-12-29 18:19:02

标签: mysql innodb unique-key

我有一个非常奇怪的问题,我不知道是什么原因引起的。任何建议将不胜感激。 这是表结构和查询:

CREATE TABLE `big_test` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
  `application_id` int(11) unsigned NOT NULL DEFAULT '0',
  `account_id` int(11) unsigned NOT NULL DEFAULT '0',
  `dev_id` char(128) NOT NULL DEFAULT '',
  `gid` char(255) NOT NULL DEFAULT '',
  `name` char(20) NOT NULL DEFAULT '',
  `age` int(11) unsigned NOT NULL DEFAULT '0',
  `image` longblob NOT NULL,
  `updatedon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `eik` bigint(11) unsigned NOT NULL DEFAULT '1',
  `me` int(2) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `gid` (`gid`,`application_id`,`account_id`),
  KEY `dev_id` (`dev_id`),
  KEY `account_id` (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=35796304 DEFAULT CHARSET=utf8


mysql> SELECT gid, application_id, account_id FROM big_test WHERE account_id=14811 AND gid='TEST773475616236';
+-------------------------------------------------------------+----------------+------------+
| gid              | application_id | account_id |
+-------------------------------------------------------------+----------------+------------+
| TEST773475616236 |           1655 |      14811 |
| TEST773475616236 |           1655 |      14811 |
+-------------------------------------------------------------+----------------+------------+
2 rows in set (0.00 sec)

mysql> SELECT gid, application_id, account_id FROM big_test WHERE account_id=14811 AND gid='TEST773475616236' AND application_id=1655;
+-------------------------------------------------------------+----------------+------------+
| gid              | application_id | account_id |
+-------------------------------------------------------------+----------------+------------+
| TEST773475616236 |           1655 |      14811 |
+-------------------------------------------------------------+----------------+------------+
1 row in set (0.00 sec)

mysql> SELECT gid, application_id, account_id FROM big_test WHERE account_id=14811 AND gid='TEST773475616236' AND application_id<>1655;
Empty set (0.00 sec)

mysql> 

用于插入/更新操作的所有查询都是“REPLACE”。 例如:

mysql> REPLACE INTO big_test SET gid='TEST773475616236', application_id=1655, account_id=14811, name='Charlie';

如何可能,UNIQUE KEY无效?

2 个答案:

答案 0 :(得分:0)

我不确定你正在运行的环境,但“NAME”是MySQL中的保留字。我在其他平台上遇到了问题,你需要回复它,或者在替换语句中将其更改为big_test.name。

-James

答案 1 :(得分:0)

与往常一样,问题不在于数据库本身,而在编程代码中。 在事务中执行了一个SQL语句,其中有一个用于停止检查唯一键的集合。真的很愚蠢,但回答问题,并解决了问题:)