为表中的每一行分别递增触发器

时间:2018-05-29 17:56:13

标签: sql oracle plsql triggers

我在为表中的每一行创建递增触发器时遇到问题。我需要的是根据其中一列来计算行数。例如:

     table
column1|column2
    1  |  1
    1  |  2
    2  |  1
    1  |  3
    3  |  1
    2  |  2

我创建了序列:

CREATE SEQUENCE inc_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1;

并触发:

CREATE OR REPLACE TRIGGER inc_on_insert
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
SELECT inc_seq.nextval
INTO :new.column2
FROM dual;
END;

我现在得到的是增量值,但每行继续递增,并且重置。我不知道如何在column1的每个不同值上创建从1开始的语句。

编辑:

CREATE TABLE moves (
    move_id        NUMBER,
    game_id NOT NULL
        REFERENCES games ( game_id )
            ON DELETE CASCADE,
    move_number    NUMBER NOT NULL,
    stages_count   NUMBER DEFAULT 1,
    CONSTRAINT move_pk PRIMARY KEY ( move_id ),
    CONSTRAINT moves_const_1 UNIQUE ( game_id,
                                      move_number,
                                      stages_count )
);

3 个答案:

答案 0 :(得分:4)

触发器不适用于此类目的。我宁愿使用readLines创建一个视图。您仍然可以使用触发器/序列为表生成row_number()列。

id

Demo

CREATE
    OR replace VIEW t_view AS
SELECT column1
    ,row_number() OVER (
        PARTITION BY column1 ORDER BY id --id generated using your trigger
        ) AS column2
FROM t
ORDER BY id;

答案 1 :(得分:1)

我第二次看到Kaushik的意见,并且不会为此目的使用触发器。例如,如果一行被删除会发生什么?

但是,如果你坚持,我会这样编码:

CREATE TABLE mytable (column1 NUMBER, column2 NUMBER);

CREATE OR REPLACE TRIGGER inc_on_insert
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
  SELECT COUNT(*)+1 INTO :NEW.column2 
    FROM mytable 
   WHERE column1=:NEW.column1;
END;
/

INSERT INTO mytable(column1) VALUES (5);
SELECT * FROM mytable;
5 1

INSERT INTO mytable(column1) VALUES (5);
SELECT * FROM mytable;
5 1
5 2

INSERT INTO mytable(column1) VALUES (6);
SELECT * FROM mytable;
5 1
5 2
6 1

INSERT INTO MYTABLE(COLUMN1) VALUES (5);
SELECT * FROM mytable;
5 1
5 2
6 1
5 3

答案 2 :(得分:0)

请尝试类似:

CREATE OR REPLACE TRIGGER inc_on_insert
BEFORE INSERT ON table
FOR EACH ROW

BEGIN
select count(1) into thereiscolumn1 from table where column1 = :new.column1 and rownum=1;
if thereiscolumn1 > 0 THEN
    SELECT (MAX(column2) + 1 ) INTO :new.column2 from table WHERE column1 = :new.column1
ELSE
    SELECT 1 INTO :new.column2 FROM dual;
END IF;

END;