Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:14:04+00:00 2026-06-15T12:14:04+00:00

how can i create a trigger function before adding/updating ,the function should check records

  • 0

how can i create a trigger function before adding/updating,the function should check records that have the same id (i.e comparison by id with existing objects that have the same property as the temporary_object)If a record with the id is found, then that entry is set to the time_dead, and then it adds an entry containing the corresponding values ​​of the attributes found in that record (except those that are set for a new record), when time_dead is empty then time_create of a new time is equal to that time at the current moment . Thus,a new record time_create is like the time_dead's ancestor.
If a record with that id is found then it is added to the database with the establishment as the time_create of the current time.

for example here is a simple explanation(just for explanation purposes)

id time_create   time-dead  student  amount

 1  06.12           07.12    henry   500

 1  07.12                    henry   1000

so if a student called henry with id 1 entered a room at 06.12 and left at 07.12 the next time he enters another room again time_dead will be equal to time_create(so time_dead of old entry and time_create of new entry – will be equal)
these are my tables below in sql format

CREATE TABLE temporary_object
(
  id integer NOT NULL,
  time_create timestamp without time zone NOT NULL,
  time_dead timestamp without time zone,
  CONSTRAINT temporary_object_pkey PRIMARY KEY (id, time_create)
)

CREATE TABLE persons
(
  fname text,
  fsurname text,
)
INHERITS (temporary_object)


CREATE TABLE rooms
(
  roomnum integer,
  course integer,
  passport text,
  students_number text
)
INHERITS (temporary_object)

this is what i am trying to do but im afraid i do not know how to finish it but im 100% not right may some help out

CREATE TRIGGER trigger2
 BEFORE INSERT OR UPDATE
 ON persons
 FOR EACH ROW
 EXECUTE PROCEDURE func1();

and this is the function

 CREATE OR REPLACE FUNCTION func1() RETURNS TRIGGER AS $persons$
 DECLARE
        time_create          integer;
        time_dead     timestamp;
        id       timestamp;
    BEGIN

 IF (TG_OP = 'INSERT') THEN

            time_create=
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:14:05+00:00Added an answer on June 15, 2026 at 12:14 pm

    I can’t tell you what I’m missing from your question, but I try to answer what I think I understood.

    Row level triggers can access the version of the row affected with the NEW and OLD variables (depending on TG_OP). In this case, you can use NEW:

    CREATE OR REPLACE FUNCTION func1() 
    RETURNS TRIGGER AS 
    $persons$
    DECLARE
        i integer;
    BEGIN
        IF TG_OP = 'INSERT'
        THEN
            UPDATE persons 
            SET time_dead = NEW.time_create
            WHERE 
                id = NEW.id -- we are looking for the same ID
                AND time_dead IS NULL
            ;
        ELSE -- UPDATE
            -- do here something
        END IF;
    END;
    $persons$
    LANGUAGE plpgsql;
    

    This is only a starter, modify it to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a trigger function in PostgreSQL that should check records
I have this Trigger in Postgresql that I can't just get to work (does
The trigger below is delaying my insert response. How can I prevent this? create
I can create a contact that is not mail enabled, but how do I
One can create an anonymous object that is initialized through constructor parameters, such as
How to create a trigger that is activated by a change in a database
I need to create a view or a table valued function that returns one
How can I update table's column in a trigger after update on the same
I have the following trigger: CREATE TRIGGER sum AFTER INSERT ON news FOR EACH
I am trying to create a trigger that will update a GEOMETRY column based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.