---
title: "postgres tip"
author: "kirin"
date: "2007-08-08T17:25:00Z"
tags: []
slug: "postgres-tip"
draft: false
lastmod: "2007-08-08T17:25:00Z"
id: 110211
url: "https://kirin.iteye.com/blog/110211"
---

postgres数据库

Table test 

 test\_id

**\[PK] int8**

a

 **varchar**

b

**int8**

12 3 4 2 5 5

select * from test where (b+1)= 6 ;               --OK

select * from test where (a+1)= 6 ;               --ERROR:  operator does not exist: character varying + integer

必须把a转型

select * from test where (int4(a)+1)= 6

连接字符串用 ||          
select * from test where (a || '2') = '12';

不等于用  &lt;&gt;          
select * from test where (a || '2') &lt;&gt; '12';

**数值类型**

名字 存储空间 描述 范围 `smallint` 2 字节 小范围整数 -32768 到 +32767 `integer` 4 字节 常用的整数 -2147483648 到 +2147483647 `bigint` 8 字节 大范围的整数 -9223372036854775808 到 9223372036854775807 `decimal` 变长 用户声明精度，精确 无限制 `numeric` 变长 用户声明精度，精确 无限制 `real` 4 字节 变精度，不精确 6 位十进制数字精度 `double precision` 8 字节 变精度，不精确 15 位十进制数字精度 `serial` 4 字节 自增整数 1 到 +2147483647 `bigserial` 8 字节 大范围的自增整数 1 到 9223372036854775807

一般只有在磁盘空间紧张的时候才使用 `smallint (即INT2)`。而只有在 `integer` (即INT4)的范围不够的时候才使用 `bigint(即INT8)`，因为前者绝对快得多。总结：多用INT4

oracle里没bool类型，所以在设计postgrs的数据表时候也不用bool，而用varchar或者INT4，这是为了以后转换数据库方便。客户没钱就用postgres数据库，有钱就用oracle，转换数据库有可能发生。