顯示具有 Oracle 標籤的文章。 顯示所有文章
顯示具有 Oracle 標籤的文章。 顯示所有文章

2011年4月1日 星期五

Enterprise Manager configuration failed (組態設定失敗)

問題

在安裝 Oracle 11g R2 時,安裝到最後,一定會出現如下面這個錯誤。

image

難道我使用虛擬機安裝,就認定本大爺沒錢嗎?即使用了英文介面也是一樣。

image

解法

原因很簡單,那就是 Oracle 必須是固定 ip。我之前安裝時,都使用 dhcp 取得動態的 ip,就會有上述的問題。

設成固定 ip 後,就安裝成功了。

image

安裝成功後,才會有如下圖的 Database Control 的功能

image

結論

同樣是資料庫,SQL Server 的安裝真的是簡易許多。相對的,Oracle 資料庫需要固定 ip才能安裝,是有點奇怪。不過,話又說回來,哪有資料庫是動態 ip 的?

2011年3月29日 星期二

Oracle 的 Schema 與 User 的關係

Oracle 真的與 SQL Server 大不同啊!

在 SQL Server 來說,Schema 只是一群 table, view, stored procedure 等的容器。一個 schema 可以給予不同 user 不同的權限。因此,schema 與 user 的關係並不是一對一的。

而在 Oracle,Schema 幾乎就等同於 User。當建立一個 User 時,就會自動建立出對應的 schema。預設的狀況下,不同的 user 因此就看不到其他user 的資料,因為是在不同的 schema 下。當然,經過授權後,其他 user 就可以讀取了。

例子

在 SQL Server 中,我們可以單純建立一個 Accounting 的 schema,再授權給已經存在的 user “Charles”。

在 Oracle 中,為了建立一個 Accounting 這個 schema,我們必須建立一個 user 名為 Accounting,才能得到這個 Accounting 這個 schema。然而,Accounting 這個 user 並不是我們想要的,只好將 Accounting 這個 user 給鎖定。

參考

http://wiki.answers.com/Q/Difference_between_user_and_schema_oracle ,寫到下面的重點

Difference between user and schema oracle?

* A schema is collection of database objects, including logical structures such as tables, views, sequences, stored procedures, synonyms, indexes, clusters, and database links.
* A user owns a schema.
* A user and a schema have the same name.
* The CREATE USER command creates a user. It also automatically creates a schema for that user.
* The CREATE SCHEMA command does not create a "schema" as it implies, it just allows you to create multiple tables and views and perform multiple grants in your own schema in a single transaction.
* For all intents and purposes you can consider a user to be a schema and a schema to be a user.

2010年9月21日 星期二

This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed

緣由

開發 Oracle 專案時,發生了下面的錯誤訊息

Attempt to load Oracle client libraries threw BadImageFormatException.  This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed. Inner exception message: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

環境

以下是我的開發環境

解決方法

    由於 ODAC 目前只有32 bits的程式,而VS2010方案(solution)指定Build Target 是 AnyCPU,在 IIS 7 上就會執行 64 bits。在 64 bits 的 Process 上載入 32 bits 的 Oracle client library 就會發生這樣的錯誤訊息。

   解決方式也很簡單,就是在 IIS 7 上指定 Application Pool 可以跑 32 bits 的程式。

  1. 找到執行該程式的 Application Pool. 我的例子是使用預設的 ASP.NET v4.0。
  2. 在右方的 Action Panel 上,點擊「Advanced Settings」
  3. 將 Enable 32-Bit Applications 設為 True

   image

結論

    此方法只適用在開發機器上,目的是為了使用 Oracle Developer Tools For Visual Studio。在正式機上,就應該安裝 64 bits 的 Oracle Client library.

2010年8月27日 星期五

遇到 Nulls 時的排序方式

今天看到了一個 Nulls Last 的 Oracle 語法,順便回頭研究 SQL Server 上要如何實作。

問題

當查詢遇到了排序的欄位中有 Null 值時,要如何將 Null 值改排在前還是排在後。

SQL Server 版

SQL Server 總認為 null 值是比較小的值。當order by 的欄位中有 null 時,null 的資料會先列出。相反地, order by …. desc 時會最後列出。

如果要改變,只好搞一下小技巧

--排列時,field1為null 的最後才列出
select * from tableName
order by case when field1 is null then 1 else 0 end, field1 asc

Oracle 版

Oracle 則認為 null 值是比較大的值。

對於 null 值要如何排序,Oracle 的獨特的語法來解決。

select * from tableName order by field1 NULLS First;   --Null 值先列出。此為預設

select * from tableName order by field1 NULLS Last;  -- Null 值後列出。

結論

這個問題,SQL Server 還是可以做到,只是要想一下小技巧。 看來這個地方 Oracle 比較貼心呢! 。

2010年8月26日 星期四

Oracle 的字串比對

問題

我想要查詢資料庫的Schema資訊時,以下列語法查詢,卻查不到資料。為什麼?

select * from sys.ALL_TAB_COLUMNS
where owner = 'hr'

原因

原來,Oracle 是有分大小寫的。此與 SQL Server 的預設值不同。為了查詢,必須改成大寫才能找到資料。

select * from sys.ALL_TAB_COLUMNS
where owner = 'HR'
image

不分大小寫

查詢時,如何不區分大小寫呢?例如在一個新聞上稿的資料庫中,查詢一篇文章內有 SQL Server 這個字串時,就必須不區分大小寫。

原來 Oracle 有兩個設定,NLS_COMP 與 NLS_SORT

NLS_COMP

這個設定是用來字串比對用的,適用於 where 子句中。

可設定的值

  • BINARY : 所有比對是根據其字元的 binary 值來比對。所以一定會分大小寫。
  • ANSI:遵守 NLS_SORT 的設定。這是10g R2 以前的設定方式。10g R2 以後仍在,只是為了向前相容性。
  • LINGUISTIC:遵守 NLS_SORT 的設定。這是 10g R2 後的設定方式。

設定方式

任選一方式來設定:

  • 環境變數:SET(export) NLS_COMP=<binary|linguistic>
  • 在連線的 session中 內設定:ALTER SESSION SET NLS_COMP=<binary|linguistic>;

NLS_SORT

NLS_SORT 設定 order by 子句的排序方式。

可設定的值

  • BINARY : 所有比對是根據其字元的 binary 值來排序。
  • V$NLS_VALID_VALUES : All sorting is done in accordance to a named linguistic definition. This means that Oracle will sort in accordance to a particular locale (GERMAN, POLISH, FRENCH, etc.).These valid named definitions may be obtained by querying the V$NLS_VALID_VALUES where parameter = `SORT'. Just be aware that all the values in this view also have a hybrid definition by adding a suffix of _CI (case insensitivity

設定方式

  • 環境變數:SET(export) NLS_SORT=<valid_value>
  • 在連線的 session中 內設定:ALTER SESSION SET NLS_SORT=<valid_value>;

範例

不區分大小寫

alter session set NLS_COMP=LINGUISTIC; --遵照 NLS_SORT 的設定,即下面的不區分大小寫
alter session set NLS_SORT=BINARY_CI; -- 不區分大小寫
select * from sys.ALL_TAB_COLUMNS where owner = 'Hr'
執行結果如下

 

image

 

參考

2010年8月21日 星期六

NHibernate 資源

前言

NHibernate 沿自 Java 的Hibernate,並在 .NET 平台中建立自己的勢力。在 ADO.NET Entity Framework 尚未出現前,在 .NET 平台中的 ORM 確實是第一把交椅。

許多客戶都在使用 Oracle,而在資料庫存取上, 到目前為止,ADO.NET Entity Framework 尚未有免費(或大廠)所推出的 provider for Oracle。Oracle 也有計畫要推出,見 Entity Framework Oracle Provider?,但時程至少落在2011年年初。我實在等不及了。只好回過頭來看看  Nhibernate 有沒有好的解決方案。

當然,我才 study 約5天左右,實在沒有實戰經驗。僅將找到的下載與學習資源集中在此,方便自己與大家學習。

官網資源

NHibernate 的官網當然是最多資源的地方。而 Download 區就有一堆實用的功能。我目前使用 2.1.2 版。

NHibernate Contrib

NHibernate Contrib 算是 NHibernate 的好兄弟,推出一些好用的工具,可在這裡下載。

其中極力推薦 NHibernate Linq,將 LINQ 的語法帶到了 NHibernate 中。因此不必再使用 Criteria 或 Query By Example 等不自然的方式來查詢,而且功能有限。HQL 雖然強大,可是不支援 Compile time 的除錯,當然也沒辦法 Intellisence。改用  LINQ 語法容易維護多了。

其他資源

PS:
   其他資源找到了會陸續補上。

2010年7月29日 星期四

2010 年 TPC-E Benchmark,微軟 SQL Server 全面勝出??

繼上次 2007 的 SQL Server 2005 vs Oracle 效能評比,這次我隔了三年才再看 TPC 的資料。

這次多了 TPC-E 的測試標準。在 TPC-E 的測試標準下,無論是效能(Top Ten TPC-E Results by Performance)或是效能/價格(Top Ten TPC-E Results by Price/Performance),微軟的 SQL Server 大獲全勝,包辦了前十名。

image

image

而舊版的 TPC-C 測試標準,Top Ten TPC-C Results by Performance: 則完全看不到微軟 SQL Server 的身影,而Top Ten TPC-C Results by Price/Performance:的效能/價格,則 SQL Server 才勉強地擠到第三名。

image

image

TPC-E 與 TPC-C 到底差在哪裡啊!怎麼兩者會差這麼多。當然 TPC 網站有對兩者的解釋。英文說明太長(TPC-C, TPC-E) 。

實在看不懂。查一下 Google,看到了 新伺服器效能評測標準TPC-E出爐這一篇。原來 TPC-E 模擬目前複雜的交易環境,十足的 B2B。而舊版 TPC-C 則只單純地模擬一家零售銷售商店經理向倉庫下達訂單,模擬系統的交易效能。

簡單地說,以現在的網路交易環境,TPC-E 比較準啦!微軟可樂壞了吧!

那Oracle 與  DB2 呢?看來這兩個資料庫不喜 TPC-E 的 Benchmark,尚未提出任何測試報告呢。

參考

TPC-E和TPC-C测试结果比较之我见

新伺服器效能評測標準TPC-E出爐

昔日贵族TPC-C今成"鸡肋" TPC-E欲篡位

Microsoft Still the Only Database Vendor Posting TPC-E Scores

2010年7月1日 星期四

Entity Framework Oracle Provider?

微軟的宣傳工作一向做的很好,而開發工具也做非常棒,每一次都帶給我無限的驚奇。ADO.NET Entity Framework (EF)的好處,在於先設計模型後再開發程式。而且該模型是 Entity Model,而非傳統的 Data Model。

這樣的好處不少,但 ADO.NET Entity Framework (EF)目前可使用的 provider 只有 SQL Server。換句話說,EF 只能連自家的 SQL Serve,其他如 Oracle, MySQL 等都不行。

CodePlex 上有幾個開源碼,但都只在 Alpha 階段,並不成熟, 如 Oracle Data Provider for Entity Frameworkeforacleodpprovider。3rd 公司有不錯的產品,如 devart 的 dotConnects

Oracle 自家的 ODP.NET 目前尚未支援 EF。未來計畫呢?在 Statement of Direction 中,宣告預計在2010年底 ODP.NET Beta with Entity Framework 會有 beta 版,而預計於 2011 年推出。當然,不保證。

後記

[20110329]

在 2011/02/10 Entity Framwork 終於有官方版了,目前是 BETA 版。見 ODAC 11.2.0.2.30 Beta for Entity Framework and LINQ to Entities

2009年6月30日 星期二

微軟不再全力 support OracleClient

根據微軟 ADO.NET Team 的部落格,System.Data.OracleClient Update 提到他們做了一個決定

The Decision

After carefully considering all the options and talking to our customers, partners, and MVPs it was decided to deprecate OracleClient as a part of our ADO.NET roadmap.

 

意思是說,雖然在 .net 4.0 仍然會支援 OracleClient,但會被標示為deprecated (過時,不被建議),請使用 3rd 的元件。

那我們的因應對策呢?新開發案,請使用 ODP.NET,維護案,就維持原來的狀況即可。

2008年8月25日 星期一

Oracle 學習(5) rownum 的技巧

Oracle 的 rownum 是一個 pseudocolumn,意思是不存在的 column,是經由計算得來的.

先建立範例資料 .

create table emp(id int, sid char(10) not null, name char(10) not null);

insert into emp(id, sid, name) values (1, 'Charles', 'F111222333');
insert into emp(id, sid, name) values (2, 'Jerry', 'A121121121');
insert into emp(id, sid, name) values (3, 'Abel', 'H114567898');
執行下面的語法,可得到所有資料,且得到 rownum 代表的「列號」
select id, sid, name, rownum from emp;
結果如下,很正常。
image
但如果需要依 name 來排序,答案就怪怪的。
select id, sid, name, rownum from emp order by name;

結果如下,RowNum 是先經過編號後,才再 order by name 的。 
image

所以,如果要先 order by name 才編號,就必須下這樣的sql script

select t.*, rownum from (
	select id, sid, name from emp order by name ) t

結果如下

image 
相同的問題,如果在 SQL Server 2005 上,就設計地好多了,因為語法上很清楚地說明了「編號是針對order by name」來編號。

select *, ROW_NUMBER() over (order by name) from emp;
結果如下圖。看來 sql server 的設計是較貼近 developer 的.
image

2008年8月22日 星期五

Oracle 學習(4) SQL Paging

在 SQL Paging 的功能上,一直「感覺」Oracle 會比較強。主要是之前 SQL Server 2000 與 Oracle 7 的印象。
但在 SQL Server 2005 之後呢?就沒有比較過了。
舉例來說,在 SQL Server 2005 ,可以使用下面的語法
select * from 
( 
   select *, ROW_NUMBER() over(order by invoicenumber) as rn from B2CInvoice 
) a 
where a.rn between 21 and 30 
SQL Server 2000的語法就不說了,是很差的,而且並沒有通用的版本。
那 Oracle 呢?我現在只找到如下的方法,看起來比sql server 2005 的麻煩
SELECT * FROM
     (
     SELECT A.*, rownum r
     FROM
          (
          SELECT * FROM B2CINVOICE
          ORDER BY invoiceNumber
          ) A
     WHERE rownum <= 30
     ) B
WHERE r >= 21;
注意到,下面的是沒有資料回傳的,也就是錯誤的查詢結果。雖然看起來比較直覺而且易懂
     SELECT A.*, rownum r
     FROM
          (
          SELECT * FROM B2CINVOICE
          ORDER BY invoiceNumber
          ) A
     WHERE rownum >= 21 and rownum <=30

2008年8月21日 星期四

Oracle 學習(3) boolean 的資料欄位

忍不住要發點牢騷。在 SQL Server 上,一直嘗試著Follow SQL 92 的標準。裡面有這麼一段
SQL defines distinct data types named by the following key words: CHARACTER, CHARACTER VARYING, BIT, BIT VARYING, NUMERIC, DECIMAL, INTEGER, SMALLINT, FLOAT, REAL, DOUBLE PRECISION, DATE, TIME, TIMESTAMP, and INTERVAL.

因此,bit 的data type 是 SQL 92 的標準。根據此標準,我們可以宣告一個欄位為 bit,此欄位只能存0 或 1 (因為是 bit)。
在 AP 的設計時,就會將該資料視為 boolean 。
範例如下:建立一個測試資料庫及資料表
create database test;
go
use test;
go
create table Emp(id int not null, IsRetired bit not null);
所以,有一個員工編號1號,尚未退休時,就會用下面的sql script 來 insert
insert into dbo.Emp(id, IsRetired) values (1, 0);
接下來,使用 LINQ to Sql 並寫如下的程式,找出所有未退休的員工資料
TestDataContext ctx = new TestDataContext();
var q = from e in ctx.Emps
        where e.IsRetired == false
        select e;

其中, linq to sql 自然就將 IsRetired 欄位當成 boolean 值,0 是 false, 1 是 true. 這在 sql server 是相當自然而沒有疑慮的事情。
那在 Oracle 呢?Oracle 是資料庫的老大,當然不需要 follow 標準。(所以別怪 MS Office 不照 OpenDocument format (ODF) 的標準,而要自行搞一套 Office Open Xml 的標準)
Oracle 不支援 bit 的 datatype,甚至在 Oracle vs. SQL Server中也指明了,bit 的相等的oracle 表達法是 number(1, 0)。
是否我們需要存 boolean 值時,在oracle 就宣告成 number(1, 0) 呢?
將來如果出了一個 linq to oracle 的 privider,是否看到 number(1, 0) 就自行轉成 boolean 呢? 看了很多前人的程式,都是自行定義的,如 char(1),然後寫入值為 '0' 或 '1' ,也有寫成 'Y' 或 'N",當然也有 'y' 或 'n'。
實在令人頭痛。
如果是您,不知道您會如何宣告呢?

2008年8月20日 星期三

Oracle 學習(2) Role 的權限

Oracle資料庫是相當大的層級,而與SQL Server 的層級不太相同。
例如,SQL Server 的層級是 Instance-Database-Schema-Securable

像是 server.myDB.mySchema.myTable

系統層級的權限,會設定在 instance 層級上,而資料庫層級的權限,則會在 database 層級上。因此,會有sysadmin (instance level role, 即server role) 及 db_owner (database level role) 會如此的類似。見之前的文章 db_owner 與 database owner

image image image

而Oracle 的設計是不太一樣的。Oracle 的資料庫相當的「高級」,幾乎等同於 SQL Server 的 Instance,因此system level 的 privileges 直接設在 database level。
雖然 Oracle 也可以安裝多個 instance 在同一個 server,但幾乎沒人這麼做,因為一個 instance 就會把伺服器的資源吃完了。

Oracle 資料庫內建的 role 有 connect, resource等,其實已經是相當大的權限了。例如 conect 就有 create table 的權限。
image
不知道 SQL Server database levle 的 role : db_datareader,在Oracle 上要如何設定呢?
從 Google 搜尋後,得到了如下的 answer

grant select any table to myUser
但是,這仍然只是近似的方法,並非等同的sql server 的 db_datareader 的權限。

2008年8月6日 星期三

Oracle 學習(1)-- Create User

目前需要用到 Oracle。不過,之前使用 Oracle 已經是十年前的事了(Oracle 7)。 現在的變化應該很大了。 將Study 的過程記錄下來,應該對之後有幫助。
--Create User
set timing off;

spool log\createMyDbReader.log;

create user MyDbReader identified by MyPassword
default tablespace users
temporary tablespace temp
quota unlimited on users
quota unlimited on INDX
quota unlimited on objects
quota unlimited on temp
;
grant create session to MyDbReader;
grant unlimited tablespace to MyDbReader;
grant query rewrite to MyDbReader;

spool off;
exit;

--使用 MyDbReader 登入後更改 password 為 MyNewPassword
connect MyDbReader/MyPassword@MyServer ;

alter user MyDbReader identified by MyNewPassword

--刪除使用者
drop user MyDbReader;

Share with Facebook