What is the default value of VARCHAR2 in Oracle?

◇◆丶佛笑我妖孽 提交于 2020-04-14 12:14:19

问题


I have a column in one of my tables that is of type varchar2(2000), but does this mean 2000 bytes? or 2000 characters? Because I know there's a difference between bytes and characters...


回答1:


The default is one of the NLS parameters of your session, namely NLS_LENGTH_SEMANTICS. Here is how I can check it from my session:

select value
from   v$nls_parameters
where  parameter = 'NLS_LENGTH_SEMANTICS'
;

VALUE 
------
BYTE

You can alter your session to change the value (or you can do it through the graphical interface in something like SQL Developer). You can also put an ALTER SESSION command in your LOGIN.SQL (or, globally, GLOGIN.SQL) if you use one, if you want a specific value to be assigned whenever you start a session. Otherwise, when you start a new session the default will come from your SPFile (most likely).

Here is how I can check what is in my SPFile:

select value
from   v$parameter
where  name = 'nls_length_semantics'
;


VALUE 
------
BYTE

I can also alter my system to change what's in the SPFile, but that's a DBA's job (I think). In any case, it CAN be changed.

This is similar to other NLS parameters - consider NLS_DATE_FORMAT for example, the behavior is very similar.




回答2:


Further to @mathguy's answer, if you have an existing table you can query the data dictionary to see whether the size you're seeing (presumbaly via describe or similar) is in bytes or characters:

create table t (col1 varchar2(2000 byte), col2 varchar2(2000 char));

select column_name, data_type, data_length, char_length, char_used
from user_tab_columns
where table_name = 'T';

COLUMN_NAME DATA_TYPE DATA_LENGTH CHAR_LENGTH CHAR_USED
----------- --------- ----------- ----------- ---------
COL1        VARCHAR2         2000        2000 B
COL2        VARCHAR2         4000        2000 C

where char_used is C for a character limit and B for a byte limit.

The data_length is always in bytes, as that's the real upper limit, and is capped (to 4000 in my DB - could be 32k; in 12c+ with MAX_STRING_SIZE set to EXTENDED) - so even though I've said 2000 characters are allowed in col2, if I use multibyte characters then I may actually be allowed fewer than 2000 of those, as the total number of bytes could attempt to exceed 4000. I would actually see 4000 as the data length for anything declared larger than 999 char, as it allows up to four bytes per character, for AL32UTF8.

The describe command in SQL*Plus etc. shows you the original size, and also shows you whether it is bytes or characters - but it omits that if it matches your session's NLS_LENGTH_SEMANTICS setting:

alter session set nls_length_semantics = 'BYTE';

desc t;

Name Null? Type                
---- ----- ------------------- 
COL1       VARCHAR2(2000)      
COL2       VARCHAR2(2000 CHAR) 

alter session set nls_length_semantics = 'CHAR';

desc t;

Name Null? Type                
---- ----- ------------------- 
COL1       VARCHAR2(2000 BYTE) 
COL2       VARCHAR2(2000)      

That setting is also used for the default semantics if you don't specify char or byte in the create statement, also as @mathguy explained; but as with most NLS settings it's usually better (IMO) to explicitly state it so there is no ambiguity or confusion about what will happen.



来源:https://stackoverflow.com/questions/57578188/what-is-the-default-value-of-varchar2-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!