2012-08-01 102 views
0

我有一個表的MySQL對重複密鑰更新麻煩

CREATE TABLE IF NOT EXISTS `b_sale_order_props_value` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `ORDER_ID` int(11) NOT NULL, 
    `ORDER_PROPS_ID` int(11) DEFAULT NULL, 
    `NAME` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `VALUE` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `CODE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL, 
    PRIMARY KEY (`ID`), 
    UNIQUE KEY `IX_SOPV_ORD_PROP_UNI` (`ORDER_ID`,`ORDER_PROPS_ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1921 ; 

我做一個查詢

INSERT INTO `b_sale_order_props_value` 
    SET `ORDER_ID`=316, 
     `ORDER_PROPS_ID`=30, 
     `NAME`='ADDRES', 
     `VALUE`='Test addres' 
    ON DUPLICATE KEY UPDATE `NAME`='ADDRES',`VALUE`='Test addres'; 

返回一個錯誤

Duplicate entry '316-30' for key 'IX_SOPV_ORD_PROP_UNI' 

如何解決這個麻煩?

+1

無法重現:[sqlfiddle(http://sqlfiddle.com/#!2/88845/1/0) – lanzz 2012-08-01 14:15:52

回答

1

太大發布的評論 - 您的疑問在這裏工作:

mysql> use test; 
Reading table information for completion of table and column names 
You can turn off this feature to get a quicker startup with -A 

Database changed 
mysql> CREATE TABLE IF NOT EXISTS `b_sale_order_props_value` (
    -> `ID` int(11) NOT NULL AUTO_INCREMENT, 
    -> `ORDER_ID` int(11) NOT NULL, 
    -> `ORDER_PROPS_ID` int(11) DEFAULT NULL, 
    -> `NAME` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    -> `VALUE` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    -> `CODE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL, 
    -> PRIMARY KEY (`ID`), 
    -> UNIQUE KEY `IX_SOPV_ORD_PROP_UNI` (`ORDER_ID`,`ORDER_PROPS_ID`) 
    ->) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1921 ; 
Query OK, 0 rows affected (0.11 sec) 

mysql> insert into b_sale_order_props_value (ORDER_ID, ORDER_PROPS_ID, NAME, VALUE, `CODE`) values (316, 30, 'replaceme', 'replaceme', 'replaceme'); 
Query OK, 1 row affected (0.42 sec) 

mysql> INSERT INTO `b_sale_order_props_value` 
    ->  SET `ORDER_ID`=316, 
    ->   `ORDER_PROPS_ID`=30, 
    ->   `NAME`='ADDRES', 
    ->   `VALUE`='Test addres' 
    ->  ON DUPLICATE KEY UPDATE `NAME`='ADDRES',`VALUE`='Test addres'; 
Query OK, 2 rows affected (0.04 sec) 

mysql> select * from b_sale_order_props_value; 
+------+----------+----------------+--------+-------------+-----------+ 
| ID | ORDER_ID | ORDER_PROPS_ID | NAME | VALUE  | CODE  | 
+------+----------+----------------+--------+-------------+-----------+ 
| 1921 |  316 |    30 | ADDRES | Test addres | replaceme | 
+------+----------+----------------+--------+-------------+-----------+ 
1 row in set (0.00 sec) 
+0

謝謝!我發現一個錯誤。我在這個表中使用觸發器,在那裏我不使用重複鍵更新 – user1568812 2012-08-01 14:46:06