PostgreSQL 数据类型介绍(一)

来源:互联网 发布:长风大数据平台 编辑:程序博客网 时间:2024/05/16 05:26
  • 查看当前数据库的数据类型
postgres=# \d pg_type ;        Table "pg_catalog.pg_type"     Column     |     Type     | Modifiers ----------------+--------------+----------- typname        | name         | not null typnamespace   | oid          | not null----//显示所有的type类型以及对应的存储类型:postgres=# select typname, typstorage  from pg_type ;                typname                | typstorage ---------------------------------------+------------ bool                                  | p bytea                                 | x char                                  | p name                                  | p int8                                  | p int2                                  | p int2vector                            | p int4                                  | p regproc                               | p text                                  | x oid                                   | p tid                                   | p xid                                   | p cid                                   | p oidvector                             | p pg_type                               | x pg_attribute                          | x pg_proc                               | x pg_class                              | x json                                  | x xml                                   | x _xml                                  | x关于存储类型 p x e m 的含义 ,请自行搜索,代表了各自 不同的存储方式。
  左侧为数据类型的分类

Paste_Image.png

  • 常见的数据类型,数字

Paste_Image.png

关于 serial类型,效果其实和integer + next sequence 一样。
当你创建了 serial 数据类型,其实也帮你自动创建了 序列

postgres=# create table t (id serial);CREATE TABLEpostgres=# \d+ t                                             Table "public.t" Column |  Type   |                   Modifiers                    | Storage | Stats target | Description --------+---------+------------------------------------------------+---------+--------------+------------- id     | integer | not null default nextval('t_id_seq'::regclass) | plain   |              | 可以看出,自动创建了 t_id_seq 这个序列,同时添加了 not null 约束。研究下这个序列:postgres=# \d+ t_id_seq               Sequence "public.t_id_seq"    Column     |  Type   |        Value        | Storage ---------------+---------+---------------------+--------- sequence_name | name    | t_id_seq            | plain last_value    | bigint  | 1                   | plain start_value   | bigint  | 1                   | plain increment_by  | bigint  | 1                   | plain max_value     | bigint  | 9223372036854775807 | plain min_value     | bigint  | 1                   | plain cache_value   | bigint  | 1                   | plain log_cnt       | bigint  | 0                   | plain is_cycled     | boolean | f                   | plain is_called     | boolean | f                   | plainOwned by: public.t.id
  • 常见的字符类型

Paste_Image.png

SQL 定义了两种基本的字符类型,varchar(n)和char(n),这里的n是一个正整数。两种类型都可以存储最多n个字符长的字串,试图存储更长的字串到这些类型的字段里会产生一个错误,除非超出长度的字符都是空白,这种情况下该字串将被截断为最大长度。如果没有长度声明,char等于char(1),而varchar则可以接受任何长度的字串。
char 类型,如果不够长度用 空格填充。

         注意 无论是那种字符集,这里和的单位是 字符,而不是字节,和 ORACLE不同。

text:表面是无限长度,其实最大可以支持到1个GB(依据版本而定)

           下面都是和字节有关的类型,上面则是和字符有关的类型

“char” 单字节的内部使用的类型
name 内部使用的类型

postgres=# create table t2(c1 varchar(3));字段 c1 最多允许3个“字符”,不是字节!!CREATE TABLEpostgres=# insert into t2 values ('你好呀');INSERT 0 1postgres=# insert into t2 values ('abc');INSERT 0 1postgres=# insert into t2 values ('abcd');这时候就报错了,因为abcd是4个字符。ERROR:  value too long for type character varying(3)postgres=# select  pg_column_size (c1),c1 from t2 ; pg_column_size |   c1   ----------------+--------             10 | 你好呀              4 | abc(2 rows)

-常用的事件类型

Paste_Image.png

interval :是一个时间间隔类型。

Paste_Image.png

时间的输出格式

Paste_Image.png

postgres=# show datestyle;  DateStyle ----------- ISO, MDY(1 row)postgres=# select now() ;              now              ------------------------------- 2017-05-26 09:32:35.197556+08   指的是8区(1 row)

interval 类型:

postgres=# select now()-current_date;?column?     ----------------- 09:35:29.864559(1 row)

Paste_Image.png

  • Boolean 类型
原创粉丝点击