22. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
SQL> create table t_number(a number);
SQL> insert into test values(1001);
SQL> commit;
SQL> select a,dump(a) physical_storage_data from t_number;
A PHYSICAL_STORAGE_DATA
----------- --------------------------------------------------
1001 Typ=2 Len=3: 194,11,2
正數值:194-193=1為指數(Exponent)
(11-1)*100^1+(2-1)*100^(1-1)=10*100+1*100^0=1000+1=1001
以上是以10進位格式為範例,真正存在資料區塊中的格式為16進位
日期型態(Date Type)的資料則會先轉換為 Julian Day(儒略日)格式,而國際
天文學上所使用的儒略日計數實際上是由西元前 4713 年 1 月 1 日起始,然
而 Oracle 所使用的儒略日計數則是以西元前 4712 年 1 月 1 日為起點。將
西元前 4712 年 1 月 1 日的儒略日計數值設為 1,之後每經過一天則加 1。
例如西元 2008 年 3 月 22 日的儒略日計數值即為 2454548,表示從西元
前 4712 年 1 月 1 日後,經過了 2454548 天。再將儒略日計數轉為世紀、
年、月、日、時、分、秒的格式存在資料區塊中。
SQL> select to_char(to_date('2008-03-22','YYYY-MM-DD'),'J') JULIAN_D
AY from dual;
JULIAN_DAY
-----------------
2454548
SQL> select to_char(to_date(2454548,'J'),'YYYY-MM-DD:HH24:MI:SS') G
REGORIAN_DAY from dual;
GREGORIAN_DAY
---------------------------
2008-03-22:00:00:00
SQL> create table t_date(a date);
SQL> insert into t_date values(to_date('2008-03-22','YYYY-MM-DD'));
SQL> commit;
SQL> select to_char(a,’YYYY-MM-DD:HH24:MI:SS’) a,dump(a) physical_
storage_data from t_date;
A PHYSICAL_STORAGE_DATA
23. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
--------------------------- --------------------------------------------------
2008-03-22:00:00:00 Typ=12 Len=7: 120,108,3,22,1,1,1
世紀:120-100=20
年:108-100=08
月:3
日:22
時:1-1=0
分:1-1=0
秒:1-1=0
以上是以10進位格式為範例,真正存在資料區塊中的格式為16進位
SQL> alter session set nls_date_format='DD-MON-YYYY BC';
SQL> select to_char(to_date('01-JAN-4713 BC'), 'J') from dual;
select to_char(to_date('01-JAN-4713 BC'), 'J') from dual
*
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
與國際天文學上常用的以西元前4713年1月1日為起點的格式不同
SQL> select to_char(to_date('01-JAN-4712 BC'), 'J') JULIAN_DAY from d
ual;
JULIAN_DAY
-----------------
0000001
SQL> select to_date(1,'J') GREGORIAN_DAY from dual;
GREGORIAN_DAY
-------------------------
01-JAN-4712 BC
SQL> select to_date(10,'J') GREGORIAN_DAY from dual;
GREGORIAN_DAY
-------------------------
10-JAN-4712 BC
39. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
UNDO_TABLESPACE=UNDOTBS,則此子句應該為 UNDO
TABLESPACE undotbs DATAFILE `
/u02/oradata/ora11g/undotbs01.dbf’ SIZE 200M。
DEFAULT TEMPORARY TABLESPACE:這個子句用來建立一個系統預
設的暫存表格空間。暫存區塊用在當使用者要求進行排序操作(SORT
OPERATION)時,使用者會談中的 PGA 空間不足以進行記憶體排序
(MEMEORY SORT),必須將部份資料先暫時存放在暫存區塊中,等待後續
的排序操作,這種排序就稱作磁碟排序(DISK SORT)。範例:DEFAULT
TEMPORARY TABLESPACE temp TEMPFILE
`/u02/oradata/ora11g/temp01.dbf’SIZE 100M。
DEFAULT TABLESPACE:這個子句用來設定系統的預設使用者表格空間,
用來當作使用者建立表格時,並未指定該表格所在的表格空間時,當作預設
的表格空間。範例:DEFAULT TABLESPACE users DATAFILE
`/u02/oradata/ora11g/users01.dbf’ SIZE 100M。
以上是大部份的子句,只要將這些子句組合起來,就可以完成 CREATE
DATABASE 的 SQL 指令。
[oracle@ELinux ~]$ export ORACLE_SID=ora11g
[oracle@ELinux ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.1.0.6.0 - Production on Fri Mar 28 14:13:45 2008
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup nomount
ORACLE instance started.
Total System Global Area 313860096 bytes
Fixed Size 1299624 bytes
Variable Size 96471896 bytes
Database Buffers 209715200 bytes
Redo Buffers 6373376 bytes
SQL> CREATE DATABASE ora11g
40. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
2> USER SYS IDENTIFIED BY abc123
3> USER SYSTEM IDENTIFIED BY abc123
4> MAXLOGFILES 5
5> MAXLOGMEMBERS 5
6> MAXLOGHISTORY 1
7> MAXDATAFILES 100
8> LOGFILE GROUP 1 ('/u02/oradata/ora11g/redo01a.log','/u02/orada
ta/ora11g/redo01b.log') SIZE 50M,GROUP 2 ('/u02/oradata/ora11g/redo0
2a.log','/u02/oradata/ora11g/redo02b.log') SIZE 50M
9> CHARACTER SET ZHT16MSWIN950
10> NATIONAL CHARACTER SET AL16UTF16
11> EXTENT MANAGEMENT LOCAL
12> DATAFILE '/u02/oradata/ora11g/system01.dbf' SIZE 325M
13> SYSAUX DATAFILE '/u02/oradata/ora11g/sysaux01.dbf' SIZE 325
M
14> DEFAULT TABLESPACE users
15> DATAFILE '/u02/oradata/ora11g/users01.dbf' SIZE 500M AUTOEX
TEND ON MAXSIZE UNLIMITED
16> DEFAULT TEMPORARY TABLESPACE tempts1
17> TEMPFILE '/u02/oradata/ora11g/temp01.dbf' SIZE 200M
18> UNDO TABLESPACE undotbs DATAFILE '/u02/oradata/ora11g/und
otbs01.dbf' SIZE 200M AUTOEXTEND ON MAXSIZE UNLIMITED
19> SET TIME_ZONE = '+08:00';
Database created.
SQL> select status from v$instance;
STATUS
------------
OPEN --建立資料庫後,Oracle執行處理的狀態已經是開啟
SQL> select name from v$datafile; --目前存在的資料檔
NAME
--------------------------------------------------------------------------------
/u02/oradata/ora11g/system01.dbf
/u02/oradata/ora11g/sysaux01.dbf
/u02/oradata/ora11g/undotbs01.dbf
/u02/oradata/ora11g/users01.dbf
SQL> select name from v$tempfile; --目前存在的暫存檔
41. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
NAME
--------------------------------------------------------------------------------
/u02/oradata/ora11g/temp01.dbf
SQL> select name from v$controlfile; --控制檔
NAME
--------------------------------------------------------------------------------
/u02/oradata/ora11g/control01.ctl
/u02/oradata/ora11g/control02.ctl
SQL> select member from v$logfile; --重作日誌檔
MEMBER
--------------------------------------------------------------------------------
/u02/oradata/ora11g/redo01a.log
/u02/oradata/ora11g/redo01b.log
/u02/oradata/ora11g/redo02a.log
/u02/oradata/ora11g/redo02b.log
當資料庫成功地建立後,資料庫中已經建好一些表格空間: SYSTEM、
SYSAUX、UNDOTBS、USERS、TEMP 等,以及系統管理的使用者帳戶:
SYS 與 SYSTEM,一些預設的角色:DBA、CONNECT、RESOURCE、
EXP_CATALOG_ROLE、IMP_CATALOG_ROLE 等,最重要的是資料辭
典也已經建立完成。這些工作都是 Oracle 執行處理在開啟 Oracle 資料庫後,
自動執行 SQL.BSQ(放在$ORACLE_HOME/rdbms/admin 下)這個指令稿
所產生的結果。
SQL> select name from v$tablespace; --表格空間
NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS
TEMPTS1
USERS
SQL> select name from sys.user$ where type#=1; --使用者
NAME
42. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
------------------------------
SYS
SYSTEM
OUTLN
SQL> select name from sys.user$ where type#=0; --角色
NAME
------------------------------
_NEXT_USER
PUBLIC
CONNECT
RESOURCE
DBA
SELECT_CATALOG_ROLE
EXECUTE_CATALOG_ROLE
DELETE_CATALOG_ROLE
EXP_FULL_DATABASE
IMP_FULL_DATABASE
10 rows selected.
4.7 建立資料庫後的後續動作
當資料庫建立完成後,目前已經可以進行使用者帳戶的建立、表格空間的建
立、表格或索引等區段的建立。不過此時資料庫還需要執行一些 SQL 指令
稿來建立一些輔助用的資料庫物件,這些物件可以讓資料庫管理者,更方便、
更清楚地了解資料庫結構。同時也可提供一些 Oracle 資料庫事先準備好的程
式套件,讓資料庫管理者直接使用這些套件,就可以完成相關的資料庫管理
工作,而不需要自己撰寫這些困難的程式功能。
4.7.1 建立資料辭典視圖(Data Dictionary View)
可是當資料庫管理者需要查詢資料辭典時,將會發現資料辭典本身的表格名
稱,往往都比較讓人難懂與記憶。同時資料辭典中的表格欄位,通常都是使
用數字形態,這些資料所代表的意義對資料庫管理者來說,可能不太容易一
眼看出來。這時資料庫管理者可以藉由執行
43. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
$ORACLE_HOME/rdbms/admin/catalog.sql,將資料辭典視圖建立,這樣
便可以將難懂的資料辭典內容用一些文字敘述來取代數字內容,讓資料庫管
理者可以輕鬆的了解資料庫目前的結構。
SQL> show user
USER is “SYS”
SQL> @$ORACLE_HOME/rdbms/admin/catalog.sql
4.7.2 建立內建套件(Build-in Package)
此外為了讓後續的資料庫管理與程式撰寫可以簡單地使用一些資料庫的功能,
Oracle 資料庫中也可以藉由執行
$ORACLE_HOME/rdbms/admin/catproc.sql,將一些 Oracle 資料庫內建
的套件產生,這樣不管是資料庫管理者或資料庫程式設計人員便可以利用這
些套件所提供的功能,讓管理資料庫或程式設計方面更加輕鬆,而不需要自
己撰寫相關的程式。這裡要提醒讀者,執行 catalog.sql 與 catproc.sql 時,
資料庫使用者必須以 sysdba 身份登入資料庫。
SQL> show user
USER is “SYS”
SQL> @$ORACLE_HOME/rdbms/admin/catproc.sql
4.7.3 建立 SQL*PLUS 所需要的表格
$ORACLE_HOME/sqlplus/admin/pupbld.sql:這個 SQL 指令稿是用在當
有非 SYS 或 SYSTEM 這兩個使用者帳戶,利用 SQL*PLUS 這個工具登入
Oracle 資料庫時,Oracle 資料庫會檢查一些 SQL*PLUS 專用的表格,決定
登入者所能執行的指令。這個 SQL 指令稿的執行者不是 SYS 而是
SYSTEM。
$ sqlplus frank/oracle --假設已經建立一個使用者frank
Error accessing PRODUCT_USER_PROFILE
Warning: Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM
SQL> connect system/abc123
44. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
SQL> show user
USER is “SYSTEM”
SQL> @$ORACLE_HOME/sqlplus/admin/pupbld.sql
4.7.4 新增其他的資料庫功能
當使用 CREATE DATABASE 指令建立資料庫時,所建立的資料庫並沒有如
使用 DBCA(DataBase Configuration Assistant)一般已經安裝好 Oracle
JVM、Oracle Text 等功能。所以如果有這些需求的話,資料庫管理人員需
要自己執行相關的 SQL 指令稿,來安裝所需要的功能。當然也可以利用
DBCA 來新增這些功能,在這裡就不多加敘述。
以下是自行安裝 JVM 與 Oracle Text 的範例
Oracle JVM:
SQL> CONNECT sys/abc123 AS SYSDBA
SQL> @/u01/app/oracle/product/11.1.0/db_1/javavm/install/initjvm.sql;
SQL> @/u01/app/oracle/product/11.1.0/db_1/xdk/admin/initxml.sql;
SQL> @/u01/app/oracle/product/11.1.0/db_1/xdk/admin/xmlja.sql;
SQL> @/u01/app/oracle/product/11.1.0/db_1/rdbms/admin/catjava.sql;
SQL> @/u01/app/oracle/product/11.1.0/db_1/rdbms/admin/catexf.sql;
Oracle Text
SQL> CONNECT sys/abc123 AS SYSDBA
SQL> @/u01/app/oracle/product/11.1.0/db_1/ctx/admin/catctx oracle SYS
AUX TEMP NOLOCK;
SQL> CONNECT CTXSYS/oracle
SQL> @/u01/app/oracle/product/11.1.0/db_1/ctx/admin/defaults/dr0defin.
sql "AMERICAN";
新增資料庫控制(Configure Database Control)
當手動建立資料庫後,如果資料庫管理人員想使用資料庫控制(Database
Control)管理這個資料庫,則必須使用手動執行 EMCA(Enterprise Manager
Configure Assistant)設定資料庫控制,或使用 DBCA(DataBase Configure
Assistant)設定資料庫控制也可以。
45. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
[oracle@ELinux ~]$ emca -config dbcontrol db -repos create
Enter the following information:
Database SID: ora11g
Database Control is already configured for the database ora11g
You have chosen to configure Database Control for managing the databa
se ora11g
This will remove the existing configuration and the default settings and pe
rform a fresh configuration
Do you wish to continue? [yes(Y)/no(N)]: y
Listener port number: 1521
Password for SYS user: oracle
Password for DBSNMP user: oracle
Password for SYSMAN user: oracle
Password for SYSMAN user: oracle
Email address for notifications (optional): frank_shen@uuu.com.tw
Outgoing Mail (SMTP) server for notifications (optional): mail.uuu.com.tw
-----------------------------------------------------------------
You have specified the following settings
Database ORACLE_HOME .............. /u01/app/oracle/product/11.1.0/db_1
Local hostname ................ elinux
Listener port number ................ 1521
Database SID ................ ora11g
Email address for notifications ............... frank_shen@uuu.com.tw
Outgoing Mail (SMTP) server for notifications ............... mail.uuu.com.tw
-----------------------------------------------------------------
Do you wish to continue? [yes(Y)/no(N)]: y
--接著等待整個動作完成即可
……………………………………………………………….
The Database Control URL is https://elinux:5500/em --EM的URL
***********************************************************
Enterprise Manager configuration completed successfully
使用網頁瀏覽器連到 https://elinux:5500/em 開始使用 Enterprise
Manager 管理 ora11g 這個資料庫。目前可以登入資料庫控制的帳號只有
47. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
移除資料庫控制
如果有需要將現有的資料庫控制移除,只能使用 emca 將資料庫控制移除。
[oracle@ELinux ~]$ export ORACLE_SID=orcl11g --指定執行處理
[oracle@ELinux ~]$ emctl stop dbconsole
Oracle Enterprise Manager 11g Database Control Release 11.1.0.6.0
Copyright (c) 1996, 2007 Oracle Corporation. All rights reserved.
https://elinux:5501/em/console/aboutApplication
Stopping Oracle Enterprise Manager 11g Database Control ...
... Stopped.
[oracle@ELinux ~]$ emca -deconfig dbcontrol db -repos drop
STARTED EMCA at Mar 31, 2009 4:25:14 PM
EM Configuration Assistant, Version 11.1.0.5.0 Production
Copyright (c) 2003, 2005, Oracle. All rights reserved.
Enter the following information:
Database SID: orcl11g
Listener port number: 1521
Password for SYS user:
Password for SYSMAN user:
Do you wish to continue? [yes(Y)/no(N)]: y
Mar 31, 2009 4:25:26 PM oracle.sysman.emcp.EMConfig perform
INFO: This operation is being logged at /u01/app/oracle/cfgtoollogs/emca/
orcl11g/emca_2009_03_31_16_25_14.log.
Mar 31, 2009 4:25:27 PM oracle.sysman.emcp.util.DBControlUtil stopOM
S
INFO: Stopping Database Control (this may take a while) ...
Mar 31, 2009 4:25:31 PM oracle.sysman.emcp.EMReposConfig invoke
INFO: Dropping the EM repository (this may take a while) ...
Mar 31, 2009 4:29:17 PM oracle.sysman.emcp.EMReposConfig invoke
INFO: Repository successfully dropped
Enterprise Manager configuration completed successfully
FINISHED EMCA at Mar 31, 2009 4:29:25 PM
[oracle@ELinux ~]$ emca -config dbcontrol db -repos create
Enter the following information:
Database SID: ora11g
Database Control is already configured for the database ora11g
You have chosen to configure Database Control for managing the databa
48. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
se ora11g
This will remove the existing configuration and the default settings and pe
rform a fresh configuration
Do you wish to continue? [yes(Y)/no(N)]: y
Listener port number: 1521
Password for SYS user: oracle
Password for DBSNMP user: oracle
Password for SYSMAN user: oracle
Password for SYSMAN user: oracle
Email address for notifications (optional): frank_shen@uuu.com.tw
Outgoing Mail (SMTP) server for notifications (optional): mail.uuu.com.tw
-----------------------------------------------------------------
You have specified the following settings
Database ORACLE_HOME .............. /u01/app/oracle/product/11.1.0/db_1
Local hostname ................ elinux
Listener port number ................ 1521
Database SID ................ ora11g
Email address for notifications ............... frank_shen@uuu.com.tw
Outgoing Mail (SMTP) server for notifications ............... mail.uuu.com.tw
-----------------------------------------------------------------
Do you wish to continue? [yes(Y)/no(N)]: y
--接著等待整個動作完成即可
……………………………………………………………….
The Database Control URL is https://elinux:5500/em --EM的URL
***********************************************************
Enterprise Manager configuration completed successfully
4.7.5 使用資料庫建立輔助(DBCA)建立資料庫控制
DBCA 可以用來新增資料庫控制,只要選擇設定”Configure Database
Options”即可。DBCA 自動會偵測資料庫中是否有資料庫控制存在,若沒
有資料庫控制存在,則會出現”Configure Enterprise Manager”的畫面,
按畫面說明一步一步設定即可完成資料庫控制新增操作。
49. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
50. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
51. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
52. Chapter 5 Manual Create Database
Oracle Database 11g Management Best Practice
結論
經過本章節的介紹,相信讀者對如何規劃、準備、建立資料庫,應該有著初
步的認識,接下來的章節將接續本章的內容,繼續為這個新建的資料庫添加
其他的儲存結構及綱要物件,如表格空間、使用者、表格、索引等,讓這個
資料庫更能符合應用程式的需求。