Sql 如何在现有视图中编辑列
我有一个输入错误的视图: 创建或替换VIEW USER.VW_X作为 选择id, 名称 解码错误的_列,1,'T',0,'F'作为问题 来自USER.TEST; 出于某种原因使用 创建或替换VIEW USER.VW_X作为 选择id, 名称 解码右_列,1,'T',0,'F'作为问题 来自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"; 不返回任何内容,这是: 选择一个表名称, a、 列名称, a、 你的名字, c、 所有者, c、 业主, c_pk.table_name r_table_name, c_pk.constraint_name r_pk, c、 地位 从所有列a 将所有约束连接到c 在a.owner=c.owner和a.constraint\u name=c.constraint\u name上 加入所有约束c_pk 在c.r\u owner=c\u pk.owner和c.r\u constraint\u name=c\u pk.constraint\u name上 其中c_pk.table_name='VW_X'或c.table_name='VW_X'; 顶部答案上的一个变体 返回四个约束,所有约束的状态均为“已禁用” 我研究过这个名字,听起来很有希望,但它似乎不是我想要的 有什么建议吗 注意:所提供的示例代码中可能存在一些语法错误。这些都是模型,为了简单起见,出于专业的偏执,大约有十几个不相关的栏目被删除了,名字也被修改了 编辑: 这是Oracle11g 编辑: 我找到的解决办法是 -下拉视图。 下拉视图USER.VW_X级联约束; 犯罪 -创建视图。 创建或替换VIEW USER.VW_X作为 选择id, 名称 解码右_列,1,'T',0,'F'作为问题 来自USER.TEST; 犯罪Sql 如何在现有视图中编辑列,sql,oracle11g,sql-view,Sql,Oracle11g,Sql View,我有一个输入错误的视图: 创建或替换VIEW USER.VW_X作为 选择id, 名称 解码错误的_列,1,'T',0,'F'作为问题 来自USER.TEST; 出于某种原因使用 创建或替换VIEW USER.VW_X作为 选择id, 名称 解码右_列,1,'T',0,'F'作为问题 来自USER.TEST; 结果显示以下消息: Error starting at line : 5 in command - CREATE OR REPLACE VIEW "USER"."VW_X" AS ( SE
级联约束似乎是缺少的链接。我很确定约束丢失了,但我并不特别在意。这里有一个解决方案,可以删除FK,创建视图,然后重新创建FK
ALTER VIEW
AS
SELECT
[new column name] = wrongcolumn
-- 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;
虽然这段代码可能会回答这个问题,但提供关于它如何和/或为什么解决问题的附加上下文将提高答案的长期价值。如果我读对了这段代码,您可能会有相反的看法,ALTER VIEW AS SELECT problem=rightcolumn这行吗?你是说ALTER VIEW VW_X AS吗?另外,我正在使用Oracle11g,我90%确定这不受支持OK,这是MS SQLI在视图上没有使用FK。但是,它的行为看起来类似于表。您需要识别视图上的FK约束,删除它们,更改视图,然后重新创建约束。您无法在单个alter view语句中执行此操作。这似乎是一个很好的解决方案,但问题之一是我无法确定它所谈论的fk。也就是说,我最终找到了一个更容易接受的解决方案。@chrisgotter你最终做了什么?