所以我想在 postgres 数据库的表中创建一个列,该列自动递增一个数字并将其放在一些数据之后。又名
协议(protocol)表:
name | uri |
_____________________________
someProtocol | /protocol/1
otherProt | /protocol/2
有没有办法用其他数据创建某种序列?我对 postgres 列创建的了解相当有限
最佳答案
create table protocols (
id serial primary key, -- creates a sequence int
name text not null,
uri text
);
create or replace function protocols_set_uri() returns trigger as $$
begin
new.uri = '/' || new.name || '/' || new.id;
return new;
end $$
language plpgsql;
create trigger protocols_set_uri
before insert or update on protocols
for each row execute procedure protocols_set_uri();
例子:
insert into protocols (name) values ('someProtocol');
select * from protocols;
结果:
id name uri
--------------------------------
1 someProtocol /someProtocol/1
关于database - 带有序列的 Postgres URI 生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483779/