Hey!! Sky!

Feb 26, 2006

BLOCK、EXTENT、SEGMENT、TABLESPACE、Data Dictionary

Block
Block 可以分为 
  Header
  Table Directory
  Row Directory
  Row Data
  Free Space
其中,block header、table directory、row directory 合起来称为 Overhead。

详细解释:


Header (Common and Variable)
  The header contains general block information, such as the block address and the type of segment (for example, data or index).

Table Directory
  This portion of the data block contains information about the table having rows in this block.

Row Directory
  This portion of the data block contains information about the actual rows in the block (including addresses for each row piece in the row data area).this space is not reclaimed when the row is deleted.

Overhead
  The data block header, table directory, and row directory are referred to collectively as overhead. Some block overhead is fixed in size; the total block overhead size is variable. On average, the fixed and variable portions of data block overhead total 84 to 107 bytes.

Row Data
  This portion of the data block contains table or index data. Rows can span blocks.

Free Space
  Free space is allocated for insertion of new rows and for updates to rows that require additional space (for example, when a trailing null is updated to a nonnull value).

Row Chaining(行连接)和 Row Migrating(行迁移)


Row Chaining:
  the row is too large to fit into one data block when it is first inserted. In this case, Oracle stores the data for the row in a chain of data blocks (one or more) reserved for that segment. Row chaining most often occurs with large rows, such as rows that contain a column of datatype LONG or LONG RAW. Row chaining in these cases is unavoidable.

Row Migrating:
  a row that originally fit into one data block is updated so that the overall row length increases, and the block's free space is already completely filled. In this case, Oracle migrates the data for the entire row to a new data block, assuming the entire row can fit in a new block. Oracle preserves the original row piece of a migrated row to point to the new block containing the migrated row. The rowid of a migrated row does not change.


SEGMENT
High Water Mark
The high water mark is the boundary between used and unused space in a segment.

When Extents Are Deallocated?
  当 Extents 分配给某个对象时,一般不会自己回收,除非以下几种情况发生:


1.drop the schema object whose data is stored in the segment (using a DROP TABLE or DROP CLUSTER statement)
2.TRUNCATE...DROP STORAGE statement(用 delete 不会回收)
3.ALTER TABLE table_name DEALLOCATE UNUSED(HWM 之后的空间);
4.Oracle deallocates one or more extents of a rollback segment if it has the OPTIMAL size specified

TRUNCATE 语法:

TRUNCATE TABLE XXX { DROP STORAGE(DEFAULT) | REUSE STORAGE }

TABLESPACE
EXTENT MANAGEMENT LOCAL 的两种 Extents 分配方式:

AUTOALLOCATE (system-managed):you can specify the size of the initial extent and Oracle determines the optimal size of additional extents, with a minimum extent size of 64 KB. This is the default for permanent tablespaces.

UNIFORM SIZE:you can specify an extent size or use the default size, which is 1 MB. Temporary tablespaces that manage their extents locally can only use this type of allocation.

    当使用 Local 管理 Extents 时参数 NEXT, PCTINCREASE, MINEXTENTS, MAXEXTENTS, and DEFAULT STORAGE 不能使用。

SEGMENT SPACE MANAGEMENT 的两种管理方式:

MANUAL
  Specifying MANUAL tells Oracle that you want to use free lists for managing free space within segments.Free lists are lists of data blocks that have space available for inserting rows. This form of managing space within segments is called manual segment-space management because of the need to specify and tune the PCTUSED, FREELISTS, and FREELISTS GROUPS storage parameters for schema objects created in the tablespace.

AUTO
  This keyword tells Oracle that you want to use bitmaps to manage the free space within segments. A bitmap, in this case, is a map that describes the status of each data block within a segment with respect to the amount of space in the block available for inserting rows. As more or less space becomes available in a data block, its new state is reflected in the bitmap. Bitmaps allow Oracle to manage free space more automatically, and thus, this form of space management is called automatic segment-space management.It completely eliminates any need to specify and tune the PCTUSED, FREELISTS, and FREELISTS GROUPS storage parameters for schema objects created in the tablespace. If such attributes should be specified, they are ignored.


系统表空间:


  When the SYSTEM tablespace is locally managed, you must define a default temporary tablespace when creating a database. A locally managed SYSTEM tablespace cannot be used for default temporary storage.

UNDO 表空间:


  Through the use of a system parameter (UNDO_RETENTION), you can specify the amount of committed undo information to retain in the database.

The Data Dictionary
参数 O7_DICTIONARY_ACCESSIBILITY 的作用:

  Oracle recommends that you implement data dictionary protection to prevent users having the ANY system privileges from using such privileges on the data dictionary. If you enable dictionary protection (O7_DICTIONARY_ACCESSIBILITY is false), then access to objects in the SYS schema (dictionary objects) is restricted to users with the SYS schema. These users are SYS and those who connect as SYSDBA.

O7_DICTIONARY_ACCESSIBILITY:
说明: 主要用于从 Oracle7 移植到 Oracle8i。如果该值为 TRUE, SYSTEM 权限 (如 SELECT ANY TABLE) 将不限制对 SYS 方案中各对象的访问 (Oracle7 行为)。如果该值为 FALSE, 只有被授予了 SELECT_CATALOG_ROLE, EXECUTE_CATALOG_ROLE 或 DELETE_CATALOG_ROLE 权限的用户才能访问 SYS 方案中的各对象。
值范围: TRUE | FALSE
默认值: TRUE

    该参数主要为了兼容 7.0 版本,用来限制其他用户是否可以访问 sys schema 下的对象。以后的版本可以通过 SELECT_CATALOG_ROLE, EXECUTE_CATALOG_ROLE 或 DELETE_CATALOG_ROLE 权限来控制。
    另外,由于 pl/sql 的编译的一些特性,oracle 采取了角色在存储过程、函数、包等在 pl/sql 中不起作用的策略,必须直接授权才生效,所以即使有dba角色在存储过程中也是无效的。(参考:http://www.itpub.net/251232,1.html
DBMS_METADATA
  The DBMS_METADATA package provides interfaces for extracting complete definitions of database objects. The definitions can be expressed either as XML or as SQL DDL.

最简单的使用方法:



SQL> set long 2000
SQL> select dbms_metadata.get_ddl('TABLE','TEST') from dual;


DBMS_METADATA.GET_DDL('TABLE','TEST')
--------------------------------------------------------------------------------


  CREATE TABLE "SYS"."TEST"
   (    "OWNER" VARCHAR2(30),
        "OBJECT_NAME" VARCHAR2(128),
        "SUBOBJECT_NAME" VARCHAR2(30),
        "OBJECT_ID" NUMBER,
        "DATA_OBJECT_ID" NUMBER,
        "OBJECT_TYPE" VARCHAR2(18),
        "CREATED" DATE,
        "LAST_DDL_TIME" DATE,
        "TIMESTAMP" VARCHAR2(19),
        "STATUS" VARCHAR2(7),
        "TEMPORARY" VARCHAR2(1),
        "GENERATED" VARCHAR2(1),
        "SECONDARY" VARCHAR2(1)
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"

参考:
http://blog.csdn.net/eygle/archive/2004/12/22/225673.aspx
更详细的使用方法:
http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96612/d_metad2.htm#1024701

Feb 24, 2006

[下载]SunnyXu 的自动备份脚本

Exp 自动备份  http://www.oracle.com.cn/viewthread.php?tid=20864


RMAN自动备份脚本(Linux)  http://www.oracle.com.cn/viewthread.php?tid=37483


RMAN自动备份脚本(Windows)  http://www.oracle.com.cn/viewthread.php?tid=42027

DBA 角色到底有没有 UNLIMITED TABLESPACE 权限

    今天看文档的时候发现当用 SELECT * FROM ROLE_SYS_PRIVS WHERE ROLE='DBA' 列出 DBA 角色的所有系统权限时,里面没有 UNLIMITED TABLESPACE 这一项,可是当新创建一个用户,不授予任何表空间的分配限额,但授予 DBA 角色时,该用户可以在任何表空间上分配空间。


    这是为什么呢?后来问了小爱,他告诉我 SYSTEM_PRIVILEGE_MAP 视图,并告诉我 DBA 拥有该视图中的所有系统权限。我查了下 Oracle 文档,并没有提到,只是说该视图包含 Oracle 中所有系统权限,还是很疑惑。


    google 了一下,的确该视图可以查询所有系统权限,并包括权限号。另外还有个 TABLE_PRIVILEGE_MAP,应该是对表的所有操作。


    另外有个 DBA_SYS_PRIVS 该视图和 SYSTEM_PRIVILEGE_MAP 的区别是,前者包含所有已经分配的系统权限,后者是所有系统权限。不过:


SQL> select * from dba_sys_privs
  2  where grantee='DBA'
  3  and privilege like 'U%';
GRANT PRIVILEGE                      ADM
----- ------------------------------ ---
DBA   UPDATE ANY TABLE               YES
显然,也没有包括 UNLIMITED TABLESPACE,不知道到底为什么。

Feb 23, 2006

如何给 sqlplus 安装 help

参考帖子:http://www.oracle.com.cn/viewthread.php?tid=1710



Installing the Help Facility Manually
The Help Facility may be installed manually using the shell script  
$ORACLE_HOME/bin/helpins. Before you run the script, the SYSTEM_PASS environment variable should be set to the SYSTEM schema name and password. For example:  
  
$ setenv SYSTEM_PASS SYSTEM/MANAGER
$ helpins
  
If the SYSTEM_PASS variable is not set, helpins prompts you for the SYSTEM password and loads the help data into the SYSTEM schema. You can also run $ORACLE_HOME/sqlplus/help/helpbld.sql helpus.sql to install the Help Facility. The system user can run the file helpbld.sql and its argument, helpus.sql, in SQL*Plus to create Help Facility Tables. For example:  
  
$ sqlplus system/manager
SQL> @?/sqlplus/admin/help/helpbld.sql helpus.sql



注:最后 @?/sqlplus/admin/help/helpbld.sql helpus.sql 这句,CP 似乎写错了,也不知道是 Windows 下和 Linux 有所不同,我在 Linux 下尝试了下,需要指定两个参数,第一个路径,第二个 helpus.sql,当然第一个可以为空,表示 helpbld.sql 所在目录。如下:@?\sqlplus\admin\help\helpbld.sql " " helpus.sql


    Oracle 9i 之后 sqlplus 的帮助默认是安装的,通过一下方法使用帮助:



SQL> help index


 @             COPY         PAUSE                    SHUTDOWN
 @@            DEFINE       PRINT                    SPOOL
 /             DEL          PROMPT                   SQLPLUS
 ACCEPT        DESCRIBE     QUIT                     START
 APPEND        DISCONNECT   RECOVER                  STARTUP
 ARCHIVE LOG   EDIT         REMARK                   STORE
 ATTRIBUTE     EXECUTE      REPFOOTER                TIMING
 BREAK         EXIT         REPHEADER                TTITLE
 BTITLE        GET          RESERVED WORDS (SQL)     UNDEFINE
 CHANGE        HELP         RESERVED WORDS (PL/SQL)  VARIABLE
 CLEAR         HOST         RUN                      WHENEVER OSERROR
 COLUMN        INPUT        SAVE                     WHENEVER SQLERROR
 COMPUTE       LIST         SET
 CONNECT       PASSWORD     SHOW

SQL> help list


 LIST
 ----


 Lists one or more lines of the SQL buffer. Enter LIST with no
 clauses to list all lines.


 L[IST] [n | n m | n  * | n LAST | * | * n | * LAST | LAST]


    另外网友还提供了一份 8.0.5 下的 SQL 帮助,还有人将其做成了 insert 语句的 script,可以在 system 下运行,不过要先 trancate 原来的 help 表,否则会出错。导入 help 表之后就可以用 help topic 来显示帮助列表了,不再是 help index。


 

Feb 05, 2006

今天星期几?

    感觉 Oracle 中关于日期的函数并没有 SQL SERVER 丰富,很多对于 SQL SERVER 来说很简单的一个函数就搞定的功能,Oracle 需要绕个圈来做,但是 Oracle 也有自己的优势,to_date 和 to_char 两个函数就能完成很多功能(虽然可能需要动动脑子),而且非常灵活,可以两个函数互相组合达到意想不到的效果。


    刚才群里有人问怎么判断今天是星期几,我一下子先想到了下面这个语句:


select to_char(sysdate,'d') from dual
    很显然这句并不能完成要求的功能,因为这只是显示当前为本星期的第几天,而老外把星期天当作每个星期的第一天,那么变通的方法就是求前一天为该周的第几天,不就相当于今天为星期几了么:

select to_char(sysdate - 1,'d') from dual

    轻松解决了问题。


    对于判断星期几来说上面的方法比较可行,因为它返回一个数字,这样比较好处理,而如果要显示的话,还有更适合的方法,让 Oracle 根据你要求的语言环境来直接显示符合该语言的表达方式。返回默认语言环境下,今天为星期几:



SQL> select to_char(sysdate,'day') day from dual;


DAY
------
星期日


    如果我们想用英文呢?简单:



SQL> select to_char(sysdate,'day','NLS_DATE_LANGUAGE=AMERICAN') day from dual;


DAY
---------
sunday


    当然你也可以直接改变当前 session 的语言设置:



SQL> ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';


会话已更改。


SQL> select to_char(sysdate,'day') day from dual;


DAY
---------
sunday


    另外可以用 'dy' 参数获得缩写:



SQL> select to_char(sysdate,'dy') day from dual;


DAY
---
sun


    格式化参数的含义(适合 to_char 和 to_number):
Day:
d:  该星期的第几天(数字表示)
dd: 该月中的第几天(同上)
ddd:该年中的第几天(同上)
dy: 星期几缩写(文字表示,可以通过设置 nls_date_language 来设置语言)
day: 星期几(同上)
Month:
mm:该年中第几个月
(数字表示)
mon:月份缩写(文字表示,可以通过设置 nls_date_language 来设置语言)
month:月份(同上)
Year:
yy:两位显示年份(数字表示)
yyyy:四位显示年份(数字表示,如位数不到 4 位,前面补 0)
rr:两位显示年份(数字表示)
rrrr:四位显示年份(数字表示,详见下面解释)
year:英文拼写显示年份
Weak:
w:该月中第几周(数字表示)
ww:该年中第几周(同上)
Century:
cc:第几世纪

关于 RRRR 参数的显示方法

A.如果当前年份在后半世纪(50-99)
1.输入的两位数字年份在‘00’和‘49’之间:将按下一世纪的年份存储。例如,1996年输入的‘02’将存为‘2002’
2.且输入的两位数字年份在‘50’和‘99’之间:将按本世纪年份存储。例如,1996年输入的‘97’将存为‘1997’。


B.如果当前年份在前半世纪(00-49)
1.且输入的两位数字年份在“00’和‘49’之间:将按本世纪年份存储。例如,2001年输入的‘02’将存为‘2002’
2.且输入的两位数字年份在‘50’和‘99’之间:将按下一世纪的年份存储。例如2001年输入的‘97’将存为‘1997’