如果SQL触发器与已存在的行类似,则不会插入行

时间:2017-10-25 18:18:50

标签: mysql sql triggers

我正在创建一个将在BEFORE中发生的触发器,并且该事件在phpMyAdmin中是INSERT。

IF(
    SELECT * FROM packageArchive
    WHERE EXISTS(
                  SELECT * FROM packageArchive WHERE 
                  packageArchive.item = NEW.item
                  AND packageArchive.pack = NEW.pack
                  AND packageArchive.weight = NEW.weight
                  ) THEN
-- Does nothing with this insert / Doesn't insert.

如果您有任何建议,我愿意尝试一下。

2 个答案:

答案 0 :(得分:1)

假设您确实在使用一个表,那么请使用唯一索引或约束:

as.vector

或者,您可能需要一个外键:

#DATA
set.seed(42)
m = set_units(matrix(rnorm(4), 2), m/s)
m
#Units: m/s
#           [,1]      [,2]
#[1,]  1.3709584 0.3631284
#[2,] -0.5646982 0.6328626

class(m)
#[1] "units"

foo = function(x){
    y = as.vector(x)
    dim(y) = dim(x)
    return(y)
}

foo(m)
#           [,1]      [,2]
#[1,]  1.3709584 0.3631284
#[2,] -0.5646982 0.6328626

class(foo(m))
#[1] "matrix"

foo(a)
# [1]  1  2  3  4  5  6  7  8  9 10

class(foo(a))
#[1] "integer"

答案 1 :(得分:1)

我在@Juan Carlos Oropeza@RuiDC以及@Drew的帮助下解决了这个问题 我的解决方案看起来像这样

IF(
    SELECT * FROM productsArchive WHERE 
    packageArchive.item = NEW.item
    AND packageArchive.pack = NEW.pack
    AND packageArchive.weight = NEW.weight
   ) THEN
signal sqlstate '45000' set message_text = 'My Error Message';
END IF;