Create Schema的使用体会

Create Schema语句是SQL99的一个特性。ORACLE中描述其作用是在你自己的schema 内在单个事务里完成多个表和视图的创建以及多个授权(Use the CREATE SCHEMA statement to create multiple tables and views and perform. multiple grants in your own schema in a single transaction.)。它的语法是:



所以首先一点,”schema” 必须是当前连接的用户,不能为其他用户创建。即使你修改current_schema,例如:

SQL> show user

USER is "SYSTEM"

SQL>  CREATE SCHEMA AUTHORIZATION guoge

  2  CREATE TABLE ASSET_ACCESSORY  (

  3     ASSET_NO             VARCHAR2(18)                    not null,

  4     ITEM_NO              NUMBER(3)                       not null,

  5     NAME                 VARCHAR2(40),

  6     SPEC                 VARCHAR2(20),

  7     AMOUNT               NUMBER(3),

  8     COSTS                NUMBER(10,2),

  9     STATUS               VARCHAR2(16),

 10     LOCATION             VARCHAR2(40),

 11     MEMO                 VARCHAR2(20)

 12     )

 13  ;

 CREATE SCHEMA AUTHORIZATION guoge

*

ERROR at line 1:

ORA-02421: missing or invalid schema authorization identifier

SQL> alter session set current_schema=guoge;

 

Session altered.

 

SQL>  CREATE SCHEMA AUTHORIZATION guoge

  2  CREATE TABLE ASSET_ACCESSORY  (

  3     ASSET_NO             VARCHAR2(18)                    not null,

  4     ITEM_NO              NUMBER(3)                       not null,

  5     NAME                 VARCHAR2(40),

  6     SPEC                 VARCHAR2(20),

  7     AMOUNT               NUMBER(3),

  8     COSTS                NUMBER(10,2),

  9     STATUS               VARCHAR2(16),

 10     LOCATION             VARCHAR2(40),

 11     MEMO                 VARCHAR2(20)

 12     )

 13  ;

 CREATE SCHEMA AUTHORIZATION guoge

*

ERROR at line 1:

ORA-02421: missing or invalid schema authorization identifier

 

 

其次,Create Schema语句关于创建视图的语句只能是Create View 而不能是Create Or Replace View ,这一点,yangtingkun方案对象的一般性管理——管理员手册》有说明。这一点,ORACLE的文档中并没有说明。

那么,这条语句什么时候使用呢?

在创建系统的安装脚本时,经常创建一部分表后出现错误,退出安装脚本。这个时候一般要先清除脚本,把原来创建的表先删除。如果使用Create Schema 语句,当后面的语句失败了,前面的语句创建的对象或授权自动取消,就不必再执行清除操作。

 

在具体实现上,ORACLE应该不是我们简单地想像先创建前面的对象,后面的语句错误时再删除前面的对象。通过LOGMNR,我们可以看不到前面创建对象真的执行,这可能被ORACLE真正封装在一个事务里。但是,在解析过程中,ORACLE还是把里面的语句逐一解析,例如:

 

SQL>  alter session set timed_statistics =  true;

 

Session altered.

SQL> alter session set sql_trace=true ;

 

Session altered.

 

SQL> create schema authorization guoge

  2       create table t (id number , name varchar2(30))

  3        grant select on v_t to public1;

      grant select on v_t to public1

      *

ERROR at line 3:

ORA-02426: privilege grant failed

ORA-00942: table or view does not exist

 

 

SQL>   alter session set sql_trace=false ;

 

Session altered.

 

D:\oracle\admin\ORCL\udump>tkprof ORA01336.trc ORA01336.txt sys=no

 

TKPROF: Release 8.1.7.4.1 - Production on Wed Dec 12 10:21:01 2007

 

(c) Copyright 2000 Oracle Corporation.  All rights reserved.

 

查看ORA01336.txt

 

 

The following statement encountered a error during parse:

 

      grant select on v_ ...

EXEC #3:c=0,e=0,p=0,cr

Error encountered: ORA-00942

********************************************************************************

 

alter session set sql_trace=true

 

 

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------  ----------

Parse        0      0.00       0.00          0          0          0           0

Execute      1      0.00       0.00          0          0          0           0

Fetch        0      0.00       0.00          0          0          0           0

------- ------  -------- ---------- ---------- ---------- ----------  ----------

total        1      0.00       0.00          0          0          0           0

 

Misses in library cache during parse: 0

Optimizer goal: CHOOSE

Parsing user id: 3727 

********************************************************************************

 

create schema authorization guoge

     create table t (id number , name varchar2(30))

      grant select on v_t to public1

 

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------  ----------

Parse        1      0.00       0.00          0          0          0           0

Execute      0      0.00       0.00          0          0          0           0

Fetch        0      0.00       0.00          0          0          0           0

------- ------  -------- ---------- ---------- ---------- ----------  ----------

total        1      0.00       0.00          0          0          0           0

 

Misses in library cache during parse: 1

Optimizer goal: CHOOSE

Parsing user id: 3727 

********************************************************************************

 

create table t (id number , name varchar2(30))

 

 

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------  ----------

Parse        1      0.00       0.00          0          0          0           0

Execute      1      0.00       0.00          0          0          4           0

Fetch        0      0.00       0.00          0          0          0           0

------- ------  -------- ---------- ---------- ---------- ----------  ----------

total        2      0.00       0.00          0          0          4           0

 

Misses in library cache during parse: 1

Optimizer goal: CHOOSE

Parsing user id: 3727     (recursive depth: 1)

********************************************************************************

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9036/viewspace-422/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9036/viewspace-422/

你可能感兴趣的:(Create Schema的使用体会)