日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

oracle类似isempty,NULLs和empty strings在不同数据库的中特点

發布時間:2023/12/4 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle类似isempty,NULLs和empty strings在不同数据库的中特点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.以oracle為例

SQL> create table test(id int primary key,content varchar(20));

SQL> INSERT INTO test (id, content) VALUES (1, NULL);

SQL> INSERT INTO test (id, content) VALUES (2, '');

SQL> INSERT INTO test (id, content) VALUES (3, ' ');

SQL> INSERT INTO test (id, content) VALUES (4, 'x');

SQL> select * from test;

ID CONTENT

---------- --------------------

1

2

3

4 x

SQL> SELECT ID,CONTENT,

case when content is null then 1 else 0 end as isnull,

case when content = '' then 1 else 0 end as isempty,

case when content = ' ' then 1 else 0 end as blank

from

test;

ID CONTENT ISNULL ISEMPTY BLANK

---------- -------------- ---------- ---------- ----------

1 1 0 0

2 1 0 0

3 0 0 1

4 x 0 0 0

SQL> select id,content,length(content) from test;

ID CONTENT LENGTH(CONTENT)

---------- -------------------- ---------------

1

2

3 1

4 x 1

SQL>

從結果可以看到,empry string被插入表中時,被當做NULL對待。因此,empty strings不會在數據庫中存儲。?單個空格是不會被轉換的,因為不是一個empty string。

2.以mysql為例

>create table test(id int primary key,content varchar(20));

>INSERT INTO test (id, content) VALUES (1, NULL);

>INSERT INTO test (id, content) VALUES (2, '');

>INSERT INTO test (id, content) VALUES (3, ' ');

>INSERT INTO test (id, content) VALUES (4, 'x');

>select * from test;

+----+---------+

| id | content |

+----+---------+

| 1 | NULL |

| 2 | |

| 3 | |

| 4 | x |

+----+---------+

4 rows in set (0.00 sec)

>SELECT ID,CONTENT,

case when content is null then 1 else 0 end as isnull,

case when content = '' then 1 else 0 end as isempty,

case when content = ' ' then 1 else 0 end as blank

from

test;

+----+---------+--------+---------+-------+

| ID | CONTENT | isnull | isempty | blank |

+----+---------+--------+---------+-------+

| 1 | NULL | 1 | 0 | 0 |

| 2 | | 0 | 1 | 1 |

| 3 | | 0 | 1 | 1 |

| 4 | x | 0 | 0 | 0 |

+----+---------+--------+---------+-------+

4 rows in set (0.00 sec)

>select id,content,length(content) from test;

+----+---------+-----------------+

| id | content | length(content) |

+----+---------+-----------------+

| 1 | NULL | NULL |

| 2 | | 0 |

| 3 | | 1 |

| 4 | x | 1 |

+----+---------+-----------------+

可以看到NULL和empty string是不同的。而empty string和空格string被認為是相同的,但是在計算長度的時候卻又不同了。

3.以pg為例

postgres=# create table test(id int primary key,content varchar(20));

postgres=# INSERT INTO test (id, content) VALUES (1, NULL);

postgres=# INSERT INTO test (id, content) VALUES (2, '');

postgres=# INSERT INTO test (id, content) VALUES (3, ' ');

postgres=# INSERT INTO test (id, content) VALUES (4, 'x');

postgres=# select * from test;

id | content

----+---------

1 |

2 |

3 |

4 | x

(4 rows)

postgres=# SELECT ID,CONTENT,

case when content is null then 1 else 0 end as isnull,

case when content = '' then 1 else 0 end as isempty,

case when content = ' ' then 1 else 0 end as blank

from test;

id | content | isnull | isempty | blank

----+---------+--------+---------+-------

1 | | 1 | 0 | 0

2 | | 0 | 1 | 0

3 | | 0 | 0 | 1

4 | x | 0 | 0 | 0

(4 rows)

postgres=# select id,content,length(content) from test;

id | content | length

----+---------+--------

1 | |

2 | | 0

3 | | 1

4 | x | 1

(4 rows)

postgres=#

看前兩行,NULL被插入后仍被當做NULL,不能當做empty string。從第二行可以看到,插入的empty string沒有被當做NULL,仍然是一個empty string。

NULLs和non-NULLs

(1)oracle數據庫

SQL> SELECT id, content,

content || NULL AS concatnull,

content || 'x' AS concatchar

FROM test;

ID CONTENT CONCATNULL CONCATCHAR

---------- -------------------- -------------------- ---------------------

1 x

2 x

3 x

4 x x xx

SQL>

在oracle中,NULLs和字符相連接后,輸出結果是字符。

(2)mysql數據庫

>SELECT id, content,

content || NULL AS concatnull,

content || 'x' AS concatchar

FROM test;

+----+---------+------------+------------+

| id | content | concatnull | concatchar |

+----+---------+------------+------------+

| 1 | NULL | NULL | NULL |

| 2 | | NULL | 0 |

| 3 | | NULL | 0 |

| 4 | x | NULL | 0 |

+----+---------+------------+------------+

mysql中可以用concat拼接多個,但用||無法拼接字符串,會顯示零。

>SELECT id, content,

concat(content,NULL) AS concatnull,

concat(content,'x') AS concatchar

FROM test;

+----+---------+------------+------------+

| id | content | concatnull | concatchar |

+----+---------+------------+------------+

| 1 | NULL | NULL | NULL |

| 2 | | NULL | x |

| 3 | | NULL | x |

| 4 | x | NULL | xx |

+----+---------+------------+------------+

NULL和non-NULLS拼接結果是NULL

(3)pg數據庫

postgres=# SELECT id, content,

postgres-# content || NULL AS concatnull,

postgres-# content || 'x' AS concatchar

postgres-# FROM test;

id | content | concatnull | concatchar

----+---------+------------+------------

1 | | |

2 | | | x

3 | | | x

4 | x | | xx

(4 rows)

postgres=#

在pg中,NULLs和字符相連接后,NULL出現在任何一個值中都意味著結果是NULL作為輸出值,而不管它連接的是什么。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的oracle类似isempty,NULLs和empty strings在不同数据库的中特点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。