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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:27:07+00:00 2026-05-23T00:27:07+00:00

I can’t find a way to apply an ‘advanced’ query-based constraint. For example, with

  • 0

I can’t find a way to apply an ‘advanced’ query-based constraint.

For example, with the following fictitious schema, how can I enforce that the total percentage of Bob’s daily activities does not exceed 100%?

I’ve already looked into trigger constraints but I don’t think they’re going to do what I want (abort the INSERT/UPDATE if some criteria is met).

Thanks for reading my question and any help you can offer.

create table employee (
    id serial PRIMARY KEY,
    name varchar(100) NOT NULL
);

create table work_day (
    employee_id integer references employee(id) NOT NULL,
    percentage integer NOT NULL CHECK (percentage > 0 and percentage <= 100),
    activity varchar(100) NOT NULL
);

INSERT INTO employee (name) VALUES ('bob');

-- Bob spends 50% of the day slacking, 20% eating and 30% working (total = 100%)
INSERT INTO work_day (employee_id, percentage, activity) VALUES (1, 50, 'slacking'); 
INSERT INTO work_day (employee_id, percentage, activity) VALUES (1, 20, 'eating');
INSERT INTO work_day (employee_id, percentage, activity) VALUES (1, 30, 'working');

-- This should be invalid!!! 100% of Bob's time has already been allocated
INSERT INTO work_day (employee_id, percentage, activity) VALUES (1, 10, 'invalid');
  • 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-23T00:27:08+00:00Added an answer on May 23, 2026 at 12:27 am

    A pl/pgsql-based constraint trigger is what you want. sum() the thing, and check if it is within the defined bounds. If not, raise an exception and the statement will be cancelled.

    Alternatively, have a normal trigger store/update the total activity in the employee table, and place a constraint on the latter.

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

Sidebar

Related Questions

Can anyone give me an example of how to return the following json simply
Can we do following in SQL Server 2005 query with convert function. MaxRegID =
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can someone tell me why the following does not match: >>> re.search(r'(\d{2, 10})', '153')
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can someone explain dependency injection with a basic .NET example and provide a few
Can someone help me? I am trying to do something like the following: #include
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through

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.