增加PostgreSQL中的多列序列

Mar*_*rio 8 postgresql sequences multiple-columns

是否有任何内置方式(我的意思是,不需要触发器和/或函数)来为每个多列增加索引?

所以表演后:

INSERT INTO "table"
    ("month", "desc")
    VALUES
    (1, 'One thing')
,   (1, 'Another thing')
,   (1, 'Last task of the month')
,   (2, 'Last task of the month')
,   (2, 'Last task of the month')
,   (3, 'First of third month')
Run Code Online (Sandbox Code Playgroud)

我的表最终会像这样(注意"任务"栏):

month    task    desc
1        1       One thing
1        2       Another thing
1        3       Last task of the month
2        1       First of second month
2        2       Second and last of second month
3        1       First of third month
Run Code Online (Sandbox Code Playgroud)

Igo*_*nko 12

您可以将simlpe SERIAL列添加到您的表中(它将为您提供事物的顺序),然​​后使用以下内容:

SELECT *, row_number() OVER (PARTITION BY month ORDER BY serial_column)
FROM table
Run Code Online (Sandbox Code Playgroud)

这将为您提供所需的结果.

如果您不需要订购行,可以尝试:

SELECT *, row_number() OVER (PARTITION BY month)
FROM table
Run Code Online (Sandbox Code Playgroud)

详细信息: row_number() OVER(...)

UPD工作原理:

具有类型的列SERIAL本质上是"自动增量"字段.它会自动从序列中获取值.向表中插入行时,它们将如下所示:

| MONTH | SERIAL_COLUMN |                     DESCRIPTION |
-----------------------------------------------------------
|     1 |             1 |                       One thing |
|     1 |             2 |                   Another thing |
|     1 |             3 |          Last task of the month |
|     2 |             4 |           First of second month |
|     2 |             5 | Second and last of second month |
|     3 |             6 |            First of third month |
Run Code Online (Sandbox Code Playgroud)

关键是 - 每个下一个添加的行的值都SERIAL_COLUMN 大于以前的所有行.

下一个.这样row_number() OVER (PARTITION BY month ORDER BY serial_column)做:

1)使用equal month(PARTITION BY month)将所有行分区为组

2)按serial_column(ORDER BY serial_column)的值对它们进行排序

3)在每个组中使用步骤2(`row_number() OVER)中的顺序分配行号

输出是:

| MONTH | SERIAL_COLUMN |                     DESCRIPTION | ROW_NUMBER |
------------------------------------------------------------------------
|     1 |             1 |                       One thing |          1 |
|     1 |             2 |                   Another thing |          2 |
|     1 |             3 |          Last task of the month |          3 |
|     2 |             4 |           First of second month |          1 |
|     2 |             5 | Second and last of second month |          2 |
|     3 |             6 |            First of third month |          1 |
Run Code Online (Sandbox Code Playgroud)

要更改输出,row_number()需要更改值SERIAL_COLUMN.例如,Second and last of second monthFirst of second month一个意志之前放置会改变那样的值SERIAL_COLUMN:

UPDATE Table1
SET serial_column = 5
WHERE description = 'First of second month';

UPDATE Table1
SET serial_column = 4
WHERE description = 'Second and last of second month';
Run Code Online (Sandbox Code Playgroud)

它将更改查询的输出:

| MONTH | SERIAL_COLUMN |                     DESCRIPTION | ROW_NUMBER |
------------------------------------------------------------------------
|     1 |             1 |                       One thing |          1 |
|     1 |             2 |                   Another thing |          2 |
|     1 |             3 |          Last task of the month |          3 |
|     2 |             4 | Second and last of second month |          1 |
|     2 |             5 |           First of second month |          2 |
|     3 |             6 |            First of third month |          1 |
Run Code Online (Sandbox Code Playgroud)

确切的值SERIAL_COLUMN无所谓.他们只在一个月内就任务设定了订单.

我的SQLFiddle示例就在这里.


Jos*_*rry 5

如果您愿意将INSERT每个插入语句分成一行数据,则可以使用PostgreSQL规则。下面的示例有点令人费解,因为规则似乎不允许您将写入重定向到关系本身。通常使用触发器来完成。但是,我们正在查看是否可以在没有触发器的情况下做到这一点,所以这里是:

--drop table table_data cascade;
CREATE TABLE table_data (
  month integer not null,
  task integer not null,
  "desc" text
);
ALTER TABLE table_data add primary key (month, task);

CREATE VIEW "table" as 
 select month, task, "desc" from table_data;

CREATE OR REPLACE RULE calculate_task AS ON INSERT TO "table"
  DO INSTEAD
  INSERT into table_data (month, task, "desc") 
  VALUES (
  NEW.month, 
  (select coalesce(max(task),0) + 1 from table_data where month = NEW.month), 
  NEW."desc");

BEGIN;
INSERT INTO "table" ("month", "desc") VALUES (1, 'One thing');
INSERT INTO "table" ("month", "desc") VALUES (1, 'Another thing');
INSERT INTO "table" ("month", "desc") VALUES (1, 'Last task of the month');
INSERT INTO "table" ("month", "desc") VALUES (2, 'Last task of the month');
INSERT INTO "table" ("month", "desc") VALUES (2, 'Last task of the month');
INSERT INTO "table" ("month", "desc") VALUES (3, 'First of third month');
COMMIT;

select * from "table";
Run Code Online (Sandbox Code Playgroud)

笔记

  • 如果需要在“表”上支持DELETE / UPDATE,则也可以为每个操作添加规则。
  • 上面的BEGINand COMMIT块用于表明,即使在同一事务中,只要每一行都分成自己的,则此方法将起作用INSERT
  • 您使用了一些保留字,例如tabledesc。确保在完成操作后将双引号括起来,这样就不会有任何问题。

是上面的sqlfiddle代码