怎么插入没有重复?

时间:2017-07-06 13:53:42

标签: mysql

我正在尝试在我的产品表中插入一些记录,我需要插入没有重复模型代码的产品,我使用查询:

INSERT IGNORE INTO erp_product
  (erp_model, erp_ean, erp_cstid, erp_quantity,
  erp_brand_erp_brandid, erp_price, erp_cost, 
  erp_weight, erp_lenght, erp_width, erp_height,
  erp_status, erp_distributor, erp_registerdate)
VALUES(
       '020-113_24','7897712061589',1,0,
       '1','22.85','17.58',
       '0.03','13','14','5',
       0,'OP','06/07/2017 10:32:47');

我需要使用WHERE NOT INWHERE NOT EXISTS吗?有什么不同? 插入不起作用,有什么建议吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

  

我需要插入没有重复模型代码的产品

那么为什么不在UNIQUE列上定义erp_model约束呢?像

ALTER TABLE erp_product ADD CONSTRAINT constr_unq UNIQUE (erp_model)