Postgresql Current timestamp on Update - mysql

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

How can I update my table field create_date to current date using trigger in sql?

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.

stored procedure returning `ASCII \0` error but can't find what that is referring to

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.

How to convert Oracle triggers to mysql

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

Error-when using ON in psql statement [duplicate]

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();

MySQL Trigger question: only trigger when a column is changed?

I don't know if this is possible, but I have a column named active in a table. Whenever the active column gets changed, I would like to reset the date in the date column, but ONLY if the active column gets changed.
If other columns are changed but not the active column, then the date would remain the same.
something like
DELIMITER //
CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.active <> OLD.active THEN
SET NEW.date = '';
END IF;
END
//
Ran into an issue with the IF test in the #2 example. When one of the values is null the <> test returns null. This leads to the test not getting met and the action of the trigger will not get run even though the one value does not equal null at all. To fix this I came up with this test that uses <=> (NULL-safe equal). Hope this helps someone out.
DELIMITER $$
DROP TRIGGER IF EXISTS updtrigger ;
$$
CREATE TRIGGER updtrigger AFTER UPDATE
ON yourTable FOR EACH ROW
BEGIN
IF ((NEW.active <=> OLD.active) = 0) THEN
SET NEW.date = '';
END IF;
$$