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

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=#

转载于:https://my.oschina.net/u/1011130/blog/1568102

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值