PostgreSQL自学笔记:5 数据类型和运算符

5 数据类型和运算符

5.1 PostgreSQL 数据类型介绍

5.1.1 整数类型

整型类型 字节 取值范围
smallint 2字节 -2^15 ~ 2^15
int integer 4字节 -2^31 ~ 2^31
bigint 8字节 -263~263

5.1.2 浮点类型

类型名称 说明 存储需求
real 6位十进制数字精度 4字节
double precision 15位十进制数字进度 8字节
  • Infinity表示正无穷大,
  • -Infinity表示负无穷大,
  • NaN表示不是一个数字

5.1.3 任意精度类型

numeric(m,n)
m表示总共位数,n表示小数位数

5.1.4 日期与时间类型

  • now() 当前时间

    • cookie:
      PostgreSQL的now()比MySQL更具体
  • time:
    一日内的时间,00:00:00~24:00:00,8字节

    • 创建:
      create table tmp4(t time);

    • 插入:
      insert into tmp4 values('10:05:05'),('23:23');
      insert into tmp4 values(now());

    • 查询:
      select * from tmp4;

  • date:
    只用于日期,4713BC~5874879AD,4字节

    1. YYYY-MM-DD 或者 YYYYMMDD
      2012-12-31 20121231

    2. YY-MM-DD 或者 YYMMDD
      00~69 = 2000~2069
      70~99 = 1970~1999
      12-12-31 : 2012-12-31
      981231 : 1998-12-31

    3. current_date 或者 now()插入当前系统时间

    • 创建:
      create table tmp5(d date);
    • 插入:
      insert into tmp5 values ('20190114'),('190115')
          ,(now()),(current_date);
      
    • 查询:
      select * from tmp5;
  • timestamp:
    日期和时间,4713BC~5874879AD,8字节

    • 创建:
      create table tmp7(ts timestamp);
    • 插入:
      insert into tmp7 values('190116 13:44:00'),(now())
    • 查询:
      select * from tmp7;

5.1.5 字符串类型

  • char(n)和character(n):
    固定长度非二进制字符串,不足补空白

  • varchar(n)和character varying(n):
    变长非二进制字符串,有长度限制
    超过长度限制会报错

    create table tmp8(ch char(4),vch varchar(4));
    
    insert into tmp8 values('ab','ab'),
        ('abcd','abcd'),('ab  ','ab  ');
    
    select concat('|',ch,'|'),concat('|',vch,'|') from tmp8;
    select * from tmp8;
    
  • text:
    变长非二进制字符串,无长度限制

    create table tmp9(te text);
    insert into tmp9 values('ab'),('abcd'),('ab  ');
    select concat('|',te,'|') from tmp9;
    
  • cookie:
    PostgreSQL中字符串用单引号,MySQL中单双引都可以

5.1.6 二进制类型

PostgreSQL提供了bytea类型,用于存储二进制字符串,存储空间为4字节
加上实际的二进制字符串

create table tmp10(b bytea);
insert into tmp10 values(e'\000');
select * from tmp10;

5.1.7 布尔类型

create table tmp11(b boolean);
insert into tmp11 values(true),(false),('y'),('no'),('0');
select * from tmp11;

5.1.8 数组类型

  1. 数组声明
    numb int[]; 一维int数组
    xuehao text[][]; 二维text数组
    zuoye text[4][4]; 二维text数组,并且声明了数组的长度

    pay_by_quarter int array[5];

  2. 插入数组数值
    create table tmp12(bt int[]);
    insert into tmp12 values('{{1,2,3},{4,5,6},{7,8,9}}');
    select * from tmp12;

5.3 常见运算符介绍

5.3.1 运算符概述

  1. 算术运算符
    + - * / %

    • cookie:
      MySQL中 / 是除法,
      PostgreSQL中 / 是地板除
  2. 比较运算符
    > < = >= <= !=
    in between and greatest least like

  3. 逻辑运算符
    not and or

  4. 位操作符
    位与& 位或| 位非~ 位异或^ 左移<< 右移>>

5.3.2 算术运算符

create table tmp14(num int);
insert into tmp14 values(64);
select num,num+10,num-10,num*2,num/3,num%3 from tmp14;

5.3.3 比较运算符

  1. =
    判断数字字符串表达式是否相等
    select 1=0,'2'=2,'b'='b',(1+3)=(2+1),null=null;

    1. 若有一个参数参数为null,则比较运算符为空
    2. 若一个字符串和数字进行相等判断,则将字符串转换为数字
  2. <> 或 != ,不能用于判断空值null

  3. < <= > >= ,,不能用于判断空值null

  4. between and
    expr between min and max
    expr 不大于max不小于min

    select 'x' between 'f' and 'g', 'b' between 'a' and 'c';

  5. least
    least(值1,值2,...,值n)
    返回最小值,空值忽略不计

    select least(1,2,3),least('a','b','c'),least(10,null);

  6. greatest
    greatest(值1,值2,...,值n)
    返回最大值,空值忽略不计

  7. in not in

  8. like
    匹配字符串
    expr like 匹配条件

    • % 匹配人数数目的字符,包括零字符
    • _ 只能匹配一个字符

    select 'stud' like 'stu_','stud' like '%d','s' like null;

    • 与null匹配时,结果为空值null

5.3.4 逻辑运算符

  1. not
    select not '1',not 'y',not '0',not 'n';
  2. and
    select '1' and 'y','1' and '0','y' and '0';
  3. or
    select '1' or 'y','1' or '0','y' or '0';
  • cookie:
    • MySQL中'y'不能表示true,字符串为false,
      数值类型时间类型为true
    • PostgreSQL中'yes''y''1'能表示true,
      'no''n''0'能表示false,其他值会报错

5.3.5 运算符的优先级

从低到高如下:

  • =(赋值运算), :=
  • or
  • and
  • not
  • between and, case, when, then, else
  • =(比较运算), >=, >, <, <=, <>, !=, is, like, in
  • -, +
  • *, /, %
  • -(负号)
  • !
原文地址:https://www.cnblogs.com/wangbaby/p/10289492.html