十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本節(jié)介紹了Oracle和PG在字符類型上面的部分不同點,具體包括數(shù)據(jù)存儲方式、實際占用的空間大小以及查詢返回數(shù)據(jù)的處理方式等。
山陰網(wǎng)站建設公司創(chuàng)新互聯(lián),山陰網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為山陰上1000家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設要多少錢,請找那個售后服務好的山陰做網(wǎng)站的公司定做!
數(shù)據(jù)存儲
Oracle
常規(guī)定義的長度,單位為Byte,如使用(N char)定義則為字符
如:
-- 以字節(jié)為單位,實際存儲長度為字節(jié)數(shù)
create table t1(c1 varchar2(2));
-- 以字符為單位,實際存儲長度與字符&字符集有關
-- 如GBK字符集,漢字字符2個字節(jié),ASCII碼1個字節(jié),大小需根據(jù)實際的存儲數(shù)據(jù)而定
create table t2(c1 varchar2(2 char)); -- 以字符為單位
PG
定義的長度,單位為字符
實際存儲長度與字符&字符集有關,如UTF8字符集,漢字3個字節(jié),ASCII嗎1個字節(jié),大小需根據(jù)實際的存儲數(shù)據(jù)而定
查詢返回數(shù)據(jù)
CHAR類型
Oracle返回實際存儲的數(shù)據(jù),包括數(shù)據(jù)庫自動補足的空格;PG返回的數(shù)據(jù)會去掉后面的空格,不管是數(shù)據(jù)庫自動補足的還是數(shù)據(jù)本身存在的空格.
空串
Oracle把空串視為NULL值,PG則不會.
PG
UTF8字符集
testdb=# create table tbl1(var varchar(2),fixed char(2));
CREATE TABLE
testdb=# insert into tbl1 values('測試','測試');
INSERT 0 1
testdb=# insert into tbl1 values('測1','測1');
INSERT 0 1
testdb=# insert into tbl1 values('測','測');
INSERT 0 1
testdb=# select lpad(var,2,'*'),octet_length(var),lpad(fixed,2,'*'),octet_length(fixed) from tbl1;
lpad | octet_length | lpad | octet_length
------+--------------+------+--------------
測試 | 6 | 測試 | 6
測1 | 4 | 測1 | 4
*測 | 3 | *測 | 4
(3 rows)
testdb=# create table tbl2(var varchar(4),fixed char(4));
CREATE TABLE
testdb=# insert into tbl2 values('測試','測試 ');
INSERT 0 1
testdb=# select lpad(fixed,4,'*'),octet_length(fixed) from tbl2;
lpad | octet_length
--------+--------------
**測試 | 8
(1 row)
Oracle
GBK字符集
TEST-orcl@server4>create table tbl1(var varchar2(2),fixed char(2));
Table created.
TEST-orcl@server4>insert into tbl1 values('測試','測試');
insert into tbl1 values('測試','測試')
*
ERROR at line 1:
ORA-12899: value too large for column "TEST"."TBL1"."VAR" (actual: 4, maximum:
2)
TEST-orcl@server4>insert into tbl1 values('1','1');
1 row created.
TEST-orcl@server4>select lpad(var,2,'*'),lengthb(var),lpad(fixed,2,'*'),lengthb(fixed) from tbl1;
LPAD LENGTHB(VAR) LPAD LENGTHB(FIXED)
---- ------------ ---- --------------
*1 1 1 2
TEST-orcl@server4>create table tbl2(var varchar2(2 char),fixed char(2 char));
Table created.
TEST-orcl@server4>
TEST-orcl@server4>insert into tbl2 values('測試','測試');
1 row created.
TEST-orcl@server4>insert into tbl2 values('1','1');
1 row created.
TEST-orcl@server4>select lpad(var,2,'*'),lengthb(var),lpad(fixed,2,'*'),lengthb(fixed) from tbl2;
LPAD LENGTHB(VAR) LPAD LENGTHB(FIXED)
---- ------------ ---- --------------
測 4 測 4
*1 1 1 2
TEST-orcl@server4>select lpad(var,4,'*'),lengthb(var),lpad(fixed,4,'*'),lengthb(fixed) from tbl2;
LPAD(VAR LENGTHB(VAR) LPAD(FIX LENGTHB(FIXED)
-------- ------------ -------- --------------
測試 4 測試 4
***1 1 **1 2
TEST-orcl@server4>