PostgreSQL - 数据类型

在创建表的时候, 指定字段数据类型, 那么有以下多种的类型选择:

数字类型

数字类型列表

数据类型说明
int2小范围整数 -32768 ~ +32767
int / int4整数类型 -2147483648 ~ +2147483647
int8大范围整数 -9223372036854775808 ~ 9223372036854775807
decimal用户指定的精度,精确. 小数点前最多为131072个数字; 小数点后最多为16383个数字。
numberic用户指定的精度,精确. 小数点前最多为131072个数字; 小数点后最多为16383个数字。
serial自动增加的整数 .1~2147483647

数字函数及运算符

函数类型说明
+ - * / %加 , 减 , 乘 , 除 , 取余数
round四舍五入
ceil整体进位, 取整
floor整体舍小数, 取整
  • ceil
dong=# select ceil(3.0) , ceil(3.1) , ceil(3.4) , ceil(3.5) , ceil(3.6)  , ceil(3.7) , ceil(3.9)  ;
 ceil | ceil | ceil | ceil | ceil | ceil | ceil 
------+------+------+------+------+------+------
    3 |    4 |    4 |    4 |    4 |    4 |    4
(1 row)

dong=# select ceil(-3.0) , ceil(-3.1) , ceil(-3.4) , ceil(-3.5) , ceil(-3.6)  , ceil(-3.7) , ceil(-3.9)  ;
 ceil | ceil | ceil | ceil | ceil | ceil | ceil 
------+------+------+------+------+------+------
   -3 |   -3 |   -3 |   -3 |   -3 |   -3 |   -3
(1 row)
  • floor
dong=# select floor(3.0) , floor(3.1) , floor(3.4) , floor(3.5) , floor(3.6)  , floor(3.7) , floor(3.9)  ;
 floor | floor | floor | floor | floor | floor | floor 
-------+-------+-------+-------+-------+-------+-------
     3 |     3 |     3 |     3 |     3 |     3 |     3
(1 row)



dong=# select floor(-3.0) , floor(-3.1) , floor(-3.4) , floor(-3.5) , floor(-3.6)  , floor(-3.7) , floor(-3.9)  ;
 floor | floor | floor | floor | floor | floor | floor 
-------+-------+-------+-------+-------+-------+-------
    -3 |    -4 |    -4 |    -4 |    -4 |    -4 |    -4
(1 row)

字符类型

字符型

数据类型说明
char(size)size是要存储的字符数。固定长度字符串,右边的空格填充到相等大小的字符。
varchar(size)可变长度字符串。
text无长度限制

字符函数

测试环境准备

## 创建测试表  
dong=# create table test_char
dong-#  (c1 char(4) ,      ## 定长类型
dong(#   c2 varchar(4));   ## 可变长类型
CREATE TABLE
 
## 插入测试数据
dong=# insert into test_char values ('a' , 'a');
dong=# insert into test_char values ('dong','SQL');

## 查询数据
dong=# select * from test_char;
  c1  | c2 
------+----
 a    | a
 dong | SQL
函数类型说明
length字符数
octet_length实际占用字节数
position指定字符在字符串的位置
substring类似substr函数, 截取指定位置字符串
split_part拆分字符串
  • length
dong=# select length(c1) , length(c2) from test_char;
 length | length 
--------+--------
      1 |      1
  • octet_length
dong=# select octet_length(c1) , octet_length(c2) from test_char;
 octet_length | octet_length 
--------------+--------------
            4 |            1
  • position
dong=# select position('o' in 'postgres') ;
 position 
----------
        2
  • substring
dong=# select substring('postgres' from 3 for 4) ;
 substring 
-----------
 stgr
(1 row)
  • split_part
dong=# select split_part('xxxxxx@gmail.com' , '@' , 1 );
 split_part 
------------
 xxxxxx


dong=# select split_part('xxxxxx@gmail@.com' , '@' , 2);
 split_part 
------------
 gmail

时间\日期类型

时间\日期的数据类型

dong=# select now();		###### 显示当前日期 & 时间 & 带时区
              now              
-------------------------------
 2020-03-02 18:36:22.027928+08
(1 row)

dong=# select now()::timestamptz(0) ; ###### 显示当前日期 & 时间 & 带时区(小数精度为0) (可不加)
          now           
------------------------
 2020-03-02 18:42:03+08
(1 row)

dong=# select now()::date ; ###### 显示当前日期
    now     
------------
 2020-03-02
(1 row)

dong=# select now()::time;	###### 显示当前时间
       now       
-----------------
 18:36:57.719849
(1 row)


dong=# select now()::timestamp ;  ###### 显示当前日期 & 时间 & 不带时区
            now             
----------------------------
 2020-03-02 18:38:06.249962
(1 row)

dong=# select now()::date , (now()+interval '1 day')::date;
    now     |    date    
------------+------------
 2020-03-02 | 2020-03-03

时间\日期操作及函数

  • 日期之间的加减乘除
  • 获取当前日期, 时间 current_datecurrent_time
dong=# select now()::date , current_date;
    now     | current_date 
------------+--------------
 2020-03-02 | 2020-03-02
(1 row)

dong=# select now()::time , current_time;
       now       |    current_time    
-----------------+--------------------
 18:46:38.178901 | 18:46:38.178901+08
  • 从日期和时间中 , 获取年\月\日\时\分\秒 extract( field FROM source )
fieldsource
yearnow()
monthtimestamp
daytime
hourinterval表达式
minute
second
week 当前日期所在年份的第几周
doy 当前日期所在年份的第几周天
dong=# select extract(year from now());
 date_part 
-----------
      2020
(1 row)

布尔型

boolean类型的数据只有 两种. (可以是null 空值)

tf
truefalse
TRUEFALSE
yesno
yn
10

网络类型

网络IP数据类型

字符类型说明
cidr更为严谨的IPv4 , IPv6 . 会输出子网掩码
inet不校验子网掩码的IPv4 , IPv6
## 错误示范
dong=# select '192.168.56.11/24'::cidr;
ERROR:  invalid cidr value: "192.168.56.11/24"
LINE 1: select '192.168.56.11/24'::cidr;
               ^
DETAIL:  Value has bits set to right of mask.

## 正确演示
dong=# select '192.168.56.11'::cidr;
       cidr       
------------------
 192.168.56.11/32
(1 row)

网络地址函数

函数说明
text( )获取IP和掩码 , 返回文本格式
host( )获取IP , 返回文本格式
netmast获取掩码 , 返回文本格式
dong=# select text('192.168.56.11'::cidr) ;
       text       
------------------
 192.168.56.11/32
(1 row)

dong=# select host('192.168.56.11'::cidr) ;
     host      
---------------
 192.168.56.11
(1 row)

dong=# select netmask('192.168.56.11'::cidr) ;
     netmask     
-----------------
 255.255.255.255
(1 row)

数组类型

P56

范围类型

P60

JSON类型

P63

数据类型转换

1 函数转换

这些函数的用法与Oracle的转换函数用法一样, 不做过多的赘述了

函数说明
to_char()把时间 , 数字 , 间隔 转换为字符类型
to_date()把字符串类型转换为时间类型
to_number()把字符串转换为数字类型
to_timestamp把字符串转换为时间戳类型

2 通过:: 操作符转换

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值