ORACLE VARCHAR2最大長度是4000?還是32767?
VARCHAR2資料型別的最大長度問題,是一個讓人迷惑的問題,因為VARCHAR2既分PL/SQL Data Types中的變數型別,也分Oracle Database中的欄位型別。簡單的說,要看你在什麼應用場景下,否則難以回答VARCHAR2資料型別的最大長度問題。
ORACLE資料庫欄位型別
關於Oracle Database中的欄位的VARCHAR2型別的最大長度,我們先看下面的例子:
SQL> create table test ( name varchar2(4001) );
create table test ( name varchar2(4001) )
*
ERROR at line 1:
ORA-00910: specified length too long for its datatype
SQL> create table test ( name varchar2(4000) );
Table created.
如上所示,在Oracle Database中,VARCHAR2欄位型別,最大值為4000,SQL參考手冊中也明確指出VARCHAR2的最大大小為4000,注意此處的最大長度是指位元組長度,而不是指字元個數。這個跟引數NLS_LENGTH_SEMANTICS有一定關係,如下所示,當引數NLS_LENGTH_SEMANTICS為位元組時,定義的變數長度為位元組長度
如下所示,本資料庫NLS_CHARACTERSET值為AL32UTF8,一個漢字佔三個位元組
SQL> ALTER SESSION SET NLS_LENGTH_SEMANTICS=BYTE;
Session altered
SQL> DROP TABLE TEST PURGE;
Table dropped
SQL> CREATE TABLE TEST ( NAME VARCHAR2(7));
Table created
SQL> INSERT INTO TEST VALUES ('字');
1 row inserted
SQL> COMMIT;
Commit complete
SQL> INSERT INTO TEST VALUES('字字字');
INSERT INTO TEST VALUES('字字字')
ORA-12899: value too large for column "SYSTEM"."TEST"."NAME" (actual: 9, maximum: 7)
SQL> SELECT LENGTH(NAME), LENGTHB(NAME) FROM TEST;
LENGTH(NAME) LENGTHB(NAME)
------------ -------------
1 3
如果將引數NLS_LENGTH_SEMANTICS,則定義VARCHAR2(7)表示
SQL> ALTER SESSION SET NLS_LENGTH_SEMANTICS=CHAR;
Session altered
SQL> DROP TABLE TEST;
Table dropped
SQL> CREATE TABLE TEST ( NAME VARCHAR2(7));
Table created
SQL> INSERT INTO TEST VALUES ('字');
1 row inserted
SQL> COMMIT;
Commit complete
SQL> INSERT INTO TEST VALUES('字字字');
1 row inserted
SQL> COMMIT;
Commit complete
SQL> SELECT LENGTH(NAME), LENGTHB(NAME) FROM TEST;
LENGTH(NAME) LENGTHB(NAME)
------------ -------------
1 3
3 9
不管引數NLS_LENGTH_SEMANTICS取值為字元或位元組,其所能容納的字串的位元組數都不能超過4000.
PL/SQL變數型別:
接下來我們看看PL/SQL中VARCHAR2變數型別,如下官方文件所示,它的最大位元組長度為32767,所能容納的字元個數取決於字符集。
Declaring Variables for Multibyte Characters
The maximum size of a CHAR or VARCHAR2 variable is 32,767 bytes, whether you specify the maximum size in characters or bytes. The maximum number of characters in the variable depends on the character set type and sometimes on the characters themselves:
Character Set Type |
Maximum Number of Characters |
Single-byte character set |
32,767 |
n-byte fixed-width multibyte character set (for example, AL16UTF16) |
FLOOR(32,767/n) |
n-byte variable-width multibyte character set with character widths between 1 and n bytes (for example, JA16SJIS or AL32UTF8) |
Depends on characters themselves—can be anything from 32,767 (for a string containing only 1-byte characters) through FLOOR(32,767/n) (for a string containing only n-byte characters). |
When declaring a CHAR or VARCHAR2 variable, to ensure that it can always hold n characters in anymultibyte character set, declare its length in characters—that is, CHAR(n CHAR) or VARCHAR2(n CHAR), where n does not exceed FLOOR(32767/4) = 8191.
可以通過下面一個PL/SQL程式碼來驗證一下,如下所示,可以定義一個VARCHAR2型別的變數,給其賦值6000個字串。
DECLARE
V_OUT VARCHAR2(32767);
BEGIN
V_OUT := RPAD('T', 6000, 'M');
DBMS_OUTPUT.PUT_LINE(LENGTH(V_OUT));
END;
如果給VARCHAR2型別變數賦值超過23767,就會報PLS-00215: String length constraints must be in range (1 .. 32767)錯誤。
DECLARE
V_OUT VARCHAR2(32768);
BEGIN
V_OUT := RPAD('T', 5000, 'M');
DBMS_OUTPUT.PUT_LINE(LENGTH(V_OUT));
END;