Oracle

更改列類型 - 視圖不會更改

  • August 4, 2016

我注意到,當我更改基礎表的數據類型時,視圖列數據類型不會改變。我需要採取任何額外的步驟嗎?

這是一個例子:

CREATE TABLE test ( amount NUMBER(10,2) );
CREATE VIEW test_vw AS SELECT * FROM test;

SELECT table_name, column_name, data_type, data_precision, data_scale 
FROM user_tab_columns 
WHERE table_name = 'TEST_VW';

ALTER TABLE test MODIFY ( amount  NUMBER(19,2) );

SELECT table_name, column_name, data_type, data_precision, data_scale 
FROM user_tab_columns 
WHERE table_name = 'TEST_VW';

兩個查詢返回相同的結果:

TABLE_NAME COLUMN_NAME DATA_TYPE DATA_PRECISION DATA_SCALE
---------- ----------- --------- -------------- ----------
TEST_VW    AMOUNT      NUMBER                10          2 

依賴對像在其父對象更改時失效:

select status from user_objects where object_name = 'TEST_VW';
-------    
INVALID

下次使用時會自動驗證無效對象:

select * from test_vw;

SELECT table_name, column_name, data_type, data_precision, data_scale 
FROM user_tab_columns 
WHERE table_name = 'TEST_VW';

TABLE_NAME COLUMN_NAME DATA_TYPE DATA_PRECISION DATA_SCALE
---------- ----------- --------- -------------- ----------
TEST_VW    AMOUNT      NUMBER                19          2 

或者,您可以自己重新編譯對象:

alter view test_vw compile;

值得注意的是,修改表後,但在重新驗證之前,沒有數據或精度失去:

SELECT table_name, column_name, data_type, data_precision, data_scale 
FROM user_tab_columns 
WHERE table_name = 'TEST_VW';

TABLE_NAME COLUMN_NAME DATA_TYPE DATA_PRECISION DATA_SCALE
---------- ----------- --------- -------------- ----------
TEST_VW    AMOUNT      NUMBER                10          2 

insert into test (amount) values (123456789012345);

select status from user_objects where object_name = 'TEST_VW';
-------    
INVALID

select * from test_vw;
---------------
123456789012345

select status from user_objects where object_name = 'TEST_VW';
-------    
VALID

引用自:https://dba.stackexchange.com/questions/145928