PostgreSQL数据类型-二进制数据和字符串数据类型与字符串函数

巧了我就是萌 提交于 2020-02-29 12:35:05

PostgreSQL支持3种字符串类型,分别是character varying(n)、character(n)和text,character varying(n)可以写成varchar(n),character(n)可以写成char(n),最大长度1GB,text最大长度无限制。n是实际字符数量。

大部分情况下,推荐使用text和varchar(n)。

PostgreSQL支持一种二进制类型bytea。

二进制数据有图片(JPG、PNG等)、音乐(MP3、WMA等)格式文件,和字符串文件区别在于文本文件需要复合文件编码和储存可见字符,二进制文件没有类似限制,字符串适合储存文本文件。

---字符串练习
postgres=# create table testtext(testtext char(2),testvarchar varchar(3), testchar text);
CREATE TABLE
postgres=#
postgres=#
postgres=# insert into testtext values('1','111','111'),('0','000','011777');
INSERT 0 2
postgres=# select * from testtext;
 testtext | testvarchar | testchar
----------+-------------+----------
 1        | 111         | 111
 0        | 000         | 011777
(2 行记录)

postgres=#

插入长度超过预定值是,提示错误信息。

下面演示部分常见字符串函数。

---字符串拼接
postgres=# select 'N'||'O';
 ?column?
----------
 NO
(1 行记录)

postgres=# select 'Hello'||' '||'World';
  ?column?
-------------
 Hello World
(1 行记录)

postgres=#

根据以上代码字符串可以拼接2个以上字符串。

---1Byte=8Bite
postgres=# select bit_length('A');
 bit_length
------------
          8
(1 行记录)

postgres=#

检查字符串byte长度。

postgres=# select char_length('A');
 char_length
-------------
           1
(1 行记录)

postgres=# select char_length('Hello world.');
 char_length
-------------
          12
(1 行记录)

postgres=#

字符串转换为小写。

postgres=# select lower('Hello');
 lower
-------
 hello
(1 行记录)

postgres=# select lower('HI');
 lower
-------
 hi
(1 行记录)

postgres=#

字符串转换为大写。

postgres=# select upper('Hello');
 upper
-------
 HELLO
(1 行记录)

postgres=# select upper('Hi');
 upper
-------
 HI
(1 行记录)

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