【保留字】使用檢視V$RESERVED_WORDS得到Oracle的保留字

secooler發表於2009-12-03
在建立表時如果使用了Oralce的某些保留字,將會收到系統提示的“無效名”等錯誤,那麼Oracle都有哪些保留字需要回避呢?
這個問題完全可以使用Oralce自帶的V$RESERVED_WORDS檢視來回答,不同的版本中這個檢視中記錄的內容是有差別的,以實際環境查詢內容為準。

簡單演示一下使用保留字建立表時報錯的現象。當看到有類似“invalid ... name”等錯誤時,就要提高警惕了,不是你敲錯了字母就是用到了某些被禁止使用的保留字。

1.保留字作為列名時
例如,我們打算使用“number”這個名字作為表的一個列名,嘗試建立這個表是就會收到報錯
sec@ora10g> create table t (number int);
create table t (number int)
                *
ERROR at line 1:
ORA-00904: : invalid identifier

2.保留字作為表名時
使用“number”作為表名也是一樣會報錯
sys@ora10g> create table number ( x int);
create table number ( x int)
             *
ERROR at line 1:
ORA-00903: invalid table name

3.查詢一下還有哪些保留字不可以作為識別符號(以Oracle 10gR2環境為例)
如果V$RESERVED_WORDS檢視的RESERVED欄位內容是“Y”表示這個保留字不可以在識別符號中使用的。
sys@ora10g> select * from v$reserved_words where RESERVED = 'Y';

KEYWORD                            LENGTH R R R R D
------------------------------ ---------- - - - - -
SHARE                                   5 Y N N N N
+                                       1 Y N N N N
PCTFREE                                 7 Y N N N N
EXISTS                                  6 Y N N N N
&                                       1 Y N N N N
INSERT                                  6 Y N N N N
DROP                                    4 Y N N N N
BETWEEN                                 7 Y N N N N
FROM                                    4 Y N N N N
)                                       1 Y N N N N
DESC                                    4 Y N N N N
OPTION                                  6 Y N N N N
TO                                      2 Y N N N N
PRIOR                                   5 Y N N N N
LONG                                    4 Y N N N N
THEN                                    4 Y N N N N
DEFAULT                                 7 Y N N N N
IS                                      2 Y N N N N
,                                       1 Y N N N N
INTO                                    4 Y N N N N
HAVING                                  6 Y N N N N
MINUS                                   5 Y N N N N
INTEGER                                 7 Y N N N Y
UPDATE                                  6 Y N N N N
GRANT                                   5 Y N N N N
/                                       1 Y N N N N
ALL                                     3 Y N N N N
^                                       1 Y N N N N
ORDER                                   5 Y N N N N
EXCLUSIVE                               9 Y N N N N
FLOAT                                   5 Y N N N N
DATE                                    4 Y N N N N
ON                                      2 Y N N N N
NUMBER                                  6 Y N N N N
UNION                                   5 Y N N N N
RESOURCE                                8 Y N N N N
PUBLIC                                  6 Y N N N N
TABLE                                   5 Y N N N N
VARCHAR2                                8 Y N N N N
@                                       1 Y N N N N
ELSE                                    4 Y N N N N
VALUES                                  6 Y N N N N
.                                       1 Y N N N N
RENAME                                  6 Y N N N N
AS                                      2 Y N N N N
=                                       1 Y N N N N
ALTER                                   5 Y N N N N
INDEX                                   5 Y N N N N
FOR                                     3 Y N N N N
WHERE                                   5 Y N N N N
CHECK                                   5 Y N N N N
SMALLINT                                8 Y N N N Y
WITH                                    4 Y N N N N
DELETE                                  6 Y N N N N
REVOKE                                  6 Y N N N N
(                                       1 Y N N N N
SIZE                                    4 Y N N N N
NOCOMPRESS                             10 Y N N N N
>                                       1 Y N N N N
AND                                     3 Y N N N N
|                                       1 Y N N N N
:                                       1 Y N N N N
NULL                                    4 Y N N N N
GROUP                                   5 Y N N N N
ASC                                     3 Y N N N N
IN                                      2 Y N N N N
VIEW                                    4 Y N N N N
SET                                     3 Y N N N N
COMPRESS                                8 Y N N N N
-                                       1 Y N N N N
[                                       1 Y N N N N

NOT                                     3 Y N N N N
LIKE                                    4 Y N N N N
TRIGGER                                 7 Y N N N N
SELECT                                  6 Y N N N N
CLUSTER                                 7 Y N N N N
LOCK                                    4 Y N N N N
CREATE                                  6 Y N N N N
INTERSECT                               9 Y N N N N
]                                       1 Y N N N N
DISTINCT                                8 Y N N N N
!                                       1 Y N N N N
CONNECT                                 7 Y N N N N
MODE                                    4 Y N N N N
OF                                      2 Y N N N N
RAW                                     3 Y N N N N
*                                       1 Y N N N N
UNIQUE                                  6 Y N N N N
SYNONYM                                 7 Y N N N N
VARCHAR                                 7 Y N N N N
ANY                                     3 Y N N N N
DECIMAL                                 7 Y N N N Y
IDENTIFIED                             10 Y N N N N
OR                                      2 Y N N N N
START                                   5 Y N N N N
NOWAIT                                  6 Y N N N N
BY                                      2 Y N N N N
CHAR                                    4 Y N N N Y

99 rows selected.

4.強制使用保留字的方法
如果要強制使用這些保留字,可以使用雙銀行將保留字括起來使用。不過如果使用了保留字,那麼使用起時將非常的不便,而且極易出現混淆的問題,因此應該嚴格禁止這種情況的發生。
1)以上面的例子為例演示一下使用保留字成功建立表的情況
sys@ora10g> create table t ("number" int);

Table created.

sys@ora10g> create table "number" ( x int);

Table created.

sys@ora10g> desc t
 Name                      Null?    Type
 ------------------------- -------- ----------------
 number                             NUMBER(38)

2)此時必須使用帶引號的形式引用這個特殊的表名,操作不便,而且容易出現錯誤。
sys@ora10g> desc "number"
 Name                      Null?    Type
 ------------------------- -------- ----------------
 X                                  NUMBER(38)

5.V$RESERVED_WORDS檢視中的其他列的的含義請參考Oracle的官方文件


V$RESERVED_WORDS

This view gives a list of all SQL keywords. To determine whether a particular keyword is reserved in any way, check the RESERVED, RES_TYPE, RES_ATTR, and RES_SEMI columns.

Column Datatype Description
KEYWORD VARCHAR2(30) Name of the keyword
LENGTH NUMBER Length of the keyword
RESERVED VARCHAR2(1) A value of Y means that the keyword cannot be used as an identifier. A value of N means that it is not reserved.
RES_TYPE VARCHAR2(1) A value of Y means that the keyword cannot be used as a type name. A value of N means that it is not reserved.
RES_ATTR VARCHAR2(1) A value of Y means that the keyword cannot be used as an attribute name. A value of N means that it is not reserved.
RES_SEMI VARCHAR2(1) A value of Y means that the keyword is not allowed as an identifier in certain situations, such as in DML. A value of N means that it is not reserved.
DUPLICATE VARCHAR2(1) A value of Y means that the keyword is a duplicate of another keyword. A value of N means that it is not a duplicate.


6.小結
很多問題Oracle都在內部給出了答案,很是便利。如本例中使用V$RESERVED_WORDS檢視快速得到保留字的功能就是一個很好的例子。

Good luck.

secooler
09.12.03

-- The End --


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-621601/,如需轉載,請註明出處,否則將追究法律責任。

相關文章