Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 March PostgreSQL: Update the Timestamp column with the use of TRIGGER

PostgreSQL: Update the Timestamp column with the use of TRIGGER

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Like MySQL, PostgreSQL doesn’t have different column for Datetime and Timestamp.
You can visit this MySQL article here:

In this post, I am sharing small demonstration on how to update a timestamp column automatically when a row is updated in the table of the PostgreSQL.

In the big transactional system like the banking system, we always require last updated datetime of every transaction and row.

We have also common practice to create last_modified_datetime column in table.
We can also update this column using application, and we can also update this column using the user-defined function.

But we can also create Trigger to perform this operation, and this automatic approach is good.

Below is a small demonstration by creating update Trigger:

Create a sample table:

1
2
3
4
5
6
7
8
CREATE TABLE tbl_EmployeeDetails
(
EmpID SERIAL
,EmpName CHARACTER VARYING(50)
,EmpDOB TIMESTAMP WITHOUT TIME ZONE
,LastUpdatedDateTime TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()
,CONSTRAINT pk_tbl_EmployeeDetails_EmpID PRIMARY KEY(EmpID)
);

Insert a sample data for EmpName and EmpDOB column:

1
2
3
4
5
6
7
INSERT INTO tbl_EmployeeDetails
(EmpName,EmpDOB)
VALUES
('Anvesh','1988-01-26')
,('Neevan','2000-06-29')
,('Martin','1985-08-04')
,('Jeny','1990-09-12');

Create a Trigger function:

1
2
3
4
5
6
7
CREATE OR REPLACE FUNCTION trg_fn_tbl_EmployeeDetails_LastUpdatedDateTime()
RETURNS TRIGGER AS $$
BEGIN
NEW.LastUpdatedDateTime = NOW();
RETURN NEW;
END;
$$ language 'plpgsql';

Create an UPDATE TRIGGER:

1
2
3
CREATE TRIGGER trg_update_tbl_EmployeeDetails BEFORE UPDATE
ON tbl_EmployeeDetails FOR EACH ROW EXECUTE PROCEDURE
trg_fn_tbl_EmployeeDetails_LastUpdatedDateTime();

Update a record and check LastUpdatedDateTime:

1
UPDATE tbl_EmployeeDetails SET EmpDOB = '1991-09-12' WHERE EmpID=4;

The Result:

1
SELECT *FROM tbl_EmployeeDetails;

PostgreSQL Update TimeStamp

Mar 6, 2016Anvesh Patel
SQL Server 2012: The Amazing CONCAT function for string concatenationNoSQL: The latest buzzword in Data Science, What is NoSQL? (Day 1)
Anvesh Patel
Anvesh Patel

Database Engineer

March 6, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, last updated time, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, TIMESTAMP
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....