2017-03-22 48 views
0

我有一個錯字視圖編輯列:我如何能在現有的視圖

CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id, 
     name, 
     decode(WRONG_COLUMN, 1, 'T', 0, 'F') AS problem 
FROM "USER"."TEST"); 

對於使用

CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id, 
     name, 
     decode(RIGHT_COLUMN, 1, 'T', 0, 'F') AS problem 
FROM "USER"."TEST"); 

結果下面的消息在一些原因:

 
Error starting at line : 5 in command - 
CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id, 
     name, 
     decode(RIGHT_COLUMN, 1, 'T', 0, 'F') AS problem 
FROM "USER"."TEST") 
Error report - 
SQL Error: ORA-02449: unique/primary keys in table referenced by foreign keys 
02449. 00000 - "unique/primary keys in table referenced by foreign keys" 
*Cause: An attempt was made to drop a table with unique or 
      primary keys referenced by foreign keys in another table. 
*Action: Before performing the above operations the table, drop the 
      foreign key constraints in other tables. You can see what 
      constraints are referencing a table by issuing the following 
      command: 
      SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam"; 

我甚至不知道,一個觀點是一個參考的有效目標。因此,這裏的問題:

SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "USER"."VW_X"; 

回報什麼,這一點:

SELECT a.table_name, 
    a.column_name, 
    a.constraint_name, 
    c.owner, 
    c.r_owner, 
    c_pk.table_name r_table_name, 
    c_pk.constraint_name r_pk, 
    c.status 
FROM all_cons_columns a 
JOIN all_constraints c 
ON a.owner = c.owner AND a.constraint_name = c.constraint_name 
JOIN all_constraints c_pk 
ON c.r_owner = c_pk.owner AND c.r_constraint_name = c_pk.constraint_name 
WHERE c_pk.table_name = 'VW_X' or c.table_name = 'VW_X'; 

(一個在頂部答案this後變體) 返回所有這四個約束與'DISABLED'狀態。

我特地到ALTER VIEW(這個名字聽起來相當有希望的),但它似乎沒有什麼我要找的。

有什麼建議嗎?

注:這是可能可能有在所提供的示例代碼中的一些語法錯誤。這些都是樣機,在簡單的利益,出於職業偏執,大約有十幾個不相關的列已被刪除,名稱已被更改。

編輯: 這是Oracle11g的

編輯: 我找到的解決方案是

-- drop view. 
DROP VIEW "USER"."VW_X" CASCADE CONSTRAINTS; 
commit; 

-- create view. 
CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id, 
     name, 
     decode(RIGHT_COLUMN, 1, 'T', 0, 'F') AS problem 
FROM "USER"."TEST"); 
commit; 

CASCADE CONSTRAINTS似乎是缺少的環節。我很確定這些限制已經消失,但我並不特別在意。

+0

我沒有用FK對意見的解決方案。但是,看起來行爲與表格類似。你將需要確定FK約束你的觀點,刪除它們,改變你的看法,然後重新創建約束。您將無法在單個alter view語句中執行此操作。 – unleashed

回答

0
ALTER VIEW 
AS 
SELECT 
[new column name] = wrongcolumn 
+2

儘管此代碼可以回答這個問題,提供了關於如何和/或爲什麼它解決了這個問題將改善答案的長期價值附加的上下文。 –

+0

如果我正確地讀這篇文章,它看起來像你可能有這樣的倒退, ALTER VIEW AS 選擇 問題= rightcolumn 將這項工作? – chrisgotter

+0

?你的意思是'ALTER VIEW VW_X AS'? – chrisgotter

0

這裏是會下降的FK,創建視圖,然後重新創建FK

-- create view with unique constraint 
    create or replace view v_foo 
    (id unique disable novalidate, val) 
    as 
    select id, val from foo; 

    -- create second view 
    create or replace view v_bar 
    (id, val) 
    as 
    (select id, val from bar); 

    -- add FK to second view 
    alter view v_bar 
    add constraint v_bar_ref 
    foreign key (id) references v_foo(id) 
    disable novalidate; 


    -- add a column, this fails b/c of FK constraint 
    create or replace view v_foo 
    (id unique disable novalidate, val, dummy) 
    as 
    select id, val, 1 from foo; 

    -- remove constraint 
    alter view v_bar 
    drop constraint v_bar_ref; 

    -- make changes to view 
    create or replace view v_foo 
    (id unique disable novalidate, val, dummy) 
    as 
    select id, val, 1 from foo; 

    -- recreate FK constraint 
    alter view v_bar 
    add constraint v_bar_ref 
    foreign key (id) references v_foo(id) 
    disable novalidate; 
+0

這似乎是一個很好的解決方案,但其中一個問題是我無法弄清楚哪個fk是它的甚至在談論。這就是說,我最終找到了一個更容易接受的解決方案。 – chrisgotter

+0

@chrisgotter你最終做了什麼? – unleashed

+0

看到我的第二次編輯 – chrisgotter

相關問題