This may be impossible, but I'd hoped to see if there is an approachable way to run an online replacement of a table with a view.
For online table restructuring like partitioning, etc. DBMS_REDEFINITION works great. But I'd like to replace a table with a (materialized) view, so DBMS_REDEFINITION appears to be unsuitable.
I have no constraints, dependencies or mutating dml, etc. to worry about during the rename; I would only like to keep the target SELECTable when replacing the table with a view. A trumped-up example is below.
CREATE TABLE SCI_FI_MOVIE (
SCI_FI_MOVIE_ID NUMBER(10, 0) NOT NULL PRIMARY KEY,
NAME VARCHAR2(100) UNIQUE NOT NULL,
DIRECTOR VARCHAR2(100) NOT NULL,
REVIEW_SCORE NUMBER(1, 0) CHECK ( REVIEW_SCORE IN (1, 2, 3, 4, 5))
);
CREATE TABLE NO_SCORES_SCI_FI_MOVIE (
SCI_FI_MOVIE_ID NUMBER(10, 0) NOT NULL PRIMARY KEY,
NAME VARCHAR2(100) UNIQUE NOT NULL,
DIRECTOR VARCHAR2(100) NOT NULL
);
CREATE MATERIALIZED VIEW KUBRICK_SABOTAGE
(SCI_FI_MOVIE_ID, NAME, DIRECTOR, REVIEW_SCORE)
REFRESH COMPLETE ON COMMIT
AS
SELECT
SCI_FI_MOVIE_ID,
NAME,
DIRECTOR,
CASE WHEN DIRECTOR = 'KUBRICK'
THEN 5
ELSE 2 END AS REVIEW_SCORE
FROM NO_SCORES_SCI_FI_MOVIE;
INSERT INTO SCI_FI_MOVIE VALUES (1, 'Apollo 13', 'HOWARD', 5);
INSERT INTO SCI_FI_MOVIE VALUES (2, '2001: A Space Odyssey', 'KUBRICK', 4);
INSERT INTO NO_SCORES_SCI_FI_MOVIE VALUES (1, 'Apollo 13', 'HOWARD');
INSERT INTO NO_SCORES_SCI_FI_MOVIE VALUES (2, '2001: A Space Odyssey', 'KUBRICK');
COMMIT;
-- THEN WHAT STEPS TO REPLACE TABLE WITH VIEW?
In this example I'd like to end up with the MV named SCI_FI_MOVIE and the TABLE SCI_FI_MOVIE renamed to SCI_FI_MOVIE_TEMP or whatever pending its removal. There isn't any requirement for the MV to exist prior to replacing the original table, if the replacement can be done atomically
I'd like to avoid any interruption or compromise to object name resolution (CREATE PUBLIC SYNONYM
then renaming original won't work here)
Is there a clean no-downtime way to do this?
I am free to disable logging, read-only etc. anything as needed; the only goal is to prevent "ORA-00942: table or view does not exist" during the dictionary switch. I'm on 11gR2 but would welcome in 12c solutions as well.
Thanks so much for your thoughts