如何在创建视图时避免“唯一约束”错误消息

时间:2017-04-07 11:35:24

标签: sql oracle view

我有一个普通的View和iam收到错误消息:

[Error] Execution (6: 83): ORA-00604: error occurred at recursive SQL level 1
ORA-00001: unique constraint (SYS.I_COL1) violated

但我不明白我做错了什么。它表示bacl.Descriptionbatl.Descriptionbagl.description违反了唯一性约束。

    CREATE OR REPLACE FORCE VIEW CCI.VW_TA04_BAC_PAGE_4_2 as
   SELECT 
          bac.id, 
         bac.code code, 
         bacl.DESCRIPTION, 
         bac.order_key, 
        bac.bat_id,  
        batl.description,  
        bac.bag_id,  
        bagl.description,  
        bac.weight_factor,  
        bac.display
     FROM BART_CATEGORIES bac,
          BART_CATEGORIES_LAE bacl,
          BART_CATEGORY_GROUPS bag,
          BART_CATEGORY_GROUPS_LAE bagl,
          BART_CATEGORY_TYPES bat,
          BART_CATEGORY_TYPES_LAE batl
    WHERE bacl.lae_id = pkg_process.language
    AND batl.lae_id = pkg_process.language
    AND bagl.lae_id = pkg_process.language

    AND (bac.bag_id = bag.id)
    AND (bac.bat_id = bat.id)     

    AND (bacl.BAC_ID = bac.id)
    AND (bagl.BAG_ID = bag.id)
    AND (batl.BAT_ID = bat.id)

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

  

“它说bacl.Description,batl.Description,bagl.description违反了唯一性约束”

您的视图包含来自三个不同表的同名列。列名在视图中必须是唯一的。所以你需要别名那些列。例如,这可以解决问题:

CREATE OR REPLACE FORCE VIEW CCI.VW_TA04_BAC_PAGE_4_2 as
SELECT 
      bac.id, 
     bac.code code, 
     bacl.DESCRIPTION as bac_description, 
     bac.order_key, 
    bac.bat_id,  
    batl.description as bat_description,  
    bac.bag_id,  
    bagl.description  as bag_description,  
    bac.weight_factor,  
    bac.display
  

“我想在前面写”bacl“或者就足够了。”

我们都需要。表别名告诉SQL引擎哪个表提供了引用的值,但它不是列名的一部分。