[20111221]关于11G Virtual columns.txt

11G以前Virtual columns的建立是通过建立函数索引的方式。而且在11G以前Virtual columns是"隐藏"的,11G以后可以定义以及显示在表中定义:

做一个测试,以scott的emp表为例子说明:

1. 10G下的测试:

SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0      Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production

SQL> create index if_ename on emp(lower(ename));
Index created.
SQL> create index if_ename1 on emp(empno,upper(ename));
Index created.

SQL> column column_name format a20
SQL> column data_type format a10
SQL> column data_default format a20
SQL> SELECT   column_name, data_type, column_id, data_default, num_distinct FROM dba_tab_cols
WHERE table_name = 'EMP' AND wner = 'SCOTT' ORDER BY 3;

COLUMN_NAME          DATA_TYPE   COLUMN_ID DATA_DEFAULT         NUM_DISTINCT
-------------------- ---------- ---------- -------------------- ------------
EMPNO                NUMBER              1                                14
ENAME                VARCHAR2            2                                14
JOB                  VARCHAR2            3                                 5
MGR                  NUMBER              4                                 6
HIREDATE             DATE                5                                13
SAL                  NUMBER              6                                12
COMM                 NUMBER              7                                 4
DEPTNO               NUMBER              8                                 3
SYS_NC00010$         VARCHAR2              UPPER("ENAME")
SYS_NC00009$         VARCHAR2              LOWER("ENAME")

10 rows selected.

select * from user_ind_expressions where table_name='EMP';
INDEX_NAME   TABLE_NAME  COLUMN_EXPRESSION   COLUMN_POSITION
------------ ----------- ------------------- ---------------
IF_ENAME     EMP         LOWER("ENAME")                    1
IF_ENAME1    EMP         UPPER("ENAME")                    2

可以发现建立两个虚拟列,SYS_NC00009$=>LOWER("ENAME"),SYS_NC00010$=>UPPER("ENAME").并且建立函数索引时没有统计NUM_DISTINCT等信息。

看看是否可以显示:
SQL> select SYS_NC00009$,SYS_NC00010$,ename from emp where empno= 7369;

SYS_NC0000 SYS_NC0001 ENAME
---------- ---------- ----------
smith      SMITH      SMITH

    可以发现在10g下,这些列是可以显示的,而且一个现实的问题就是SYS_NC00009$这样的字段难以记忆,看看是否可以改名呢!

SQL> alter table emp rename column  SYS_NC00009$ to lowerename;
Table altered.

    发现居然可以,但是如果接着执行,执行出错退出,即使不访问其他字段情况也一样,也许这个是bug,或者oracle当时没有做好:
SQL> select * from emp ;
select * from emp
                 *
ERROR at line 1:
ORA-03113: end-of-file on communication channel

SQL> select sal from emp ;
select sal from emp
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel

SQL> alter table emp rename column  lowerename to SYS_NC00009$;
Table altered.
    
补充一点,如果要在SYS_NC00009$建立直方图,可以执行如下:

BEGIN
  SYS.DBMS_STATS.GATHER_TABLE_STATS (
      OwnName        => 'SCOTT'
     ,TabName        => 'EMP'
    ,Estimate_Percent  => SYS.DBMS_STATS.AUTO_SAMPLE_SIZE
    ,Method_Opt        => 'FOR COLUMNS SYS_NC00009$ SIZE 10 '
    ,Degree            => 4
    ,Cascade           => TRUE
    ,No_Invalidate     => TRUE);
END;
/

SQL> SELECT   column_name, data_type, column_id, data_default, num_distinct,histogram FROM dba_tab_cols
WHERE table_name = 'EMP' AND wner = 'SCOTT' ORDER BY 3;

COLUMN_NAME          DATA_TYPE   COLUMN_ID DATA_DEFAULT         NUM_DISTINCT HISTOGRAM
-------------------- ---------- ---------- -------------------- ------------ ---------------
EMPNO                NUMBER              1                                14 NONE
ENAME                VARCHAR2            2                                14 NONE
JOB                  VARCHAR2            3                                 5 NONE
MGR                  NUMBER              4                                 6 NONE
HIREDATE             DATE                5                                13 NONE
SAL                  NUMBER              6                                12 NONE
COMM                 NUMBER              7                                 4 NONE
DEPTNO               NUMBER              8                                 3 NONE
SYS_NC00010$         VARCHAR2              UPPER("ENAME")                    NONE
SYS_NC00009$         VARCHAR2              LOWER("ENAME")                 14 HEIGHT BALANCED
10 rows selected.

2.11G的测试:
SQL> alter table emp add (upperename as (upper(ename)));
Table altered.
SQL>  create index if_ename on emp(lower(ename));
Index created.

SQL> column column_name format a20
SQL> column data_type format a10
SQL> column data_default format a20
SQL> SELECT   column_name, data_type, column_id, data_default, num_distinct,histogram FROM dba_tab_cols
WHERE table_name = 'EMP' AND wner = 'SCOTT' ORDER BY 3;

COLUMN_NAME          DATA_TYPE   COLUMN_ID DATA_DEFAULT         NUM_DISTINCT HISTOGRAM
-------------------- ---------- ---------- -------------------- ------------ ---------------
EMPNO                NUMBER              1                                14 NONE
ENAME                VARCHAR2            2                                14 NONE
JOB                  VARCHAR2            3                                 5 NONE
MGR                  NUMBER              4                                 6 NONE
HIREDATE             DATE                5                                13 NONE
SAL                  NUMBER              6                                12 NONE
COMM                 NUMBER              7                                 4 NONE
DEPTNO               NUMBER              8                                 3 NONE
UPPERENAME           VARCHAR2            9 UPPER("ENAME")                    NONE
SYS_NC00010$         VARCHAR2              LOWER("ENAME")                    NONE

10 rows selected.
--注意upperename的column_id=9,而SYS_NC00010$的column_id=NULL.

执行select * from emp;就很容易明白,会显示upperename字段。

SQL> select upperename,SYS_NC00010$,ename from emp where empno= 7369;

UPPERENAME SYS_NC0001 ENAME
---------- ---------- ----------
SMITH      smith      SMITH

看看是否可以改名:
SQL> alter table emp rename column   SYS_NC00010$ to lowerename;
Table altered.

SQL> select upperename,lowerename,ename from emp where empno= 7369;

UPPERENAME LOWERENAME ENAME
---------- ---------- ----------
SMITH      smith      SMITH

正常!

 

时间: 2024-10-02 18:37:56

[20111221]关于11G Virtual columns.txt的相关文章

[20120903]关于Virtual index.txt

[20120903]关于Virtual index.txt         virtual index没有segment,如何去产生该虚拟索引的统计信息,如何保证CBO的有效判断. 做一个测试与学习看看: 1.测试环境: SQL> select * from v$version ; BANNER ------------------------------------------------------------------------------ Oracle Database 11g En

[20151221]11g设置db_flash_cache_file.txt

[20151221]11g设置db_flash_cache_file.txt --11GR2下可以设置db_flash_cache_file与db_flash_cache_size参数,提高数据库的性能. --我曾经通过例子,快速建立测试数据库,在里面使用ram盘,今天测试使用该参数看看. http://blog.itpub.net/267265/viewspace-1845062/ 1.环境: SYS@book> @ &r/ver1 PORT_STRING                 

[20170628]11g修改用户名.txt

[20170628]11g修改用户名.txt --//昨天看了链接,提到修改用户名: http://www.oratea.com/2017/06/26/oracle-11g%e4%bf%ae%e6%94%b9%e7%94%a8%e6%88%b7%e5%90%8d/ --//自己也测试看看. 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING         VERSION    BANNER ------------------- ---------- -----

[20170313]11G use_large_pages参数.txt

[20170313]11G use_large_pages参数.txt --11G 增加参数use_large_pages,可以灵活使用hugepages. 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- --------------------------------------------

[20141111]11G redo暴涨.txt

[20141111]11G redo暴涨.txt --新上线生产系统日志出现暴涨,当天达到了400G,正常业务每天7G上下,导致dataguard,备份出现问题,磁盘空间不足 --自己做一些调查: SYS@xxxx> @ ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- -----------------------------------

[20130721]ORACLE 12C Invisible Columns.txt

[20130721]ORACLE 12C Invisible Columns.txt 在ORACLE 12C中有了Invisible Columns的概念,就是在表中真实的存在该列,但是通过设置Invisible可以实现不显示该列.自己测试看看. SQL> select banner from v$version ; BANNER -------------------------------------------------------------------------------- Or

解析Oracle 11g:提升管理功能 重用XML

Oracle最新的数据库产品在去年年底开始进入Beta版状态,在最初公布的测试版当中,Oracle发布了数百项功能改进.作为目前全球市场占有率最大的数据库产品供应商之一,Oracle的新版产品足可以用万众瞩目来形容,目前可以确定的是,这个产品的命名将是Oracle 11g.除了版本的更新之外,新的Oracle数据库仍将使用g作为后缀,以代表这是一个包含了网格技术基础的数据库软件.根据以往几个版本的经验,Oracle发布新版数据库的频率在3年左右,所以Oracle 11g极有可能在2007年年底之

[20141029]10g和11G视图DBA_CONSTRAINTS

[20141029]10g和11G视图DBA_CONSTRAINTS.txt --上午同事讲一下表从10g导入11g时,一些约束没有导入,感觉不可能出现这种情况,我在10g下查看一些约束的状态是是disabled,但是 --type显示的是问号(在toad下).想起来我以前遇到的问题, --[20140131]toad看constraints的问题.txt http://blog.itpub.net/267265/viewspace-1076597/ --很容易明白这些表是没有主键,不过在11g

20160822Oracle 11g Temporary Tablespace

[20160822]Oracle 11g Temporary Tablespace.txt 1.11G改进了Temporary Tablespace管理,可以回收临时表空间. alter tablespace temp shrink space; alter tablespace temp shrink space keep 10m; --也可以单独回收一个临时表空间数据文件. alter tablespace temp shrink tempfile  '/u01/app/Oracle/ora