Trigger for generating IDs - sql

I would like to create trigger for generating ID in table:
CREATE TABLE client (
clientID INT PRIMARY KEY NOT NULL,
name VARCHAR(16) NOT NULL,
surname VARCHAR(16) NOT NULL,
personalID VARCHAR(10) NOT NULL,
CONSTRAINT verifyPersonalID CHECK ((personalID BETWEEN 1000000000 and 9999999999) and (MOD(personalID, 11) = 0))
);
I tried to write it like this but it keeps returning errors and I dont know why. Can you, please, give me an advice what Im doing wrong?
CREATE OR REPLACE TRIGGER clientID
AFTER INSERT
ON client
FOR EACH ROW
BEGIN
UPDATE client
SET client.clientID = klientSeq.nextval
WHERE :new.personalID = client.personalID;
END;
/

You want a before insert trigger:
CREATE OR REPLACE TRIGGER klientID
BEFORE INSERT
ON klient
FOR EACH ROW
BEGIN
SELECT klientSeq.nextval INTO :new.cisloKlienta FROM dual;
END;

Maybe instead of creating a trigger, you could make the default value of the primary key the next value of the sequence.

Related

PostgreSQL Trigger takes 5 seconds

i have a MediaStore Database on Postgres where tried to make a trigger which Updates the average Rating of a Product if a new review is inserted.
The Problem is: If I insert a review now, it takes more than 5 seconds.
Im not really into Databases so i thought of asking you people here :)
The DDL of the two relevant tables are:
create table review
(
review_id bigint generated by default as identity primary key,
rating integer not null CHECK (rating BETWEEN 1 AND 5),
helpful integer not null CHECK (helpful >= 0),
reviewDate date,
benutzer varchar(255),
summary varchar(255),
comment text,
produkt_id bigint NOT NULL references produkt ON DELETE CASCADE
);
create table produkt
(
produkt_id bigint generated by default as identity primary key,
asin varchar(255) unique NOT NULL,
titel varchar(1000) NOT NULL,
rating double precision,
bild varchar(1000),
verkaufsrang integer
);
And the Trigger:
CREATE OR REPLACE FUNCTION update_rating()
RETURNS TRIGGER AS $$
BEGIN
UPDATE produkt
SET rating =
(SELECT AVG(rating) AS rating
FROM review
GROUP BY produkt_id
Having review.produkt_id = new.produkt_id)
WHERE produkt_id = new.produkt_id;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER update_rating
AFTER INSERT ON review
FOR EACH ROW
EXECUTE PROCEDURE update_rating();
Does somebody have a solution which reduces the Time of the Insert?
You don't describe your indexes. An index on review (produkt_id, rating) could help a lot if you don't have one already. If you had columns in produkt for sum and count, then you could just compute the new average without needing to traverse the entire set in review for that produkt_id. You might have a problem with concurrency, but that could be a problem with your current one too.

How to create date trigger?

How can I create a trigger to increase a date data from my table with each next row? I had an attempt, is below the table
What I want to do is to increase date from training_date_end by 1 week. But I don't know how to do it, just studying. Can anyone help?
CREATE TABLE training
(
coach_id int NOT NULL,
customer_id int NOT NULL,
training_date_start date NULL,
training_date_end date NULL,
training_place_id int NOT NULL,
CONSTRAINT training_pk PRIMARY KEY (training_place_id)
);
create or replace trigger lucky_week
before insert or update on training
for each row
begin
update training
set training_date_end = :new.training_date_end + 7
where training_date_end = :new.training_date_end;
end;
Most probably like this:
create or replace trigger lucky_week
before insert or update on training
for each row
begin
:new.training_date_end := :new.training_date_end + 7;
end;
Because, your trigger will suffer from the mutating table error (if you insert more than a single row), and - won't do anything in that case (because row you'd like to update doesn't exist yet).

How to fix the trigger with check compiler log error?

Please, help!
I have trigger:
CREATE TRIGGER check_reservation BEFORE INSERT ON order
FOR EACH ROW
DECLARE mistake INTEGER;
BEGIN
SELECT count(*) INTO mistake FROM order join reserving
on id_order = reserving.order_id_order
WHERE reserving.room_num_room=:new.room_num_room
AND (order.reservation_from < :new.reservation_from AND :new.reservation_from < order.reservation_to) OR
(order.reservation_from < :new.reservation_from AND :new.reservation_to < order.reservation_to) OR
(:new.reservation_from <= order.reservation_from AND order.reservation_to <= :new.reservation_to);
IF mistake>0 THEN
raise_application_error(-20001,'reservation already exists');
END IF;
END;
The idea of the trigger is not to allow make a reservation on already booked room. When I run it I had check compiler log error message. How can I change trigger?
I have following tables:
CREATE TABLE order (
id_order CHAR(100) NOT NULL,
reservation_from DATE NOT NULL,
reservation_to DATE NOT NULL,
);
ALTER TABLE order ADD CONSTRAINT order_pk PRIMARY KEY ( id_order );
CREATE TABLE room (
num_room CHAR(100) NOT NULL,
type VARCHAR2(100) NOT NULL,
);
ALTER TABLE room ADD CONSTRAINT room_pk PRIMARY KEY ( num_room );
CREATE TABLE reserving (
room_num_room CHAR(100) NOT NULL,
order_id_order CHAR(100) NOT NULL
);
ALTER TABLE reserving ADD CONSTRAINT reserving_pk PRIMARY KEY ( room_num_room,
order_id_order );
ALTER TABLE reserving
ADD CONSTRAINT reserving_order_fk FOREIGN KEY ( order_id_order )
REFERENCES order ( id_order );
ALTER TABLE reserving
ADD CONSTRAINT reserving_room_fk FOREIGN KEY ( room_num_room )
REFERENCES room ( num_room );
I tried recreating the trigger with the statements above. The statements failed with several errors, it looks as if they were not tested before posting them as question. Please take some time posting a quality question.
Example:
CREATE TABLE room (
num_room CHAR(100) NOT NULL,
type VARCHAR2(100) NOT NULL, << this trailing comma makes this statement fail.
);
After fixing all errors I ran the "CREATE TRIGGER" and it errored out with
PLS-00049: bad bind variable 'NEW.ROOM_NUM_ROOM'
That is because the column ROOM_NUM_ROOM does not exist in the "ORDER" table.
If I remove the reference to 'NEW.ROOM_NUM_ROOM' the trigger compiles successfully.
However, as gsalem pointed out, this will not work because it will raise a mutating table error. In the trigger code you cannot execute DML referencing the table that the trigger is on. There is plenty of documentation on how to avoid mutating table errors.

How to use trigger in Postgres after Update?

Hi guys i need your help :D
I'm using the latest version of PostgreSQL
First of all, here is my database's tables:
CREATE TABLE colore (
idcolore INTEGER PRIMARY KEY,
nome VARCHAR(100),
note TEXT
);
CREATE TABLE Prodotto (
SKU varchar(50) PRIMARY KEY,
nome varchar(255) NOT NULL,
quantita INTEGER DEFAULT -1,
idColore INTEGER,
prezzo NUMERIC(10, 2),
FOREIGN KEY(idColore) REFERENCES Colore(idColore) ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE TABLE Ordine (
idOrdine INTEGER PRIMARY KEY,
SKU varchar(50) NOT NULL,
quantita INTEGER NOT NULL,
CHECK (check_quantita(SKU, quantita)),
FOREIGN KEY(SKU) REFERENCES Prodotto(SKU) ON UPDATE NO ACTION ON DELETE NO ACTION
);
What I want is that when I insert a new Ordine, the quantita of the Prodotto references by SKU is the quantity available minus the quantity ordered.
For Example:
I have this Prodotto:
SKU : AAA
Nome: Prodotto1
Quantita: 11
And then I do the following:
INSERT INTO Ordine (idOrdine, SKU, quantita) VALUES (1, 'AAA', 10);
What I want is that after the last insert the quantity of the product AAA would be 1.
I've tried using this piece of code
CREATE OR REPLACE FUNCTION aggiorna_quantita() RETURNS trigger AS
$$
BEGIN
UPDATE Prodotto
SET quantita = (SELECT Quantita FROM Prodotto WHERE SKU = TG_ARGV[0]) - TV_ARGV[$1]
WHERE SKU = TV_ARGV[$0] ;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_aggiorna_quantita
AFTER INSERT ON Ordine
FOR EACH STATEMENT
EXECUTE PROCEDURE aggiorna_quantita(SKU, quantita);
But nothing happens :(
Thank you in advance and forgive me for my bad English :D
The arguments to a trigger can only be string literals. Simple names and numeric values are converted to strings at compile time. What you want cannot be done using these arguments. Luckily there is a much simpler method. Inside the trigger a variable called NEW is available which is the row that just got inserted.
Also you do not have to use a select to retrieve the current value of quantita.
Oh and don't use uppercase characters for object names in postgresql. It's handling of uppercase is very confusing because it converts them to lowercase unless you put the names between double quotes.
And you also want your trigger to be row level instead of statement level.
So your code would become:
CREATE OR REPLACE FUNCTION aggiorna_quantita() RETURNS trigger AS
$$
BEGIN
UPDATE prodotto
SET quantita = prodotto.quantita - NEW.quantita
WHERE sku = NEW.sku;
RETURN NEW;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_aggiorna_quantita
AFTER INSERT ON ordine
FOR EACH ROW
EXECUTE PROCEDURE aggiorna

Oracle Script problem - create trigger not terminating

I am trying to make some changes to an oracle database and have a script put together to do so. The problem is when it gets to a point in the script where I am creating a trigger it seems like the Create Trigger block does not properly terminate, when I look at the trigger afterwards it contains all of the remaining code in the script.
This is what I have:
CREATE OR REPLACE TRIGGER user_publish_log_trg
BEFORE INSERT ON USER_PUBLISH_LOG
FOR EACH ROW
BEGIN
SELECT user_publish_log_seq.NEXTVAL INTO :NEW.Id FROM dual;
END user_publish_log_trg;
CREATE TABLE USER_APPROVAL_LOG
(
Id number(10) NOT NULL ,
CommodityId number(10) NOT NULL,
QuarterEndDate DATE NOT NULL,
ActionId int NOT NULL ,
...
What am I doing wrong in ending the trigger?
You need to terminate the PL/SQL by using a slash on a new line, like this:
CREATE OR REPLACE TRIGGER user_publish_log_trg
BEFORE INSERT ON USER_PUBLISH_LOG
FOR EACH ROW
BEGIN
SELECT user_publish_log_seq.NEXTVAL INTO :NEW.Id FROM dual;
END user_publish_log_trg;
/
CREATE TABLE USER_APPROVAL_LOG
(
Id number(10) NOT NULL ,
CommodityId number(10) NOT NULL,
QuarterEndDate DATE NOT NULL,
ActionId int NOT NULL ,
...