Oracle11G 虛擬列 Virtual Column使用
Oracle 11G 虛擬列 Virtual Column
Oracle 11G 在表中引入了虛擬列,虛擬列是一個表示式,在執行時計算,不儲存在資料庫中,不能更新虛擬列的值。
定義一個虛擬列的語法:
column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]
1.虛擬列可以用在select,update,delete語句的where條件中,但是不能用於DML語句
2.可以基於虛擬列來做分割槽
3. 可以在虛擬列上建索引,oracle的函式索引就類似。
4. 可以在虛擬列上建約束
建立一個帶虛擬列的表:
CREATE TABLE EMPLOYEE
(
empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*12 + bonus)
);
total_sal就是一個虛擬列
檢視下錶定義內容:
SELECT column_name, data_type, data_length, data_default, virtual_column
FROM user_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_DEFAULT | VIRTUAL_COLUMN
EMPL_ID | NUMBER | 22 | null | NO
EMPL_NM | VARCHAR2 | 50 | null | NO
MONTHLY_SAL | NUMBER | 22 | null | NO
BONUS | NUMBER | 22 | null | NO
TOTAL_SAL | NUMBER | 22 | "MONTHLY_SAL"*12+"BONUS" | YES
virtual_column是yes,表示是虛擬列,我們也可以在建表語句加上VIRTUAL,顯示宣告為虛擬列
DROP TABLE EMPLOYEE PURGE;
CREATE OR REPLACE FUNCTION get_empl_total_sal ( p_monthly_sal NUMBER,
p_bonus NUMBER)
RETURN NUMBER
DETERMINISTIC
IS
BEGIN
RETURN p_monthly_sal * 12 + p_bonus;
END;
CREATE TABLE EMPLOYEE
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (get_empl_total_sal(monthly_sal, bonus)) VIRTUAL
);
我們可以在虛擬列上建立索引
CREATE INDEX idx_total_sal ON employee(total_sal);
SELECT index_name, index_type
FROM user_indexes
WHERE table_name = 'EMPLOYEE';
INDEX_NAME INDEX_TYPE
IDX_TOTAL_SAL FUNCTION-BASED NORMAL
這個函式也是表定義內容
DROP FUNCTION get_empl_total_sal;
SELECT * FROM employee;
*
Error at line 0
ORA-00904: "schema"."GET_EMPL_TOTAL_SAL": invalid identifier
可以新增一個虛擬列
DROP TABLE EMPLOYEE PURGE;
CREATE TABLE EMPLOYEE
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2)
);
ALTER TABLE EMPLOYEE ADD (total_sal AS (monthly_sal * 12 + bonus));
這個新列的型別時不確定的,是根據表示式來推斷的。
INSERT INTO employee (empl_id, empl_nm, monthly_sal, bonus)
WITH DATA AS
(SELECT 100 empl_id, 'AAA' empl_nm, 20000 monthly_sal, 3000 bonus
FROM DUAL
UNION
SELECT 200, 'BBB', 12000, 2000
FROM DUAL
UNION
SELECT 300, 'CCC', 32100, 1000
FROM DUAL
UNION
SELECT 400, 'DDD', 24300, 5000
FROM DUAL
UNION
SELECT 500, 'EEE', 12300, 8000
FROM DUAL)
SELECT *
FROM DATA;
SELECT * FROM employee;
EMPL_ID | EMPL_NM | MONTHLY_SAL | BONUS | TOTAL_SAL
100 | AAA | 20000 | 3000 | 243000
200 | BBB | 12000 | 2000 | 146000
300 | CCC | 32100 | 1000 | 386200
400 | DDD | 24300 | 5000 | 296600
500 | EEE | 12300 | 8000 | 155600
在虛擬列上執行update語句是不允許的
UPDATE employee
SET total_sal = 2000;
ORA-54017: UPDATE operation disallowed on virtual columns
能夠從虛擬列上收集表的統計資訊
EXEC DBMS_STATS.GATHER_TABLE_STATS(user, 'EMPLOYEE');
SELECT column_name, num_distinct,
display_raw (low_value, data_type) low_value,
display_raw (high_value, data_type) high_value
FROM dba_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | NUM_DISTINCT | LOW_VALUE | HIGH_VALUE
TOTAL_SAL | 5 | 146000 | 386200
BONUS | 5 | 1000 | 8000
MONTHLY_SAL | 5 | 12000 | 32100
EMPL_NM | 5 | AAA | EEE
EMPL_ID | 5 | 100 | 500
基於虛擬列的分割槽
DROP TABLE EMPLOYEE PURGE;
CREATE TABLE employee
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (monthly_sal*12 + bonus)
)
PARTITION BY RANGE (total_sal)
(PARTITION sal_200000 VALUES LESS THAN (200000),
PARTITION sal_400000 VALUES LESS THAN (400000),
PARTITION sal_600000 VALUES LESS THAN (600000),
PARTITION sal_800000 VALUES LESS THAN (800000),
PARTITION sal_default VALUES LESS THAN (MAXVALUE));
INSERT INTO employee (empl_id, empl_nm, monthly_sal, bonus)
WITH DATA AS
(SELECT 100 empl_id, 'AAA' empl_nm, 20000 monthly_sal, 3000 bonus
FROM DUAL
UNION
SELECT 200, 'BBB', 12000, 2000
FROM DUAL
UNION
SELECT 300, 'CCC', 32100, 1000
FROM DUAL
UNION
SELECT 400, 'DDD', 24300, 5000
FROM DUAL
UNION
SELECT 500, 'EEE', 12300, 8000
FROM DUAL)
SELECT *
FROM DATA;
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'EMPLOYEE',granularity => 'PARTITION');
SELECT table_name, partition_name, num_rows
FROM user_tab_partitions
WHERE table_name = 'EMPLOYEE'
ORDER BY partition_name;
TABLE_NAME | PARTITION_NAME | NUM_ROWS
EMPLOYEE | SAL_200000 | 2
EMPLOYEE | SAL_400000 | 3
EMPLOYEE | SAL_600000 | 0
EMPLOYEE | SAL_800000 | 0
EMPLOYEE | SAL_DEFAULT | 0
在分割槽情況下,不能更新虛擬列引用的列
UPDATE employee
SET monthly_sal = 30000
WHERE empl_id = 500;
ORA-14402: updating partition key column would cause a partition change
如果在分割槽情況能夠更新,則需要設定ENABLE ROW MOVEMENT
ALTER TABLE employee ENABLE ROW MOVEMENT;
UPDATE employee
SET monthly_sal = 80000
WHERE empl_id = 500;
1 row updated.
不能基於函式表示式的虛擬列上的分割槽
CREATE TABLE employee_new
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (get_empl_total_sal(monthly_sal, bonus))
)
PARTITION BY RANGE (total_sal)
(PARTITION sal_200000 VALUES LESS THAN (200000),
PARTITION sal_400000 VALUES LESS THAN (400000),
PARTITION sal_600000 VALUES LESS THAN (600000),
PARTITION sal_800000 VALUES LESS THAN (800000),
PARTITION sal_default VALUES LESS THAN (MAXVALUE));
ORA-54021: Cannot use PL/SQL expressions in partitioning or subpartitioning columns
原文連結:http://blog.csdn.net/zhongweijian/article/details/9008067
Oracle 11G 在表中引入了虛擬列,虛擬列是一個表示式,在執行時計算,不儲存在資料庫中,不能更新虛擬列的值。
定義一個虛擬列的語法:
column_name [datatype] [GENERATED ALWAYS] AS [expression] [VIRTUAL]
1.虛擬列可以用在select,update,delete語句的where條件中,但是不能用於DML語句
2.可以基於虛擬列來做分割槽
3. 可以在虛擬列上建索引,oracle的函式索引就類似。
4. 可以在虛擬列上建約束
建立一個帶虛擬列的表:
CREATE TABLE EMPLOYEE
(
empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) GENERATED ALWAYS AS (monthly_sal*12 + bonus)
);
total_sal就是一個虛擬列
檢視下錶定義內容:
SELECT column_name, data_type, data_length, data_default, virtual_column
FROM user_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | DATA_TYPE | DATA_LENGTH | DATA_DEFAULT | VIRTUAL_COLUMN
EMPL_ID | NUMBER | 22 | null | NO
EMPL_NM | VARCHAR2 | 50 | null | NO
MONTHLY_SAL | NUMBER | 22 | null | NO
BONUS | NUMBER | 22 | null | NO
TOTAL_SAL | NUMBER | 22 | "MONTHLY_SAL"*12+"BONUS" | YES
virtual_column是yes,表示是虛擬列,我們也可以在建表語句加上VIRTUAL,顯示宣告為虛擬列
DROP TABLE EMPLOYEE PURGE;
CREATE OR REPLACE FUNCTION get_empl_total_sal ( p_monthly_sal NUMBER,
p_bonus NUMBER)
RETURN NUMBER
DETERMINISTIC
IS
BEGIN
RETURN p_monthly_sal * 12 + p_bonus;
END;
CREATE TABLE EMPLOYEE
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (get_empl_total_sal(monthly_sal, bonus)) VIRTUAL
);
我們可以在虛擬列上建立索引
CREATE INDEX idx_total_sal ON employee(total_sal);
SELECT index_name, index_type
FROM user_indexes
WHERE table_name = 'EMPLOYEE';
INDEX_NAME INDEX_TYPE
IDX_TOTAL_SAL FUNCTION-BASED NORMAL
這個函式也是表定義內容
DROP FUNCTION get_empl_total_sal;
SELECT * FROM employee;
*
Error at line 0
ORA-00904: "schema"."GET_EMPL_TOTAL_SAL": invalid identifier
可以新增一個虛擬列
DROP TABLE EMPLOYEE PURGE;
CREATE TABLE EMPLOYEE
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2)
);
ALTER TABLE EMPLOYEE ADD (total_sal AS (monthly_sal * 12 + bonus));
這個新列的型別時不確定的,是根據表示式來推斷的。
INSERT INTO employee (empl_id, empl_nm, monthly_sal, bonus)
WITH DATA AS
(SELECT 100 empl_id, 'AAA' empl_nm, 20000 monthly_sal, 3000 bonus
FROM DUAL
UNION
SELECT 200, 'BBB', 12000, 2000
FROM DUAL
UNION
SELECT 300, 'CCC', 32100, 1000
FROM DUAL
UNION
SELECT 400, 'DDD', 24300, 5000
FROM DUAL
UNION
SELECT 500, 'EEE', 12300, 8000
FROM DUAL)
SELECT *
FROM DATA;
SELECT * FROM employee;
EMPL_ID | EMPL_NM | MONTHLY_SAL | BONUS | TOTAL_SAL
100 | AAA | 20000 | 3000 | 243000
200 | BBB | 12000 | 2000 | 146000
300 | CCC | 32100 | 1000 | 386200
400 | DDD | 24300 | 5000 | 296600
500 | EEE | 12300 | 8000 | 155600
在虛擬列上執行update語句是不允許的
UPDATE employee
SET total_sal = 2000;
ORA-54017: UPDATE operation disallowed on virtual columns
能夠從虛擬列上收集表的統計資訊
EXEC DBMS_STATS.GATHER_TABLE_STATS(user, 'EMPLOYEE');
SELECT column_name, num_distinct,
display_raw (low_value, data_type) low_value,
display_raw (high_value, data_type) high_value
FROM dba_tab_cols
WHERE table_name = 'EMPLOYEE';
COLUMN_NAME | NUM_DISTINCT | LOW_VALUE | HIGH_VALUE
TOTAL_SAL | 5 | 146000 | 386200
BONUS | 5 | 1000 | 8000
MONTHLY_SAL | 5 | 12000 | 32100
EMPL_NM | 5 | AAA | EEE
EMPL_ID | 5 | 100 | 500
基於虛擬列的分割槽
DROP TABLE EMPLOYEE PURGE;
CREATE TABLE employee
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (monthly_sal*12 + bonus)
)
PARTITION BY RANGE (total_sal)
(PARTITION sal_200000 VALUES LESS THAN (200000),
PARTITION sal_400000 VALUES LESS THAN (400000),
PARTITION sal_600000 VALUES LESS THAN (600000),
PARTITION sal_800000 VALUES LESS THAN (800000),
PARTITION sal_default VALUES LESS THAN (MAXVALUE));
INSERT INTO employee (empl_id, empl_nm, monthly_sal, bonus)
WITH DATA AS
(SELECT 100 empl_id, 'AAA' empl_nm, 20000 monthly_sal, 3000 bonus
FROM DUAL
UNION
SELECT 200, 'BBB', 12000, 2000
FROM DUAL
UNION
SELECT 300, 'CCC', 32100, 1000
FROM DUAL
UNION
SELECT 400, 'DDD', 24300, 5000
FROM DUAL
UNION
SELECT 500, 'EEE', 12300, 8000
FROM DUAL)
SELECT *
FROM DATA;
EXEC DBMS_STATS.GATHER_TABLE_STATS(user,'EMPLOYEE',granularity => 'PARTITION');
SELECT table_name, partition_name, num_rows
FROM user_tab_partitions
WHERE table_name = 'EMPLOYEE'
ORDER BY partition_name;
TABLE_NAME | PARTITION_NAME | NUM_ROWS
EMPLOYEE | SAL_200000 | 2
EMPLOYEE | SAL_400000 | 3
EMPLOYEE | SAL_600000 | 0
EMPLOYEE | SAL_800000 | 0
EMPLOYEE | SAL_DEFAULT | 0
在分割槽情況下,不能更新虛擬列引用的列
UPDATE employee
SET monthly_sal = 30000
WHERE empl_id = 500;
ORA-14402: updating partition key column would cause a partition change
如果在分割槽情況能夠更新,則需要設定ENABLE ROW MOVEMENT
ALTER TABLE employee ENABLE ROW MOVEMENT;
UPDATE employee
SET monthly_sal = 80000
WHERE empl_id = 500;
1 row updated.
不能基於函式表示式的虛擬列上的分割槽
CREATE TABLE employee_new
(empl_id NUMBER,
empl_nm VARCHAR2(50),
monthly_sal NUMBER(10,2),
bonus NUMBER(10,2),
total_sal NUMBER(10,2) AS (get_empl_total_sal(monthly_sal, bonus))
)
PARTITION BY RANGE (total_sal)
(PARTITION sal_200000 VALUES LESS THAN (200000),
PARTITION sal_400000 VALUES LESS THAN (400000),
PARTITION sal_600000 VALUES LESS THAN (600000),
PARTITION sal_800000 VALUES LESS THAN (800000),
PARTITION sal_default VALUES LESS THAN (MAXVALUE));
ORA-54021: Cannot use PL/SQL expressions in partitioning or subpartitioning columns
原文連結:http://blog.csdn.net/zhongweijian/article/details/9008067
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29812844/viewspace-1878517/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【VIRTUAL COLUMN】Oracle 11g中的虛擬列技術Oracle
- oracle11g新特性之--虛擬列Oracle
- openNebula 運維繫列虛擬機器virtual machines operations運維虛擬機Mac
- 虛擬私有云(Virtual Private Cloud,VPC)Cloud
- [CareerCup] 13.3 Virtual Functions 虛擬函式Function函式
- Virtual Box 虛擬機器擴容虛擬機
- 介紹Oracle Virtual Index虛擬索引(上)OracleIndex索引
- Vue 為什麼要用虛擬 DOM(Virtual DOM)Vue
- C++中的虛擬函式(virtual function)C++函式Function
- MySQL虛擬列MySql
- How To Use Virtual Column-Based Partitioning
- 修改Virtual PC和VMWare虛擬機器BIOS虛擬機iOS
- 虛擬機器Virtual PC,Vmware裡安裝SoftICE虛擬機
- 虛擬機器管理器(Virtual Machine Manager)簡介虛擬機Mac
- Java 21 新特性:虛擬執行緒(Virtual Threads)Java執行緒thread
- 【C++基礎】純虛擬函式 - pure virtual functionC++函式Function
- 虛擬網路卡介面VETH(Virtual Ethernet )建立使用和繫結關係
- 虛擬機器 redhat 6.5 oracle11g RAC虛擬機RedhatOracle
- mysql 5.7 虛擬列功能MySql
- 虛擬歌姬列傳
- 虛擬機器和容器的對比 Virtual Server VS Docker虛擬機ServerDocker
- Azure Virtual Netwok(二)配置 ExpressRoute 虛擬網路閘道器Express
- Virtual Box 虛擬機器上安裝多個映象檔案虛擬機
- C++繼承三之純虛擬函式pure virtual functionC++繼承函式Function
- 虛擬機器無法開機提示:This virtual machine appears to be in use虛擬機MacAPP
- oracle 11g 虛擬列Oracle
- __cxa_pure_virtual報錯(g++編譯虛擬函式時)編譯函式
- Java21的虛擬執行緒Virtual Thread初體驗Java執行緒thread
- oracle11g alter table set unused column指定表某列不可用之系列一Oracle
- Oracle11新特性——虛擬列Oracle
- 虛擬機器上靜默安裝oracle11g rac虛擬機Oracle
- 利用虛擬列實現虛擬刪除的主外來鍵約束
- 內聯(inline)函式與虛擬函式(virtual)的討論inline函式
- Oracle中的虛擬列索引-nosegment indexOracle索引Index
- Oracle11新特性——虛擬列(二)Oracle
- Linux中Postfix虛擬使用者及虛擬域(六)Linux
- 虛擬機器下red hat 6.5 linux安裝oracle11g虛擬機LinuxOracle
- jvm HotSpot虛擬機器主要參數列JVMHotSpot虛擬機