What is the postgres equivalent of the below mysql code
CREATE TABLE t1 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?
--update
Almost ready
CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
BEGIN
NEW.modified := current_timestamp;
RETURN NEW;
END;
$update_timestamp$ LANGUAGE plpgsql;
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
Just make sure all tables have the same columnname:
CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE TRIGGER t_name
BEFORE UPDATE
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE upd_timestamp();
Thank you for the information Mithun and Alex Brasetvik.
I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (NEW != OLD) THEN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END IF;
RETURN OLD;
END;
$$;
This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.
Update it with a trigger. Documentation and examples.
To further improve, the answer given by #Lauri Silvennoinen:
This trigger uses the WHEN clause as recommended by the official docs to check for changes in the row even before calling the specified function.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER tg_update_modified
AFTER UPDATE ON table_name
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE FUNCTION update_modified_timestamp();
Related
Error Code: 1442. Can't update table 'participant' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
So I want to achieve this -> once the participant is updated I want to update his create_date to current date
drop trigger update_user;
DELIMITER //
CREATE
TRIGGER update_user BEFORE UPDATE
ON Competition.participant
FOR EACH ROW BEGIN
update participant
set create_date = CURRENT_TIMESTAMP
where id_participant = old.id_participant;
END//
DELIMITER ;
update participant
set name = 'Rostyk'
where id_participant = 1;
CREATE
TRIGGER update_user
BEFORE UPDATE
ON Competition.participant
FOR EACH ROW
SET NEW.create_date = CURRENT_TIMESTAMP;
But it is more simple to define create_date column as ON UPDATE CURRENT_TIMESTAMP, and do not use triggers.
I am writing my first stored procedure as a trigger. I am doing this in a dev migration as we have two systems which don't speak to each other in dev, so I need to mock the data which would normally come from the other system.
My procedure is added as part of our dev migration script.
DELIMITER |;
CREATE TRIGGER `activity_insert` AFTER INSERT ON `activity`
FOR EACH ROW
BEGIN
UPDATE `activity` AS `a` JOIN `handle` AS `h` on `a.handle_id` = `h.handle_id` SET `path` = CONCAT(`h.handle`,'/',`a.activity_handle`) WHERE `a.path` IS NULL;
END;
|
DELIMITER;
I would expect the logic to be:
DELIMITER $$
CREATE TRIGGER activity_insert BEFORE INSERT ON activity
FOR EACH ROW
BEGIN
IF new.path IS NULL THEN
SET new.path = (SELECT CONCAT(h.handle, '/', new.activity_handle)
FROM handle h
WHERE new.handle_id = h.handle_id
);
END IF;
END;$$
DELIMITER;
There are numerous problem with your code:
You don't update the table being modified using update.
You want a "before" triggers, not an "after trigger".
Don't use | for the the delimited. It is a valid MySQL operator.
You have over-used the backtick, including putting the table alias in with the column alias.
This assumes that handle.handle_id is unique. This seems like a reasonable assumption based on the names, but you can add limit 1 to guarantee no more than one row is returned.
This is oracle trigger :
CREATE OR REPLACE TRIGGER "SW_TARIFF_TARIFF_TRIGGER"
BEFORE INSERT OR UPDATE OF "LAST_UPDATED_DT" ON "DEMO_DB"."SOLID_WASTE_TARIFF_MASTER"
REFERENCING OLD AS "OLD" NEW AS "NEW"
FOR EACH ROW
BEGIN
:NEW.LAST_UPDATED_DT := SYSDATE;
END;
I want to move it to mysql. But i don't know know i write cretae and replace with one trigger and also have problem with mysql replacement for of keyword of oracle . Can any one suggest me how to convert this oracle trigger to mysql .
In MySql you need to create two separate triggers, a first trigger BEFORE INSERT and a second trigger BEFORE UPDATE.
MySql doesn't support this clause UPDATE OF column_name - that is the trigger is fired only when a change of the specified column occured. In MySql the trigger is always fired, and You need to detect this condition yourself in a body of the trugger.Try:
CREATE TRIGGER SW_TARIFF_TARIFF_TRIGGER1
BEFORE INSERT ON SOLID_WASTE_TARIFF_MASTER
FOR EACH ROW
SET NEW.LAST_UPDATED_DT := Now();
and this:
CREATE TRIGGER SW_TARIFF_TARIFF_TRIGGER2
BEFORE UPDATE ON SOLID_WASTE_TARIFF_MASTER
FOR EACH ROW
BEGIN
IF NEW.LAST_UPDATED_DT <> OLD.LAST_UPDATED_DT
AND NEW.LAST_UPDATED_DT IS NOT NULL
AND OLD.LAST_UPDATED_DT IS NOT NULL
OR NEW.LAST_UPDATED_DT IS NULL AND OLD.LAST_UPDATED_DT IS NOT NULL
OR NEW.LAST_UPDATED_DT IS NOT NULL AND OLD.LAST_UPDATED_DT IS NULL
THEN
SET NEW.LAST_UPDATED_DT := Now();
END IF;
END;
This horrible IF condition detects if the column LAST_UPDATED has been changed (it simulates UPDATE OF "LAST_UPDATED_DT" clause from Oracle)
Please refer to the documentation for more information about triggers in MySql:
http://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
So I am trying to create trigger that will only execute when a specific column within a table is updated. The table in question is track the column in question is track.track_hits. What I want to do is if that value changes then it inserts a value into a table called play_log. The insert should be the track_id from track and a timestamp.
The code I have done so far is below but it doesn't work, how can it be fixed?
CREATE TRIGGER pi_play AFTER UPDATE ON track
FOR EACH ROW
BEGIN
if :new.track_hits != :old.track_hits
then
INSERT INTO play_log (track_id,access_time)
VALUES (NEW.track_id, NOW())
end if;
END;
NEW and OLD do not need : in front. Also, when creating triggers, you may need to change the delimiter. If you don't change the delimiter, your create trigger statement may be terminated early.
Here's how I would do it:
delimiter |
create trigger pi_play after update on track
for each row
begin
if new.track_hits != old.track_hits then
insert into play_log(track_id, access_time) values (new.track_id, now());
end if;
end|
delimiter ;
Here's the fiddle showing it in action: http://sqlfiddle.com/#!2/3d99d/1 Notice that I changed the delimiter in the schema window to | instead of the default ;. Since SQL uses JDBC and JDBC doesn't take the command DELIMITER, I chose the delimiter.
What is the postgres equivalent of the below mysql code
CREATE TABLE t1 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE t2 (
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
As per Alex Brasetvik answer below, it seems i should go with triggers, my problem is i have a number of tables t1, t2... with created and modified fields, is it possible to write a generalized procedure?
--update
Almost ready
CREATE FUNCTION update_timestamp() RETURNS trigger AS $update_timestamp$
BEGIN
NEW.modified := current_timestamp;
RETURN NEW;
END;
$update_timestamp$ LANGUAGE plpgsql;
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t1
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
CREATE TRIGGER update_timestamp BEFORE INSERT OR UPDATE ON t2
FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
Just make sure all tables have the same columnname:
CREATE OR REPLACE FUNCTION upd_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE TRIGGER t_name
BEFORE UPDATE
ON tablename
FOR EACH ROW
EXECUTE PROCEDURE upd_timestamp();
Thank you for the information Mithun and Alex Brasetvik.
I'd like to add one minor tweak to the trigger. Since we mostly likely want the modified column to store the timestamp when the row was last changed, not when it was the target of an UPDATE statement, we have to compare the new and the old value of the row. We update the modified column only if these two values differ.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
IF (NEW != OLD) THEN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END IF;
RETURN OLD;
END;
$$;
This trigger ensures that the modified column is updated only if the UPDATE operation actually changes the values stored in the row.
Update it with a trigger. Documentation and examples.
To further improve, the answer given by #Lauri Silvennoinen:
This trigger uses the WHEN clause as recommended by the official docs to check for changes in the row even before calling the specified function.
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
NEW.modified = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
CREATE OR REPLACE TRIGGER tg_update_modified
AFTER UPDATE ON table_name
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE FUNCTION update_modified_timestamp();