空字串和空格字串在informix和oralce 的差異

liahtobjtosh發表於2009-10-09
空字串和空格字串在informix和oralce 的差異[@more@]

informix:操作如下:
> CREATE TABLE tt ( c1 int,c2 VARCHAR(8),UNIQUE (c1,c2) CONSTRAINT u_tt );

Table created.

> insert into tt(c1,c2) values(1,' '); -- 兩個空格,' '被解釋為''

1 row(s) inserted.

> insert into tt(c1,c2) values(1,' '); --一個空格,' '被解釋為'',所以出錯

268: Unique constraint (informix.u_tt) violated.

100: ISAM error: duplicate value for a record with unique key.
Error in line 1
Near character position 33
> insert into tt(c1) values(2);

1 row(s) inserted.

> select * from tt;


c1 c2

1
2

2 row(s) retrieved.

> select * from tt where c2 ='';


c1 c2

1

1 row(s) retrieved.

> select * from tt where c2 = ' '; --一個空格' '被解釋為''


c1 c2

1

1 row(s) retrieved.

> select * from tt where c2 = ' '; --二個空格' '被解釋為''


c1 c2

1

1 row(s) retrieved.

> select * from tt where c2 is null;


c1 c2

2

1 row(s) retrieved.

> select c1,length(c2) from tt;


c1 (expression)

1 0
2

2 row(s) retrieved.

>
可見informix 把 '' 與 ' ' 與 ' ' 是同等對待,將其看成''


oracle:

操作如下:

SQL> CREATE TABLE tt ( c1 int,c2 VARCHAR(8), CONSTRAINT u_tt UNIQUE (c1,c2));

表已建立。

SQL> insert into tt(c1,c2) values(1,' ');--兩個空格

已建立 1 行。

SQL> insert into tt(c1,c2) values(1,''); --沒有空格

已建立 1 行。

SQL> insert into tt(c1) values(2); --c2被填充為null

已建立 1 行。

SQL> select * from tt;

C1 C2
---------- ----------------
1
1
2

SQL> select c1,nvl(c2,'null') from tt;-- 可見,''被解釋成null

C1 NVL(C2,'NULL')
---------- ----------------
1
1 null
2 null

SQL> select * from tt where c2 =''; --因為表中的''被解釋成null,所以沒有查到

未選定行

SQL> select * from tt where c2 is null;

C1 C2
---------- ----------------
1
2

SQL> select * from tt where c2 = ' ';

C1 C2
---------- ----------------
1

SQL>

從上可見。ORACLE把''解釋成 NULL。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22565551/viewspace-1027676/,如需轉載,請註明出處,否則將追究法律責任。

相關文章