SQL> create table t
2 (char_column char(20),
3 varchar2_column varchar2(20));
2 (char_column char(20),
3 varchar2_column varchar2(20));
Table created.
SQL> insert into t values('Hello World','Hello World');
1 row created.
SQL> select * from t;
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
SQL> select length(char_column),length(varchar2_column) from t;
LENGTH(CHAR_COLUMN) LENGTH(VARCHAR2_COLUMN)
------------------- -----------------------
20 11
------------------- -----------------------
20 11
SQL> select * from t where char_column='Hello World';
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
--此处,字面值'Hello World'有做提升,加了9个空格。补空格就涉及到NULL在行中的存储。
SQL> select * from t where char_column=varchar2_column;
SQL> select * from t where char_column=varchar2_column;
no rows selected
SQL> select * from t where trim(char_column)=varchar2_column;
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
--加trim很容易导致列上的索引失效(当然可以通过建函数索引解决)
SQL> select * from t where char_column=rpad(varchar2_column,20);
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
--在plsql中,对于特定类型的变量,没有做提升
SQL> variable varchar2_bv varchar2(20)
SQL> exec :varchar2_bv :='Hello World';
SQL> variable varchar2_bv varchar2(20)
SQL> exec :varchar2_bv :='Hello World';
PL/SQL procedure successfully completed.
SQL> select * from t where char_column=:varchar2_bv;
no rows selected
SQL> select * from t where varchar2_column=:varchar2_bv;
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
SQL> variable char_bv char(20)
SQL> exec :char_bv := 'Hello World';
SQL> exec :char_bv := 'Hello World';
PL/SQL procedure successfully completed.
SQL> select * from t where char_column= :char_bv;
CHAR_COLUMN VARCHAR2_COLUMN
-------------------- --------------------
Hello World Hello World
-------------------- --------------------
Hello World Hello World
SQL> select * from t where varchar2_column= :char_bv;
no rows selected
TOM的9i&10g编程艺术中,在12.2.2节中,"在数据库块上,最前面都有一个1~3字节的长度字段,其后才是数据",应该是在每行的每个列值前面,有1或3个字节表示列值的长度。null的存储未说明表中有long类型时的特殊情况。详见concept。
有字节和字符两种模式,对于多字节字符集,应注意。
varchar2的最大字节长度是 4000,char的最大字节长度是2000,当pl/sql中的 varchar2最大为32k.
SQL> declare
2 l_data varchar2(4000 char);
3 l_ch varchar2(1) := unistr('\00d6');
4 begin
5 l_data := rpad(l_ch,4000,l_ch);
6 insert into t (a) values(l_data);
7 end;
8 /
2 l_data varchar2(4000 char);
3 l_ch varchar2(1) := unistr('\00d6');
4 begin
5 l_data := rpad(l_ch,4000,l_ch);
6 insert into t (a) values(l_data);
7 end;
8 /
PL/SQL procedure successfully completed
单字节字符集的可以。
单字节字符集的可以。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20648244/viewspace-667703/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20648244/viewspace-667703/