Oracle避免重复键异常

时间:2014-02-26 14:08:33

标签: oracle primary-key duplicate-data

我有一个带有正则表达式的查询,它接受一个字符串并拆分要插入表中的单词。该表只有2列(TWEET和PALABRA),它们都构成了主键。

查询是这样的:

INSERT INTO TWEET_PALABRAS (TWEET, PALABRA)
  SELECT 
         NEW_TWEET,
         TRANSLATE(regexp_substr(NEW_TITULO,
                                '[^[:punct:]|[:space:]]+',
                                 1,
                                 level),
                  'áéíóúÁÉÍÓÚ',
                  'aeiouAEIOU')
  FROM DUAL 
  where LENGTH(regexp_substr(NEW_TITULO, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
  connect by regexp_substr(NEW_TITULO, '[^[:punct:]|[:space:]]+', 1, level) is not null;

如果在推文中重复了一个单词,我将有一个重复键异常,避免这种情况的正确方法是添加ON DUPLICATE KEY UPDATE子句,但这会导致语法错误:

INSERT INTO TWEET_PALABRAS (TWEET, PALABRA)
  SELECT 
        NEW_TWEET,
        TRANSLATE(regexp_substr(NEW_TITULO,
                                '[^[:punct:]|[:space:]]+',
                                1,
                                level), 
                  'áéíóúÁÉÍÓÚ',
                  'aeiouAEIOU')
   FROM DUAL 
  where LENGTH(regexp_substr(NEW_TITULO, '[^[:punct:]|[:space:]]+', 1, level)) >= 5
  connect by regexp_substr(NEW_TITULO, '[^[:punct:]|[:space:]]+', 1, level) is not null;
ON DUPLICATE KEY UPDATE TWEET=NEW_TWEET;

我不需要在重复键上更新任何内容,只是为了避免异常,而且,如果发生重复键,则还应插入重复键之后的nexts字。此代码位于触发器内部,在此触发器之后有更多操作(因此异常块不是解决方案)。

谢谢

1 个答案:

答案 0 :(得分:1)

只需在查询中添加DISTINCT即可避免重复:

INSERT INTO TWEET_PALABRAS
  (TWEET, PALABRA)
  SELECT DISTINCT NEW_TWEET,
                  TRANSLATE(regexp_substr(NEW_TITULO,
                                          '[^[:punct:]|[:space:]]+',
                                          1,
                                          level),
                            'áéíóúÁÉÍÓÚ',
                            'aeiouAEIOU')
    FROM DUAL
   where LENGTH(regexp_substr(NEW_TITULO,
                              '[^[:punct:]|[:space:]]+',
                              1,
                              level)) >= 5
  connect by regexp_substr(NEW_TITULO, '[^[:punct:]|[:space:]]+', 1, level) is not null;