Search Google

Friday, November 07, 2008

Derby之schema does not exist

原本想說在java的ij console下執行query指令,那麼在java app裡面照著下應該也不會有問題,沒想到昨天這麼做的時候應是得到了個shema does not exist的錯誤,原文是:"綱目 'A' 不存在。"('A'是連進該DB的username,稍後會有解釋)。
Google之後在derby自己的網頁中找到答案,借quote一下:

5.3. Why do I get the error 'schema does not exist'?

^

The current schema for any connection defaults to a schema corresponding to the user name. If no user name is supplied then the user name (and hence current schema) defaults to APP.

However even though the current schema is set to the user name, that schema may not exist. A schema is only created by CREATE SCHEMA or creating an object (table etc.) in that schema (this is implicit schema creation).

The one exception to this is the APP schema, which is always created, though applications should not depend on that.

So you will see the schema not exists error if your application tries to access the current schema before any objects have been created in it. Possibilities are you try to perform a DROP TABLE before creating it, or try to SELECT from a table to see if it should be created or not, or when there is a problem with the sql for the very first object you try to create in a particular schema and no explicit CREATE SCHEMA was issued.

重點就是query的時候最好前面還是加上個shema名稱,如果在建立table時沒有指定則預設為"APP",但是如果有使用特定user name登入的話就會以username作為預設schema名稱。

因為當初建立table時我是以embedded client連進DB,自然就沒有使用username,使得預設的shema名稱為"APP",當我從另外一台電腦使用username "A"透過network client連進DB後在沒有指定schema名稱下執行query時,DB自然會把username "A"當作schema下去找,因為DB中根本就沒有"A"這個schema存在,因此就得到了上述的"綱目 'A' 不存在。"錯誤。

因此要嘛就不要用username建立connection(但是security風險太大了),要嘛就是下query時指定schema名稱(還是建議這麼做)。

PS. 原本不知道"綱目"是schema的中譯名稱,是在ij console下看到42Y07的error code(完整錯誤訊息為:"錯誤 42Y07:綱目 'A' 不存在。")之後透過google才找到這個mapping。

2 comments:

Lexel Yu said...

James大, 貴公司的Lotus Notes和ClearQuest好難用, 請你upgrade一下

t@c0 said...

Lotus Notes明明就蠻好用的啊!
Upgrade請找負責你們家的account sales聊聊吧 ^^