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 3932826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:31:58+00:00 2026-05-19T23:31:58+00:00

I have table Candidates with id (primary key) and application_counter and table Applications with

  • 0

I have table "Candidates" with id (primary key) and application_counter
and table "Applications" with foreign key (candidate_id). I want application_counter to be modified each time Application is added or removed (or modified by changing candidate_id).

All I can do is to write:

CREATE TRIGGER myTrigger AFTER INSERT OR DELETE OR UPDATE
ON "Applications" FOR EACH ROW
EXECUTE PROCEDURE funcname ( arguments )

And the question is How can I write this trigger?

Synopsis from page http://www.postgresql.org/docs/8.1/interactive/sql-createtrigger.html

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
ON table [ FOR [ EACH ] { ROW | STATEMENT } ]
EXECUTE PROCEDURE funcname ( arguments )
  • 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-05-19T23:31:58+00:00Added an answer on May 19, 2026 at 11:31 pm

    I’d use a view, INNER JOIN the two tables, and count the rows in the applications table. (See COUNT().) Triggers can be disabled; that view will always give you the right answer.

    (Later . . .)

    I understand you want to limit a candidate’s rows in the table “applications” to 3 or less. In that case, I think it’s best to use a CHECK() constraint on “applications” rather than the combination of a trigger on “applications” and a CHECK() constraint on “candidates”.

    To do that in PostgreSQL, you have to use a function, and call the function from the CHECK(). (As far as I know. You still can’t execute arbitrary SELECT statements in CHECK() constraints, right?) So, you’d create this function,

    CREATE FUNCTION num_applications(cand_id integer)
      RETURNS integer AS
    $BODY$ 
    
      DECLARE 
        n integer; 
      BEGIN 
        select count(*) 
        into n 
        from applications
        where (candidate_id = cand_id);
    
        return n; 
      END; 
    $BODY$ 
    LANGUAGE plpgsql;
    

    and then you’d add a CHECK() constraint to the table ‘applications’.

    ALTER TABLE applications
      ADD CONSTRAINT max_of_three CHECK (num_applications(candidate_id) < 3);
    

    “< 3” because the CHECK() constraint is evaluated before adding a row. It’s probably worth testing to see how this behaves with deferred constraints. If I have time later, I’ll do that.

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

Sidebar

Related Questions

I have a table with subscription values and want to calculate the time when
I have table rows of data in html being filled from a CGI application.
Struggling getting a query to work…….. I have two tables:- tbl.candidates: candidate_id agency_business_unit_id tbl.candidate_employment_tracker
Why is it necessary to have a primary key on a column of one
I am trying to add a foreign key to a table, and it give
I have table with Timestamp, temperature and humidity. I want to have the average
I have table project, screenshot, sub_screenshot, and user I want to achieve this kind
I have table in my gsp. And I want to have vertical scroll bar
I have table inside a div tab. The table has 40 rows in it
I have table with 50 entries (users with such details like Name Surname Location

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.