I have a view with a typo:
CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id,
name,
decode(WRONG_COLUMN, 1, 'T', 0, 'F') AS problem
FROM "USER"."TEST");
For some reason using
CREATE OR REPLACE VIEW "USER"."VW_X" AS (
SELECT id,
name,
decode(RIGHT_COLUMN, 1, 'T', 0, 'F') AS problem
FROM "USER"."TEST");
results in the following message:
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";
I didn't even know that a view was a valid target for a reference. So here's the problem:
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "USER"."VW_X";
returns nothing, this:
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';
(a variant on the top answer to this post)
returns four constraints all of which with a status of 'DISABLED'
.
I have looked into ALTER VIEW
(the name sounded pretty promising), but it does not appear to be what I'm looking for.
Any suggestions?
NOTE: It is possible probable that there are some syntax errors in the provided sample code. These are mockups, in the interest of simplicity and out of professional paranoia, about a dozen irrelevant columns have been removed and names have been altered.
EDIT: this is Oracle11g
EDIT: The solution I found was
-- 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
appears to be the missing link. I am pretty sure the constraints are lost, but I don't particularly care.