正文
包含postgresqlreplace的词条
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
请问 postgresql中有 replace into或者 Merge into 这样的功能的语句吗 我想实现这样的功能啊
没有这样的功能,前几天才找到一个存储过程,不过我没用过
CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
LOOP
UPDATE db SET b = data WHERE a = key;
IF found THEN
RETURN;
END IF;
BEGIN
INSERT INTO db(a,b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- do nothing
END;
END LOOP;
END;
$$
LANGUAGE plpgsql;
SELECT merge_db(1, 'david');
SELECT merge_db(1, 'dennis');
postgresql 创建带参数函数
在postgreSQL中用select语句就可以调用函数了,包括自定义函数:
select * from your_function(param1, param2, ...) s;
如果你不需要函数的返回值,也可以用perform语句来调用:
perform your_function(param1, param2, ..);
PostgreSQL如果不知道函数的返回类型,怎么处理
postgreSQL的JDBC驱动是支持数组返回的,数组对应的类型为org.postgresql.jdbc4.Jdbc4Array(我用的是jdbc4).
一个示例, 函数如下:
create or replace function fn_rtbAry()
returns integer[]
as $fn_rtbAry$
begin
return '{10, 20, 30}'::integer[];
end;
$fn_rtbAry$ language plpgsql;
Java中获得返回的数组, 要引入java.lang.*,java.sql.*,java.util.*,org.postgresql.jdbc4.*:
Connection conn = null;
Statement cmd = null;
ResultSet rs = null;
Jdbc4Array obj = null;
Integer[] dataAry = null;
// ... 连接,查询创建以及初始化代码省略
rs = cmd.executeQuery("select fn_rtbAry() as v");
// 循环获得数据
while (rs.next()) {
obj = (Jdbc4Array)rs.getObject(1);
dataAry = (Integer[])obj.getArray(); // 这里获得函数返回的数组,并转换成Integer数组
// 其他代码省略
}
// 释放对象代码省略
PostgreSQL 执行函数错误
初步认为是传入的事件类型参数不匹配,定义可以用timestamp,传入时直接用字符串,比如:'2012-8-8 00:00:00'
now()返回的是timestamp类型,所以问题出在now()这里
最好的解决办法就是函数定义的date参数改为timestamp
希望对你有所帮助
PostgreSQL中使用UUID
UUID(Universal Unique Identifier)或者 GUID(Globally Unique Identifier)是一个 128 比特的数字,可以用于唯一标识每个网络对象或资源。由于它的生成机制,一个 UUID 可以保证几乎不会与其他 UUID 重复,因此常常用于生成数据库中的主键值。
1.pgcrypto 模块提供的 uuid
PostgreSQL 提供了一个用于加/解密的扩展模块 pgcrypto,其中的 gen_random_uuid() 函数可以用于返回一个 version 4 的随机 UUID。
2.uuid-ossp 模块提供的 uuid
uuid-ossp模块提供函数使用几种标准算法之一产生通用唯一标识符(UUID)。还提供产生某些特殊 UUID 常量的函数。
1.将当前目录转移到 PostgreSQL 源代码目录下的 contrib;如:
2.执行如下命令来安装扩展模块
如果要安装 uuid-ossp 模块,需要在执行安装扩展模块之前,执行 configure 并添加 --with-uuid=xxx,xxx取值为:
然后再执行安装扩展模块的命令。
3.检查是否安装,在 PostgreSQL 的安装目录下的 /share/extension 目录下,查看是否有模块相关的文件。如:
注: gen_random_uuid() 从 PostgreSQL 13 开始成为了一个内置函数
如果您所使用的PostgreSQL版本在13以上,则不需要执行如下语句:
生成uuid:
如果想要生成没有中划线(-)的 UUID 字符串,可以使用 REPLACE 函数:
查看包含的函数:
执行如下命令生成 uuid:
postgresql----数组类型和函数
postgresql支持数组类型,可以是基本类型,也可以是用户自定义的类型。日常中使用数组类型的机会不多,但还是可以了解一下。不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从1开始,既可以指定长度,也可以不指定长度。且postgresql既支持一维数组,也支持多维数组,但是平时二维数组也就够用了。
示例1.使用ARRAY构建数组类型
---1*4的一维数组test=#selectarray[1,2,3,4];
array -----------{1,2,3,4}
(1 row)--2*2的二维数组test=#selectarray[[1,2],[3,4]];
array ---------------{{1,2},{3,4}}
(1 row)--1*2的二维数组,基本类型为box矩形类型,且和上面相比box类型元素之间是以分号分隔的,其他所有类型的数据都是以逗号分隔的test=#selectarray[box'(1,1),(0,0)',box'(0,0),(-1,-1)'];
array -----------------------------{(1,1),(0,0);(0,0),(-1,-1)}
(1row)
示例2.创建一张表,字段包含数组类型
其中int[]表示数组长度无限制,int[4]表示数组长度为4.
test=#createtabletbl_array(aint[],bvarchar(32)[][],cint);CREATETABLEtest=#insertintotbl_array (a,b,c)values(array[1,2],array[[1,2,3],[4,5,6]],1);INSERT01test=#insertintotbl_array (a,b,c)values(array[1,2,3],array[[1,2],[4,5]],2);INSERT01test=#select*from tbl_array ;
a |b| c ---------+-------------------+---{1,2}|{{1,2,3},{4,5,6}}|1 {1,2,3}|{{1,2},{4,5}}|2(2 rows)
test=#selecta[1],b[2]fromtbl_arraywherec=1;
a | b ---+---1|
(1 row)
test=#selecta[1],b[2][1]fromtbl_arraywherec=1;
a | b ---+---1|4(1 row)
test=#selecta[1],b[2][4]fromtbl_arraywherec=1;
a | b ---+---1|
(1row)
test=#updatetbl_arrayseta[1]=200wherea[1]=1;UPDATE1test=#selecta[1],b[2][4]from tbl_array ;
a | b -----+---100|200|
(2rows)
也可以使用[下标:上标]方式来查询数组连续的某些元素。
test=#selecta[2:3]from tbl_array ;
a -------{2}
{2,3}
(2 rows)
test=#selecta[1:3]from tbl_array ;
a -----------{100,2}
{200,2,3}
(2rows)
数组操作符与函数
操作符
操作符描述示例结果
=相等 SELECT ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]; t
不等于 select ARRAY[1,2,3] ARRAY[1,2,4]; t
小于 select ARRAY[1,2,3] ARRAY[1,2,4]; t
大于 select ARRAY[1,4,3] ARRAY[1,2,4]; t
=小于或等于 select ARRAY[1,2,3] = ARRAY[1,2,3]; t
=大于或等于 select ARRAY[1,4,3] = ARRAY[1,4,3]; t
@包含 select ARRAY[1,4,3] @ ARRAY[3,1]; t
@包含于 select ARRAY[2,7] @ ARRAY[1,7,4,2,6]; t
重叠(是否有相同元素) select ARRAY[1,4,3] ARRAY[2,1]; t
||数组与数组连接 select ARRAY[1,2,3] || ARRAY[4,5,6]; {1,2,3,4,5,6}
||数组与数组连接 select ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]; {{1,2,3},{4,5,6},{7,8,9}}
||元素与数组连接 select 3 || ARRAY[4,5,6]; {3,4,5,6}
||数组与元素连接 select ARRAY[4,5,6] || 7; {4,5,6,7}
函数
函数返回类型描述示例结果
array_append(anyarray,anyelement)anyarray 在数组末尾追加元素
SELECT array_append(ARRAY[1,2], 3);
{1,2,3}
array_cat(anyarray,anyarray)anyarray 连接两个数组 SELECT array_cat(ARRAY[1,2,3], ARRAY[4,5]); {1,2,3,4,5}
array_ndims(anyarray)int 返回数组维数 SELECT array_ndims(ARRAY[[1,2,3], [4,5,6]]); 2
array_dims(anyarray)text 返回数组维数的文本表示 SELECT array_dims(ARRAY[[1,2,3], [4,5,6]]); [1:2][1:3]
array_fill(anyelement,int[], [,int[]])anyarray使用提供的值和维度初始化一个数组,其中anyelement是值,第一个int[]是数组的长度,第二个int[]是数组下界,下界默认是1 SELECT array_fill(7, ARRAY[3], ARRAY[2]); [2:4]={7,7,7}
array_length(anyarray,int)int 返回数组指定维度的长度 SELECT array_length(array[1,2,3], 1); 3
array_lower(anyarray,int)int 返回数组指定维度的下界 SELECT array_lower('[0:2]={1,2,3}'::int[], 1); 0
array_position(anyarray,anyelement[,int])int 返回数组元素anyelement从数组的[,int]位置(默认为1)开始第一次出现在数组中的位置,数组必须是一维的 SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon'); 2
array_positions(anyarray,anyelement)int[] 返回元素在数组中的所有位置 SELECT array_positions(ARRAY['A','A','B','A'], 'A'); {1,2,4}
array_prepend(anyelement,anyarray)anyarray 在数组开头添加新的元素 SELECT array_prepend(1, ARRAY[2,3]); {1,2,3}
array_remove(anyarray,anyelement)anyarray 从数组中删除所有的指定元素,必须是一维数组 SELECT array_remove(ARRAY[1,2,3,2], 2); {1,3}
array_replace(anyarray,anyelement,anyelement)anyarray 替换指定数组元素为新的元素 SELECT array_replace(ARRAY[1,2,5,4], 5, 3); {1,2,3,4}
array_to_string(anyarray,text[,text])text 将数组元素使用分隔符连接为文本,NULL可以使用指定元素替换 SELECT array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*'); 1,2,3,*,5
array_upper(anyarray,int)int 数组指定维度的上届 SELECT array_upper(ARRAY[1,8,3,7], 1); 4
cardinality(anyarray)int 返回数组所有维度的长度总和,如果是空数组则返回0 SELECT cardinality(ARRAY[[1,2],[3,4]]); 4
string_to_array(text,text[,text])text[] 将文本使用分隔符分隔后转换为数组,如果指定第三个参数,则第三个参数在数组中被转换为NULL SELECT string_to_array('xx~^~yy~^~zz', '~^~', 'yy'); {xx,NULL,zz}
unnest(anyarray)setof anyelement 将数组元素转换为行 SELECT unnest(ARRAY[1,2]);
1
2
unnest(anyarray,anyarray[, ...])setof anyelement, anyelement [, ...] 将多维数组转换为行集合,其中第一个数组显示为第一列,第二个数组显示为第二列,以此类推。但是这个函数只在from子句中使用 SELECT * from unnest(ARRAY[1,2],ARRAY['foo','bar','baz']);
unnest | unnest
--------+----
1 | foo
2 | bar
| baz
postgresqlreplace的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、postgresqlreplace的信息别忘了在本站进行查找喔。