PostgreSQL 数据类型介绍(四)

来源:互联网 发布:大数据相关的技术 编辑:程序博客网 时间:2024/05/18 15:26
  • uuid类型

    UUIDs could be generated by client applications or other libraries invoked through a server-side function.
    specifically a group of 8 digits followed by three groups of 4 digits followed by a group of 12 digits, for a total of 32 digits representing the 128 bits


*关于如何安装uuid插件,本文不作描述,请自行安装。
这里介绍的是 pgcrypto 模块的 gen_random_uuid() 函数。使用该函数之前,确保pg安装了pgcrypto扩展。*以下示例参考博客:https://segmentfault.com/a/1190000008793136
示例: 用 uuid 当作 primary key (主键)postgres=# CREATE SCHEMA IF NOT EXISTS developerworks;  CREATE SCHEMApostgres=# CREATE TABLE developerworks.contacts (  postgres(# id UUID PRIMARY KEY DEFAULT gen_random_uuid(),postgres(# name TEXT,postgres(# email TEXTpostgres(# );CREATE TABLEpostgres=# INSERT INTO developerworks.contacts (name,email) VALUES  postgres-# ('Dr Nic Williams','drnic'),postgres-# ('Brian Mattal','brian'),postgres-# ('Wayne E. Seguin','wayneeseguin'),postgres-# ('Long Nguyen','long'),postgres-# ('Bill Chapman','bill'),postgres-# ('Chris Weibel','chris'),postgres-# ('Jeremey Budnack','jrbudnack'),postgres-# ('Ruben Koster','rkoster'),postgres-# ('Jamie Van Dyke','jamie'),postgres-# ('Quintessence Anx','qanx'),postgres-# ('McGowan','mcg'),postgres-# ('高,秀娇 (XJ)','xj'),postgres-# ('Geoff Franks','geoff'),postgres-# ('Van Nguyen','vnguyen'),postgres-# ('John Longanecker','jlonganecker')postgres-# ;INSERT 0 15postgres=# SELECT * FROM developerworks.contacts;                  id                  |       name       |    email     --------------------------------------+------------------+-------------- 312b5f4a-f362-4fc4-b432-d508266014a1 | Dr Nic Williams  | drnic 4e534213-861b-47ab-b56a-ac8e5d4aa31c | Brian Mattal     | brian b1936e1c-31a1-4855-919b-54959eecde1a | Wayne E. Seguin  | wayneeseguin 01ede71d-2490-4b09-ba91-6aaeee94b7e9 | Long Nguyen      | long 5d83c888-1598-497b-9082-764d74be6edc | Bill Chapman     | bill d962ed8c-6a60-4864-a96e-c905da0e2f80 | Chris Weibel     | chris 9b5766e6-663c-4280-b901-9169cbc601f6 | Jeremey Budnack  | jrbudnack 9188ba0e-e313-4eb9-ae3b-506389db8760 | Ruben Koster     | rkoster f928c293-392c-4233-8b34-2b41d18b6e36 | Jamie Van Dyke   | jamie bbb653c7-ba44-4edd-975e-8b91d102288b | Quintessence Anx | qanx 15eff13b-dc38-47bb-843b-aa762439c6d9 | McGowan          | mcg 71dc6344-cc48-4600-a094-4639b861b4ed | 高,秀娇 (XJ)    | xj ca9ddf25-cff8-4c2a-af1f-ea8841c4b540 | Geoff Franks     | geoff e2d1f1f4-4e3a-41a9-b7aa-2111af985614 | Van Nguyen       | vnguyen 44a26489-c78f-49a5-aeae-7453073dc2f6 | John Longanecker | jlonganecker(15 rows)

创建uuid的方式有很多,这个扩展只是一种方式,可以看看其他的方式。

  • 数组类型
1.不限制长度

目前PostgreSQL未对长度强限定, 如int[]和int[10]都不会限定元素个数.
array_length(ARRAY[[1,2,3,4,5],[6,7,8,9,10]], 1)

2.不限维度

目前PostgreSQL未对维度强限定,如int[]和int[][], 效果是一样的, 都可以存储任意维度的数组

3.矩阵强制

多维数组中, 同一个维度的元素个数必须相同
正确 array[[1,2,3,4],[5,6,7,8]]
不正确 array[[1,2,3,4],[5,6,7]]

4.元素强制

元素类型必须一致
正确
array[1,2,3]
不正确
array[1,2,’abc’]

关于使用:
请参考blog:http://www.cnblogs.com/alianbog/p/5665411.html

  • 数组类型如何扩展:
一维数组的扩展postgres=# select array_append(ARRAY['digoal','francs'],'david');     array_append      ----------------------- {digoal,francs,david}(1 row)postgres=# select array_append(ARRAY['digoal','francs'],'david');     array_append      ----------------------- {digoal,francs,david}(1 row)postgres=# select array_prepend('david',ARRAY['digoal','francs']);     array_prepend     ----------------------- {david,digoal,francs}(1 row)二维数组的扩展postgres=# array_cat(ARRAY[['digoal','zhou'],['francs','tan']], ARRAY['david','guo']);postgres=# select array_cat(ARRAY[['digoal','zhou'],['francs','tan']], ARRAY['david','guo']) ;                array_cat                 ------------------------------------------ {{digoal,zhou},{francs,tan},{david,guo}}(1 row)

关于 Postgresql array类型的相关函数
这里,我刚开始不是很理解,这几个函数到底如何工作的,经过两个小时的探讨,我决定写一篇独立博客来解释明白这个问题。
请关注我相关博客: Postgres array 数组类型详细使用

  • 自定义数据类型
创建表时默认创建一个同名composite type, 因此表名和自定义类名不能重复  如:postgres=# create type test as (info text,id int,crt_time timestamp(0));ERROR:  type "test" already exists因为我之前已经创建了一个test表了,相应地创建了test类型,所以这里再次创建test类型的话,就会报错。按照如上逻辑,创建表就创建类型了,那么也就意味着我们可以使用该类型了:postgres=# create table diy(name test );//成功使用该类型。CREATE TABLE接下来:创建一个自定义类型的成功示例:postgres=# CREATE TYPE inventory_item AS( name text,supplier_id integer,price numeric);CREATE TYPE使用该自定义类型postgres=# CREATE TABLE on_hand (item inventory_item,count integer);CREATE TABLE
对含有自定义类型的表进行  增删改查操作:插入记录:ROW 在这里指定其是一个符合类型。个人感觉特别像java里的传入参数是个自定义对象的函数。postgres=# INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);INSERT 0 1查询记录:postgres=# SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price <10;    name    -----------------------+------- fuzzy dice(1 row)更新操作  更新整个item:postgres=# UPDATE on_hand SET item = ROW('fuzzy dice',10,100) WHERE count=1000;UPDATE 1更新操作 更新item类型里的某个子类型postgres=# UPDATE on_hand SET item.price = (on_hand.item).price + 100 WHERE(on_hand.item).name='fuzzy dice';UPDATE 1插入操作 (item.name, item.supplier_id) 和 VALUES('test', 2.2); 对应好就OK了。postgres=# INSERT INTO on_hand (item.name, item.supplier_id) VALUES('test', 2.2);INSERT 0 1postgres=# select * from on_hand;         item          | count -----------------------+------- ("fuzzy dice",10,200) |  1000 (test,2,)             |      (2 rows)