Broadcast a column to all rows using Trigger Function on Update - postgresql

I have a table:
uid
history
updatedAt
a
someOlderJson
2022-12-31:23:59:59
b
someNewerJson
2023-01-01:00:00:30
And I would like to update the table to the below state when the history column is updated to someNewestJson:
uid
history
updatedAt
a
someNewestJson
2023-01-02:00:00:30
b
someNewestJson
2023-01-02:00:00:30
Is there a way to do this in postgresql?
P/S: I only have access to the DB and would like the update to be automatically perform anytime the column is updated.

Related

Supabase: How to automatically update a timestamp field after updating a row?

What DB I am using?
Supabase hosted version
What do I need?
After I update a row with .update({ name: 'Middle Earth' }) method I need to
automatically update also a timestamp in my table.
How can I update automatically a timestamp?
1)If you already have a table use this script (provided by Supabase devs themselves):
create extension if not exists moddatetime schema extensions;
-- assuming the table name is "todos", and a timestamp column "updated_at"
-- this trigger will set the "updated_at" column to the current timestamp for every update
create trigger handle_updated_at before update on todos
for each row execute procedure moddatetime (updated_at);
2)What if I don't wanna use the moddatetime extension?
This stackoverflow question will give you an answer.

Created date, last modified date fields in postgress

In PostgreSQL, is there a way to add columns that will automatically record the creation date and latest updated date of a row?
for table creation date look to event triggers
for insertion look into DEFAULT value for timestamptz column (works only if you don't explicitly define value)
for last modification, use trigger FOR EACH ROW before DELETE/UPDATE
The idea - Robust way of adding created and modified fields for data we add to database through db triggers
Update modified_by and modeified_on or modified_at for every db transaction.
Pick created_on and created_by or created_at from modified details whenever you insert a row into tables.
For trigger function, check this repo https://github.com/charan4ks/created_fields.git

Update all tables using trigger

My customer table is (customer_id, customerName). Almost all the tables have customerName column. I need to make a trigger that will update all the customerNames' in Database when the customerName in customer table is updated or changed.
A better approach is to normalise your database, so that other tables do not hold a customer name, rather they hold a reference to the customer_id. This way customer details only need to be updated in the customer table.

update the value of a column every time an update is made to the row

I am using SQL Server 2012. I need to track all the rows that were updates (as part of any action) made on a table.
I thought to add a new column (Column Name: "LastUpdated" of type Datetime.) to the table that I want to track. I would like to update the LastUpdated value every time an update is made to that row. Is there a specific way to acheive this task?
You could create a trigger. Like this:
CREATE TRIGGER dbo.Table1_Updated
ON dbo.Table1
FOR UPDATE /* Fire this trigger when a row is UPDATEd */
AS BEGIN
UPDATE dbo.Table1 SET dbo.Table1.LastUpdated = GETDATE()
FROM INSERTED
WHERE inserted.id=Table1.id
END
However, this trigger will not store WHAT was updated. Also keep in mind that if two people update it, you will only have the date of the most recent update (not all updates).
To keep a history on all changes, you might want to create an audit table (nearly identical structure to your existing table) and use a trigger to copy the pre-update data into the audit table.
This SO article talks about a solid approach: Using table auditing in order to have "snapshots" of table
You would want to look into creating a trigger to fire on update
Create TRIGGER Trigger_Name ON Table_Name
FOR update
AS
BEGIN
// update lastupdated column of updated record with current date and or time
END
Alliteratively you could just pass the new value for lastupdated when updating the other fields
We have implemented a system similar to the following:
Add a new column to your table LastUpdatedDate with a datatype of datetime
If needed, add another column with LastUpdatedBy with a datatype of varchar(100) or whatever length you need
This will store both the date/time and who performed the update.
Then when you update the row in the table, you will include those two columns in your update statement similar to this:
update yourtable
set yourCol = 'newValue',
LastUpdatedDate = getdate(),
LastUpdatedBy = 'yourUserIdentifier'

DB2 iSeries Trigger for added & modified timestamp and user fields

Perhaps triggers are not needed for added/modifed dates, maybe there are appropriate functions to set their values, in any case:
My question is with the following fields,
created (timestamp)
updated (timestamp)
createdBy (string, to hold the created by user name)
updatedBy (string, to hold the updated by user name)
how do I alter the table such that on creation and update these fields hold the appropriate values?
Edit: I now just need to know how to set the updatedBy and updated timestamp fields each time the record is accessed.
Create the following table for a reference:
create table test(
id integer generated always as identity,
content char(60),
createdBy char(30) default user,
created timestamp default current timestamp,
updatedBy char(30),
updated timestamp default null,
primary key(id)
)
This table has an auto incrementing primary key (id), a createdBy field which is set on insert, a created timestamp which is set on insert now we just need triggers to make the last two work as expected (there is a new feature to set updated on update without the use of triggers but the feature does not seem to allow a null value to show the record has never been updated so that does not work for me).
insert into test (content) VALUES ('first thing'),
('second thing')
To see that the default values for created and createdBy have been set:
select * from test
To add update triggers:
CREATE TRIGGER mytrigger
NO CASCADE BEFORE UPDATE ON test
REFERENCING NEW AS post
FOR EACH ROW MODE DB2ROW
SET
post.updated = CURRENT TIMESTAMP,
post.updatedBy = USER
To see if the above is working, lets update the values in "content":
update co05arh/test
set content = 'first thing updated'
where id = 1
To see the new default values
select * from co05arh/test
We should then see something like
ID CONTENT CREATEDBY CREATED UPDATEDBY UPDATED
1 first thing updated KEN 2011-04-29 16:16:17.942429 KEN 2011-04-29 16:16:28.649543
2 second thing KEN 2011-04-29 16:16:18.01629 <null> <null>