VARCHAR2數據類型的最大長度問題,是一個讓人迷惑的問題,由於VARCHAR2既分PL/SQL Data Types中的變量類型,也分Oracle Database中的字段類型。簡單的說,要看你在什麼應用場景下,不然難以回答VARCHAR2數據類型的最大長度問題。 數據庫
ORACLE數據庫字段類型 app
關於Oracle Database中的字段的VARCHAR2類型的最大長度,咱們先看下面的例子: spa
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爲字節時,定義的變量長度爲字節長度 code
以下所示,本數據庫NLS_CHARACTERSET值爲AL32UTF8,一個漢字佔三個字節 blog
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)表示 ip
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. ci
PL/SQL變量類型: 文檔
接下來咱們看看PL/SQL中VARCHAR2變量類型,以下官方文檔所示,它的最大字節長度爲32767,所能容納的字符個數取決於字符集。 字符串
Declaring Variables for Multibyte Characters get
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 any multibyte 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;