使用复合键从表中删除行

时间:2014-01-02 20:21:11

标签: mysql sql

我在Windows Server 2008 R2上运行MySQL Server 5.6。我有以下带有复合主键的InnoDB表:

CREATE TABLE `myschema`.`mytable` (
    `id_1` TINYINT UNSIGNED NOT NULL,
    `id_2` SMALLINT UNSIGNED NOT NULL
     PRIMARY KEY (`id_1`, `id_2`));

我正在尝试编写一个将删除此表中的行的SQL语句。我试图在MySQL Workbench中执行以下语句:

DELETE FROM `mytable` WHERE `id_1`=12 AND `id_2`=238;
DELETE FROM `mytable` WHERE `id_1`='12' AND `id_2`='238';
DELETE FROM `mytable` WHERE ((`id_1`=12) AND (`id_2`=238));
DELETE FROM `mytable` WHERE ((`id_1`=CAST(12 AS UNSIGNED)) AND (`id_2`=CAST(238 AS UNSIGNED)));
DELETE FROM `mytable` WHERE ((`id_1`=CAST('12' AS UNSIGNED)) AND (`id_2`=CAST('238' AS UNSIGNED)));
DELETE FROM `mytable` WHERE ((`id_1`='12') AND (`id_2`='238'));

DELETE FROM `mytable` WHERE (`id_1`, `id_2`) IN (12, 238);
DELETE FROM `mytable` WHERE (`id_1`, `id_2`) IN (CAST(12 AS UNSIGNED), CAST(238 AS UNSIGNED));
DELETE FROM `mytable` WHERE (`id_1`, `id_2`) IN (CAST('12' AS UNSIGNED), CAST('238' AS UNSIGNED));
DELETE FROM `mytable` WHERE (`id_1`, `id_2`) IN ('12', '238');

并且在每种情况下,我都会收到以下错误:

Error Code: 1292. Truncated incorrect DOUBLE value: ' AND '

有谁知道我做错了什么?

更新

问题是由于我没有意识到的BEFORE DELETE触发器有缺陷引起的。删除触发器后,所有DELETE FROM语句都正常工作。这非常令人沮丧,因为错误消息没有表明问题是在触发器中发生的。感谢大家的投入!

1 个答案:

答案 0 :(得分:2)

我很确定你的括号有问题。如果有效,请告诉我

DELETE FROM `mytable` WHERE `id_1`=12 AND `id_2`=238;

或潜在的MySQL抱怨使用整数(它不应该是)

DELETE FROM `mytable` WHERE `id_1`='12' AND `id_2`='238';