如何使用复合类型更新 Postgresql table
How to update a Postgresql table using a composite type
我正在尝试使用复合键更新 table,但无法弄清楚
语法。我有一个 table 定义为:
create schema test;
create type test.ra_dec as (f1 double precision, f2 double precision);
create table test.pos_vel(
xp double precision,
yp double precision,
xv double precision,
yv double precision
);
insert into test.pos_vel(xp,yp,xv,yv) values
(1,2,5,10),
(2,3,10,20),
(3,4,20,40);
我可以验证我的定义类型是否有效:
gsh=# select cast( (100,200) as test.ra_dec);
row
-----------
(100,200)
(1 row)
但我不知道如何更新我的 table。
gsh=# update test.pos_vel set (xv,yv) = (select cast( (100,200) as test.ra_dec));
ERROR: syntax error at or near "select"
LINE 1: update test.pos_vel set (xv,yv) = (select cast( (100,200) as...
我知道对于这个简单的例子我可以不使用复合类型来完成,但是出于速度原因我想在 pl/python 中定义一个函数并让它 return 成为复合类型,所以我需要找出正确的语法。
有人可以帮忙吗?
-- drop schema if exists test cascade;
create schema test;
create type test.ra_dec as (f1 double precision, f2 double precision);
-- Your function here
create function test.f() returns test.ra_dec language sql as $$
select (100,200)::test.ra_dec $$;
create table test.pos_vel(
xp double precision,
yp double precision,
xv double precision,
yv double precision
);
insert into test.pos_vel(xp,yp,xv,yv) values
(1,2,5,10),
(2,3,10,20),
(3,4,20,40);
update test.pos_vel set (xv,yv) = (select * from test.f());
更新:
因为 sub-select 在 PostgreSQL 9.5 中引入了这种情况,所以它在早期版本中不起作用。有两种解决方法:
update test.pos_vel set (xv,yv) = ((test.f()).f1, (test.f()).f2);
update test.pos_vel set (xv,yv) = (t.f1, t.f2) from (select * from test.f()) t;
在 8.4 上测试:http://dbfiddle.uk/?rdbms=postgres_8.4&fiddle=0a4927ed804b76442a69f8259cca1929
我正在尝试使用复合键更新 table,但无法弄清楚 语法。我有一个 table 定义为:
create schema test;
create type test.ra_dec as (f1 double precision, f2 double precision);
create table test.pos_vel(
xp double precision,
yp double precision,
xv double precision,
yv double precision
);
insert into test.pos_vel(xp,yp,xv,yv) values
(1,2,5,10),
(2,3,10,20),
(3,4,20,40);
我可以验证我的定义类型是否有效:
gsh=# select cast( (100,200) as test.ra_dec);
row
-----------
(100,200)
(1 row)
但我不知道如何更新我的 table。
gsh=# update test.pos_vel set (xv,yv) = (select cast( (100,200) as test.ra_dec));
ERROR: syntax error at or near "select"
LINE 1: update test.pos_vel set (xv,yv) = (select cast( (100,200) as...
我知道对于这个简单的例子我可以不使用复合类型来完成,但是出于速度原因我想在 pl/python 中定义一个函数并让它 return 成为复合类型,所以我需要找出正确的语法。
有人可以帮忙吗?
-- drop schema if exists test cascade;
create schema test;
create type test.ra_dec as (f1 double precision, f2 double precision);
-- Your function here
create function test.f() returns test.ra_dec language sql as $$
select (100,200)::test.ra_dec $$;
create table test.pos_vel(
xp double precision,
yp double precision,
xv double precision,
yv double precision
);
insert into test.pos_vel(xp,yp,xv,yv) values
(1,2,5,10),
(2,3,10,20),
(3,4,20,40);
update test.pos_vel set (xv,yv) = (select * from test.f());
更新:
因为 sub-select 在 PostgreSQL 9.5 中引入了这种情况,所以它在早期版本中不起作用。有两种解决方法:
update test.pos_vel set (xv,yv) = ((test.f()).f1, (test.f()).f2);
update test.pos_vel set (xv,yv) = (t.f1, t.f2) from (select * from test.f()) t;
在 8.4 上测试:http://dbfiddle.uk/?rdbms=postgres_8.4&fiddle=0a4927ed804b76442a69f8259cca1929