分享

Oracle 12c使用初体验,分享如何让12c的varchar2类型支持32k字符以及top

 昵称10087950 2015-06-18
本帖最后由 luocs 于 2013-7-6 13:02 编辑

原文发布:LTB(个人技术博客)
原文地址:http://www./archives/815.html



安装Oracle 12c已经多日了,都没怎么尝试去使用。今天有时间,就在12c上尝试了下基础的一些东西。



下面就把这个实验过程分享一下。
  1. sys@LUOCS12C> select * from v$version;

  2. BANNER                                                                               CON_ID
  3. -------------------------------------------------------------------------------- ----------
  4. Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0
  5. PL/SQL Release 12.1.0.1.0 - Production                                                    0
  6. CORE    12.1.0.1.0      Production                                                                0
  7. TNS for Linux: Version 12.1.0.1.0 - Production                                            0
  8. NLSRTL Version 12.1.0.1.0 - Production                                                    0

  9. 创建一个表空间,大家想想该表空间创建于CDB还是PDB?
  10. sys@LUOCS12C> create tablespace luocs datafile size 100M autoextend on;

  11. Tablespace created.

  12. sys@LUOCS12C> select name from v$datafile;

  13. NAME
  14. ----------------------------------------------------------------------------------------------------
  15. +RACDATA/LUOCS12C/DATAFILE/system.261.819647163
  16. +RACDATA/LUOCS12C/DATAFILE/sysaux.269.819647079
  17. +RACDATA/LUOCS12C/DATAFILE/undotbs1.265.819647271
  18. +RACDATA/LUOCS12C/DD7C48AA5A4404A2E04325AAE80A403C/DATAFILE/system.267.819647373
  19. +RACDATA/LUOCS12C/DATAFILE/users.268.819647269
  20. +RACDATA/LUOCS12C/DD7C48AA5A4404A2E04325AAE80A403C/DATAFILE/sysaux.259.819647373
  21. +RACDATA/LUOCS12C/DATAFILE/undotbs2.257.819648905
  22. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/system.272.819650131
  23. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/sysaux.273.819650131
  24. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/users.275.819650827
  25. +RACDATA/LUOCS12C/DATAFILE/luocs.276.819903043

  26. 当我们在CDB中使用传统方式创建用户会遇到错误
  27. sys@LUOCS12C> create user luocs identified by oracle default tablespace luocs;
  28. create user luocs identified by oracle default tablespace luocs
  29.             *
  30. ERROR at line 1:
  31. ORA-65096: invalid common user or role name

  32. sys@LUOCS12C> !oerr ora 65096
  33. 65096, 00000, "invalid common user or role name"
  34. // *Cause:  An attempt was made to create a common user or role with a name
  35. //          that wass not valid for common users or roles.  In addition to
  36. //          the usual rules for user and role names, common user and role
  37. //          names must start with C## or c## and consist only of ASCII
  38. //          characters.
  39. // *Action: Specify a valid common user or role name.
  40. //
  41. 根据错误提示了解,在CDB中用户得以C##开头,如下:
  42. sys@LUOCS12C> create user c##luocs identified by oracle default tablespace luocs;

  43. User created.

  44. sys@LUOCS12C> col USERNAME for a10
  45. sys@LUOCS12C> col ACCOUNT_STATUS for a6
  46. sys@LUOCS12C> col LAST_LOGIN for a20
  47. sys@LUOCS12C> set line 150 pages 9999
  48. sys@LUOCS12C> select USERNAME, ACCOUNT_STATUS, PASSWORD_VERSIONS, LAST_LOGIN from dba_users where username='C##LUOCS';

  49. USERNAME   ACCOUN PASSWORD_VER LAST_LOGIN
  50. ---------- ------ ------------ --------------------
  51. C##LUOCS   OPEN   10G 11G

  52. sys@LUOCS12C> grant connect to c##luocs;

  53. Grant succeeded.

  54. sqlplus连接尝试:
  55. [oracle@12crac1 ~]$ sqlplus c##luocs/oracle

  56. SQL*Plus: Release 12.1.0.1.0 Production on Thu Jul 4 14:59:18 2013

  57. Copyright (c) 1982, 2013, Oracle.  All rights reserved.


  58. Connected to:
  59. Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
  60. With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
  61. Advanced Analytics and Real Application Testing options

  62. c##luocs@LUOCS12C> col USERNAME for a15
  63. c##luocs@LUOCS12C> col GRANTED_ROLE for a10
  64. c##luocs@LUOCS12C> select * from USER_ROLE_PRIVS;

  65. USERNAME        GRANTED_RO ADM DEF OS_ COM
  66. --------------- ---------- --- --- --- ---
  67. C##LUOCS        CONNECT    NO  YES NO  NO

  68. 查看下CONNECT角色有什么权限:
  69. sys@LUOCS12C> col GRANTEE for a20
  70. sys@LUOCS12C> select GRANTEE,PRIVILEGE  from dba_sys_privs where GRANTEE='CONNECT';

  71. GRANTEE              PRIVILEGE
  72. -------------------- ----------------------------------------
  73. CONNECT              SET CONTAINER
  74. CONNECT              CREATE SESSION

  75. 明显发现跟11g比较多了个SET CONTAINER的权限。
  76. 11g:
  77. SQL> select GRANTEE,PRIVILEGE  from dba_sys_privs where GRANTEE='CONNECT';

  78. GRANTEE              PRIVILEGE
  79. -------------------- --------------------
  80. CONNECT              CREATE SESSION
复制代码
关于CONTAINER容器这个概念,官方介绍:

The data dictionary in each container in a CDB is separate, and the current container is the container whose data dictionary is used for name resolution and for privilege authorization. The current container can be the root or a PDB. Each session has exactly one current container at any point in time, but it is possible for a session to switch from one container to another.

Each container has a unique ID and name in a CDB. You can use the CON_ID and CON_NAME parameters in the USERENV namespace to determine the current container ID and name with the SYS_CONTEXT function.
  1. sys@LUOCS12C> SELECT SYS_CONTEXT ('USERENV', 'CON_NAME') FROM DUAL;

  2. SYS_CONTEXT('USERENV','CON_NAME')
  3. ----------------------------------------------------------------------------------------------------
  4. CDB$ROOT

  5. 我们可以通过ALTER SESSION SET CONTAINER 指定其他容器,如下:
  6. sys@LUOCS12C> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

  7.     CON_ID       DBID NAME                           OPEN_MODE
  8. ---------- ---------- ------------------------------ ----------
  9.          2 4062250843 PDB$SEED                       READ ONLY
  10.          3  611934728 LUOCS                          MOUNTED

  11. 将Pdb open:
  12. sys@LUOCS12C> alter pluggable database luocs open;

  13. Pluggable database altered.

  14. sys@LUOCS12C> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

  15.     CON_ID       DBID NAME                           OPEN_MODE
  16. ---------- ---------- ------------------------------ ----------
  17.          2 4062250843 PDB$SEED                       READ ONLY
  18.          3  611934728 LUOCS                          READ WRITE

  19. 切换容器到pdb:
  20. sys@LUOCS12C> alter session set container=luocs;

  21. Session altered.

  22. sys@LUOCS12C> SELECT SYS_CONTEXT ('USERENV', 'CON_NAME') FROM DUAL;

  23. SYS_CONTEXT('USERENV','CON_NAME')
  24. ----------------------------------------------------------------------------------------------------
  25. LUOCS

  26. 切换到SEED
  27. sys@LUOCS12C> ALTER SESSION SET CONTAINER = PDB$SEED;

  28. Session altered.

  29. sys@LUOCS12C> SELECT SYS_CONTEXT ('USERENV', 'CON_NAME') FROM DUAL;

  30. SYS_CONTEXT('USERENV','CON_NAME')
  31. ----------------------------------------------------------------------------------------------------
  32. PDB$SEED

  33. 容器切回ROOT
  34. sys@LUOCS12C> ALTER SESSION SET CONTAINER = CDB$ROOT;

  35. Session altered.

  36. sys@LUOCS12C> SELECT SYS_CONTEXT ('USERENV', 'CON_NAME') FROM DUAL;

  37. SYS_CONTEXT('USERENV','CON_NAME')
  38. ----------------------------------------------------------------------------------------------------
  39. CDB$ROOT
复制代码
继续讨论权限问题。
  1. sys@LUOCS12C> grant resource to c##luocs;

  2. Grant succeeded.

  3. c##luocs@LUOCS12C> create table t1 (id number, name varchar2(20));

  4. Table created.

  5. c##luocs@LUOCS12C> insert into t1 values(1,'LUOCS');
  6. insert into t1 values(1,'LUOCS')
  7.             *
  8. ERROR at line 1:
  9. ORA-01950: no privileges on tablespace 'LUOCS'

  10. sys@LUOCS12C> alter user c##luocs quota unlimited on luocs;
  11. alter user c##luocs quota unlimited on luocs
  12. *
  13. ERROR at line 1:
  14. ORA-65048: error encountered when processing the current DDL statement in pluggable database LUOCS
  15. ORA-00959: tablespace 'LUOCS' does not exist
复制代码
在这里我们了解下COMMON USERS和LOCAL USERS。
1)COMMON USERS(普通用户):经常建立在CDB层,用户名以C##或c##开头;

2)LOCAL USERS(本地用户):仅建立在PDB层,建立的时候得指定CONTAINER。
  1. 上面我们创建的c##luocs明显为COMMON USER,那如何解决上述错误,我们只要指定一下容器就可以了

  2. sys@LUOCS12C> alter user c##luocs quota unlimited on luocs container=current;

  3. User altered.

  4. 这样我们也可以插入数据
  5. c##luocs@LUOCS12C> insert into t1 values(1,'LUOCS');

  6. 1 row created.

  7. c##luocs@LUOCS12C> commit;

  8. Commit complete.

  9. c##luocs@LUOCS12C> select * from t1;

  10.         ID NAME
  11. ---------- --------------------
  12.          1 LUOCS
复制代码
下面演示一下创建LOCAL USER:
  1. 首先容器指定到PDB:
  2. sys@LUOCS12C> alter session set container=luocs;

  3. Session altered.


  4. 我们都知道PDB有独立的system、sysaux和users、temp表空间
  5. sys@LUOCS12C> select name from v$datafile;

  6. NAME
  7. ----------------------------------------------------------------------------------------------------
  8. +RACDATA/LUOCS12C/DATAFILE/undotbs1.265.819647271
  9. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/system.272.819650131
  10. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/sysaux.273.819650131
  11. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/users.275.819650827

  12. sys@LUOCS12C> select name from v$tempfile;

  13. NAME
  14. ----------------------------------------------------------------------------------------------------
  15. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/TEMPFILE/temp.274.819650243

  16. 因此创建用户的时候,我们不能指定CDB中创建的表空间,如下:
  17. sys@LUOCS12C> create user luocs identified by oracle quota 50M on luocs;
  18. create user luocs identified by oracle quota 50M on luocs
  19. *
  20. ERROR at line 1:
  21. ORA-00959: tablespace 'LUOCS' does not exist

  22. 指定PDB中的表空间即可正确创建,注意,这里container指定的不是容器名字,而是写current,或者不写也可以
  23. sys@LUOCS12C> create user luocs identified by oracle quota 50M on users container=current;

  24. User created.

  25. sys@LUOCS12C> grant connect, resource to luocs;

  26. Grant succeeded.

  27. 当然我们也可以在PDB中创建独立表空间:
  28. sys@LUOCS12C> create tablespace luocs;

  29. Tablespace created.

  30. sys@LUOCS12C> select name from v$datafile;

  31. NAME
  32. ----------------------------------------------------------------------------------------------------
  33. +RACDATA/LUOCS12C/DATAFILE/undotbs1.265.819647271
  34. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/system.272.819650131
  35. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/sysaux.273.819650131
  36. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/users.275.819650827
  37. +RACDATA/LUOCS12C/E0701B9C2C674F73E0439601A8C0CB5C/DATAFILE/luocs.277.819908067
复制代码
下面我们看看12c新特性部分 Increased Size Limit for VARCHAR2, NVARCHAR2, and RAW Data Types

官方如下介绍:

The maximum size of the VARCHAR2, NVARCHAR2, and RAW data types has been increased from 4,000 to 32,767 bytes.

Increasing the allotted size for these data types allows users to store more information in character data types before switching to large objects (LOBs). This is especially useful for brief textual data types and the capabilities to build indexes on these types of columns.



也就是说,在12c中varchar2、NVARCHAR2和RAW类型从原先的4000字节增长到32767字节了。

但这取决于参数MAX_STRING_SIZE的设置,默认为STANDARD,表示依然是4000字节上限。
  1. sys@LUOCS12C> show parameter MAX_STRING_SIZE

  2. NAME                                 TYPE        VALUE
  3. ------------------------------------ ----------- ------------------------------
  4. max_string_size                      string      STANDARD

  5. luocs@LUOCS> create table t2 (col1 varchar2(4001));
  6. create table t2 (col1 varchar2(4001))
  7.                                *
  8. ERROR at line 1:
  9. ORA-00910: specified length too long for its datatype
复制代码
该参数设置为EXTENDED,上述类型才能支持到32767字节。

但该参数并不是简单使用alter去修改就能解决的事情,如下:
  1. sys@LUOCS12C> alter system set MAX_STRING_SIZE=EXTENDED;
  2. alter system set MAX_STRING_SIZE=EXTENDED
  3. *
  4. ERROR at line 1:
  5. ORA-02097: parameter cannot be modified because specified value is invalid
  6. ORA-14694: database must in UPGRADE mode to begin MAX_STRING_SIZE migration
复制代码
修改该参数必须遵循如下几个步骤:

1)关闭数据库

2)数据库重新启动到UPGRADE模式

3)修改MAX_STRING_SIZE参数,指定为EXTENDED

4)执行脚本:$ORACLE_HOME/rdbms/admin/utl32k.sql

5)数据库正常重启



OK,下面演示一下,我这里是12c RAC环境,目前仅开启节点1:
  1. [grid@12crac1 ~]$ crsctl stat res -t
  2. --------------------------------------------------------------------------------
  3. Name           Target  State        Server                   State details      
  4. --------------------------------------------------------------------------------
  5. Local Resources
  6. --------------------------------------------------------------------------------
  7. ora.LISTENER.lsnr
  8.                ONLINE  ONLINE       12crac1                  STABLE
  9. ora.RACCRS.dg
  10.                ONLINE  ONLINE       12crac1                  STABLE
  11. ora.RACDATA.dg
  12.                ONLINE  ONLINE       12crac1                  STABLE
  13. ora.RACFRA.dg
  14.                ONLINE  ONLINE       12crac1                  STABLE
  15. ora.asm
  16.                ONLINE  ONLINE       12crac1                  Started,STABLE
  17. ora.net1.network
  18.                ONLINE  ONLINE       12crac1                  STABLE
  19. ora.ons
  20.                ONLINE  ONLINE       12crac1                  STABLE
  21. --------------------------------------------------------------------------------
  22. Cluster Resources
  23. --------------------------------------------------------------------------------
  24. ora.12crac1.vip
  25.       1        ONLINE  ONLINE       12crac1                  STABLE
  26. ora.12crac2.vip
  27.       1        ONLINE  INTERMEDIATE 12crac1                  FAILED OVER,STABLE
  28. ora.LISTENER_SCAN1.lsnr
  29.       1        ONLINE  ONLINE       12crac1                  STABLE
  30. ora.LISTENER_SCAN2.lsnr
  31.       1        ONLINE  ONLINE       12crac1                  STABLE
  32. ora.LISTENER_SCAN3.lsnr
  33.       1        ONLINE  ONLINE       12crac1                  STABLE
  34. ora.MGMTLSNR
  35.       1        ONLINE  ONLINE       12crac1                  169.254.88.173 192.1
  36.                                                              68.80.150,STABLE
  37. ora.cvu
  38.       1        ONLINE  ONLINE       12crac1                  STABLE
  39. ora.luocs12c.db
  40.       1        OFFLINE OFFLINE                               Instance Shutdown,ST
  41.                                                              ABLE
  42.       2        ONLINE  ONLINE       12crac1                  Open,STABLE
  43. ora.mgmtdb
  44.       1        ONLINE  ONLINE       12crac1                  Open,STABLE
  45. ora.oc4j
  46.       1        ONLINE  ONLINE       12crac1                  STABLE
  47. ora.scan1.vip
  48.       1        ONLINE  ONLINE       12crac1                  STABLE
  49. ora.scan2.vip
  50.       1        ONLINE  ONLINE       12crac1                  STABLE
  51. ora.scan3.vip
  52.       1        ONLINE  ONLINE       12crac1                  STABLE
  53. --------------------------------------------------------------------------------

  54. 关闭数据库:
  55. [grid@12crac1 ~]$ srvctl stop instance -d luocs12c -i luocs12c1

  56. 数据库重新启动到UPGRADE模式
  57. 为了启动到UPGRADE模式,RAC中应如下操作:
  58. [oracle@12crac1 ~]$ sqlplus / as sysdba

  59. SQL*Plus: Release 12.1.0.1.0 Production on Thu Jul 4 17:14:30 2013

  60. Copyright (c) 1982, 2013, Oracle.  All rights reserved.

  61. Connected to an idle instance.

  62. idle> startup mount
  63. ORACLE instance started.

  64. Total System Global Area  835104768 bytes
  65. Fixed Size                  2293880 bytes
  66. Variable Size             750784392 bytes
  67. Database Buffers           75497472 bytes
  68. Redo Buffers                6529024 bytes
  69. Database mounted.
  70. idle> show parameter cluster_database

  71. NAME                                 TYPE        VALUE
  72. ------------------------------------ ----------- ------------------------------
  73. cluster_database                     boolean     TRUE
  74. cluster_database_instances           integer     2

  75. idle> alter system set cluster_database=false scope=spfile;

  76. System altered.

  77. idle> startup upgrade;
  78. ORACLE instance started.

  79. Total System Global Area  835104768 bytes
  80. Fixed Size                  2293880 bytes
  81. Variable Size             725618568 bytes
  82. Database Buffers          100663296 bytes
  83. Redo Buffers                6529024 bytes
  84. Database mounted.
  85. Database opened.

  86. 修改MAX_STRING_SIZE参数,指定为EXTENDED
  87. MAX_STRING_SIZE参数为静态参数
  88. idle> alter system set MAX_STRING_SIZE=EXTENDED scope=spfile;

  89. System altered.


  90. 执行脚本:$ORACLE_HOME/rdbms/admin/utl32k.sql
  91. idle> @$ORACLE_HOME/rdbms/admin/utl32k
  92. -- 该脚本执行时间稍长,请耐心等待

  93. 容器切换到SEED
  94. idle> ALTER SESSION SET CONTAINER = PDB$SEED;

  95. Session altered.

  96. idle> @$ORACLE_HOME/rdbms/admin/utl32k

  97. 启动pdb luocs
  98. sys@LUOCS12C> alter pluggable database luocs open upgrade;

  99. Pluggable database altered.

  100. idle> ALTER SESSION SET CONTAINER = luocs;

  101. Session altered.

  102. idle> @$ORACLE_HOME/rdbms/admin/utl32k
  103. 如果pdb上没执行过该脚本,在打开pdb的时候将会报:
  104. sys@LUOCS12C> alter pluggable database luocs open;
  105. alter pluggable database luocs open
  106. *
  107. ERROR at line 1:
  108. ORA-14694: database must in UPGRADE mode to begin MAX_STRING_SIZE migration


  109. 数据库正常重启
  110. 将RAC集群激活:
  111. idle> alter system set cluster_database=true scope=spfile;

  112. System altered.

  113. idle> shutdown immediate
  114. Database closed.
  115. Database dismounted.
  116. ORACLE instance shut down.

  117. [oracle@12crac1 ~]$ srvctl start instance -d luocs12c -i luocs12c1



  118. 就这样,数据库varchar2、nvarchar2和raw类型支持32k长度字符了。

  119. 举个例子看看:

  120. sys@LUOCS12C> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

  121.     CON_ID       DBID NAME                           OPEN_MODE
  122. ---------- ---------- ------------------------------ ----------
  123.          2 4062250843 PDB$SEED                       READ ONLY
  124.          3  611934728 LUOCS                          MOUNTED

  125. 我们启动pdb
  126. sys@LUOCS12C> alter pluggable database luocs open;

  127. Pluggable database altered.

  128. 连接pdb并创建表
  129. sys@LUOCS12C> conn luocs/oracle@luocs
  130. Connected.

  131. luocs@LUOCS> create table t1 (col1 varchar2(32767));

  132. Table created.
复制代码
– OK。



下面再看一下另一个新特性:Native SQL Support for Query Row Limits and Row Offsets

官方介绍如下:

The FETCH FIRST and OFFSET clauses provides native SQL language support to limit the number of rows returned and to specify a starting row for the return set.

Many queries need to limit the number of rows returned or offset the starting row of the results. For example, top-N queries sort their result set and then return only the first n rows. FETCH FIRST and OFFSET simplify syntax and comply with the ANSI SQL standard.



我们就简单做个测试:
  1. luocs@LUOCS> create table t2 (col number);

  2. Table created.

  3. luocs@LUOCS> insert into t2 select level from dual connect by level <=20;

  4. 20 rows created.

  5. luocs@LUOCS> commit;

  6. Commit complete.

  7. luocs@LUOCS> select * from t2;

  8.        COL
  9. ----------
  10.          1
  11.          2
  12.          3
  13.          4
  14.          5
  15.          6
  16.          7
  17.          8
  18.          9
  19.         10
  20.         11
  21.         12
  22.         13
  23.         14
  24.         15
  25.         16
  26.         17
  27.         18
  28.         19
  29.         20

  30. 20 rows selected.


  31. luocs@LUOCS> select col from t2 fetch first 10 rows only;

  32.        COL
  33. ----------
  34.          1
  35.          2
  36.          3
  37.          4
  38.          5
  39.          6
  40.          7
  41.          8
  42.          9
  43.         10

  44. 10 rows selected.

  45. luocs@LUOCS> select col from t2 order by col desc fetch first 10 rows only;

  46.        COL
  47. ----------
  48.         20
  49.         19
  50.         18
  51.         17
  52.         16
  53.         15
  54.         14
  55.         13
  56.         12
  57.         11

  58. 10 rows selected.

  59. luocs@LUOCS> select col from t2 offset 5 rows fetch next 10 rows only;

  60.        COL
  61. ----------
  62.          6
  63.          7
  64.          8
  65.          9
  66.         10
  67.         11
  68.         12
  69.         13
  70.         14
  71.         15

  72. 10 rows selected.

  73. 传统我们通过如下方式:
  74. luocs@LUOCS> select col from
  75.   2  (select col, rownum as rn from
  76.   3      (select col from t2) where rownum <=15)
  77.   4  where rn >=6;

  78.        COL
  79. ----------
  80.          6
  81.          7
  82.          8
  83.          9
  84.         10
  85.         11
  86.         12
  87.         13
  88.         14
  89.         15

  90. 10 rows selected.
复制代码
OK。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多