database - 带有序列的 Postgres URI 生成?

标签 database postgresql uri psql

所以我想在 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/

相关文章:

mysql - 生成此报告的最有效方法是什么?

java - 不使用 native SQL 查询的正则表达式查询 - JPA

html - 有没有办法判断最终用户是否能够处理 <a href ="tel:###"> 链接?

database - Redis级联复制

sql - 为什么要使用 SQL 数据库?

android - Stetho,没看到检查链接

c# - 从 Uri 获取单个查询参数

php - 多语言网站、技巧和解决方案

python - SQLAlchemy 多列相关更新

rest - 语言环境属于路径还是作为 URI 的请求参数?