Postgresql 非主键上的POSTGRES序列触发器
我有一个PostgreSQL数据库版本9.5.0 创建的表Postgresql 非主键上的POSTGRES序列触发器,postgresql,triggers,count,sequence,Postgresql,Triggers,Count,Sequence,我有一个PostgreSQL数据库版本9.5.0 创建的表UserComments如下所示: create table UserComments ( userId integer, commentNr integer, comment text, UNIQUE (userId, commentNr) ); 该表不能更改,我也不能添加额外的表 我想根据userId条目的数量在commentNr上创建一个序列(换句话说:一个自动递增的值)。 对于具有相同用户ID的每个新
UserComments
如下所示:
create table UserComments (
userId integer,
commentNr integer,
comment text,
UNIQUE (userId, commentNr)
);
该表不能更改,我也不能添加额外的表
我想根据userId
条目的数量在commentNr
上创建一个序列(换句话说:一个自动递增的值)。
对于具有相同用户ID的每个新条目,其值应递增。例如: userId | commentNr | comment -------|-----------|--------- 1 | 1 | blabla 1 | 2 | more bla 2 | 1 | myownbla 1 | 3 | evenmorebla
任何帮助都将不胜感激 对于这样的简单情况,不需要创建触发器。可以在CTE或子查询中计算并插入正确的序列 注意:如果使用下面的sql,则必须指定两次插入的
userId
INSERT INTO "user" ("userId", "commentNr", "comment") (
WITH
nextval AS (
SELECT COALESCE(MAX("commentNr"),0)+1 commentNr
FROM "user"
WHERE "userId" = '1'
)
SELECT '1', nextval.commentNr, 'bla'
FROM nextval
);
要自动计算commentNr字段,可以使用触发器:
CREATE INDEX i1 ON UserComments (userId, commentNr DESC);
CREATE OR REPLACE FUNCTION set_commentNr() RETURNS TRIGGER AS $sql$
BEGIN
NEW.commentNr = coalesce(max(commentNr) + 1, 1) FROM UserComments WHERE UserId = NEW.userID;
RETURN NEW;
END;
$sql$ LANGUAGE plpgsql STABLE;
CREATE TRIGGER ins_comment BEFORE INSERT ON UserComments FOR EACH ROW EXECUTE PROCEDURE set_commentNr();
小型测试:
insert into userComments (userId, comment) values ('1', 'bla');
insert into userComments (userId, comment) values ('1', 'bla');
insert into userComments (userId, comment) values ('2', 'bla');
insert into userComments (userId, comment) values ('1', 'bla');
insert into userComments (userId, comment) values ('2', 'bla');
SELECT * FROM userComments;
userid | commentnr | comment
--------+-----------+---------
1 | 1 | bla
1 | 2 | bla
2 | 1 | bla
1 | 3 | bla
2 | 2 | bla
(5 rows)
为提高计算下一个值的性能而创建的索引i1:
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Result (cost=4.17..4.18 rows=1 width=4) (actual time=0.032..0.032 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.15..4.17 rows=1 width=4) (actual time=0.026..0.026 rows=1 loops=1)
-> Index Only Scan using i1 on usercomments (cost=0.15..24.27 rows=6 width=4) (actual time=0.021..0.021 rows=1 loops=1)
Index Cond: ((userid = 1) AND (commentnr IS NOT NULL))
Heap Fetches: 1
Planning time: 0.238 ms
Execution time: 0.095 ms
主键(id),
::表中没有列id
。请发布真实代码。你是对的-本例不需要主键。
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Result (cost=4.17..4.18 rows=1 width=4) (actual time=0.032..0.032 rows=1 loops=1)
InitPlan 1 (returns $0)
-> Limit (cost=0.15..4.17 rows=1 width=4) (actual time=0.026..0.026 rows=1 loops=1)
-> Index Only Scan using i1 on usercomments (cost=0.15..24.27 rows=6 width=4) (actual time=0.021..0.021 rows=1 loops=1)
Index Cond: ((userid = 1) AND (commentnr IS NOT NULL))
Heap Fetches: 1
Planning time: 0.238 ms
Execution time: 0.095 ms