Answer the question
In order to leave comments, you need to log in
How to automatically change the total column when adding a new line?
I wrote a trigger, but something doesn't work:
CREATE OR REPLACE FUNCTION autocuontfunc() RETURNS TRIGGER AS $big_bank$
BEGIN
INSERT INTO big_bank(total) VALUES (NEW.total = percent * score);
RETURN NEW;
END;
$big_bank$ LANGUAGE plpgsql;
CREATE TRIGGER auto_cuont BEFORE DELETE ON big_bank FOR EACH ROW EXECUTE PROCEDURE autocuontfunc();
CREATE TABLE big_bank(
id SERIAL NOT NULL PRIMARY KEY,
fio TEXT NOT NULL,
percent numeric DEFAULT 0,
score NUMERIC DEFAULT 0,
total NUMERIC DEFAULT 0
);
Answer the question
In order to leave comments, you need to log in
when adding a new line
CREATE TABLE big_bank(
id SERIAL NOT NULL PRIMARY KEY,
fio TEXT NOT NULL,
percent numeric DEFAULT 0,
score NUMERIC DEFAULT 0,
total NUMERIC GENERATED ALWAYS AS (percent * score) STORED
);
CREATE OR REPLACE FUNCTION autocuontfunc() RETURNS TRIGGER AS $big_bank$
BEGIN
NEW.total = NEW.percent * NEW.score;
RETURN NEW;
END;
$big_bank$ LANGUAGE plpgsql;
CREATE TRIGGER auto_cuont BEFORE INSERT OR UPDATE ON big_bank FOR EACH ROW EXECUTE PROCEDURE autocuontfunc();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question