我有一个错字的观点:
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 :(得分:0)
ALTER VIEW
AS
SELECT
[new column name] = wrongcolumn
答案 1 :(得分: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;