热衷技术,热爱交流
分类: Oracle
2013-06-14 00:11:48
The length semantics of character data types can be
measured in bytes or characters. Byte semantics treat
strings as a sequence of bytes. This is
the default for character data types. Character semantics treat strings as a sequence of characters. A character is technically a
code point of the
database character set.
Oracle字符数据类型的长度语义有两种,即字节语义和字符语义。varchar2按照字节长度语义来限定字符串长度,而nvarchar2按照字符长度语义限定字符串长度。
查看数据库的字符集:
HR >select PARAMETER,value from v$nls_parameters where parameter like '%CHARACTERSET%';
PARAMETER |VALUE
------------------------------|------------------------------
NLS_CHARACTERSET |ZHS16GBK
NLS_NCHAR_CHARACTERSET |AL16UTF16
测试varchar2(一个汉字占用两个字节):
HR >create table test(id varchar2(5));
HR >insert into test values('你好');
1 row created.
HR >insert into test values('你好a');
1 row created.
HR >insert into test values('你好啊');
insert into test values('你好啊')
ERROR at line 1:
ORA-12899: 列 "HR"."TEST"."ID" 的值太大 (实际值: 6, 最大值: 5)
Elapsed: 00:00:00.01
Nvarchar2字符类型存储的是unicode字符集数据,本例是AL16UTF16,它可以存放几乎所有国家的字符。当创建国家字符类型属性时候,最大长度始终
是字符长度语义(When you create a table with an NCHAR or NVARCHAR2 column, the maximum size is always in character length
semantics)
测试nvarchar(2):
HR >drop table test;
Table dropped.
HR >create table test(id nvarchar2(5));
HR >insert into test values('你好啊是吗');
1 row created.
HR >insert into test values('你好啊是吗不是');
insert into test values('你好啊是吗不是') *
ERROR at line 1:
ORA-12899: 列 "HR"."TEST"."ID" 的值太大 (实际值: 7, 最大值: 5)
HR >insert into test values('aaaaaa');
insert into test values('aaaaaa')
*
ERROR at line 1:
ORA-12899: 列 "HR"."TEST"."ID" 的值太大 (实际值: 6, 最大值: 5)