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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:10:00+00:00 2026-06-09T23:10:00+00:00

i have this table: id | product_group_id | percentage 1 | 1 | 0.2

  • 0

i have this table:

id | product_group_id | percentage
1  | 1                | 0.2
2  | 1                | 0.3
3  | 1                | 0.5
4  | 2                | 0.4
5  | 2                | 0.6

I want to know if there is a way to create constraints that for each product_group_id the percentage will be equal to 1 ( if product_group_id = 1 so 0.2 + 0.3 + 0.5 = 1).
For example if i will change the percentage where id = 2 to 0.8 or 0.1, the update (or insert) will fail because the sum is not 1.

Thanks in advanced.

  • 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-09T23:10:03+00:00Added an answer on June 9, 2026 at 11:10 pm

    It would generally make more sense to enforce this sort of constraint in the API you build on top of the table to manipulate the percentages. Assuming, of course, that each product_group_id will be manipulated by at most one session at a time.

    If you do really want to enforce this sort of restriction, you could create a materialized view that refreshes on commit and create a constraint on that materialized view. Something like

    SQL> create table table_name(
      2    id number primary key,
      3    product_group_id number,
      4    percentage number
      5  );
    
    Table created.
    
    SQL> create materialized view log on table_name;
    
    Materialized view log created.
    
    SQL> ed
    Wrote file afiedt.buf
    
      1  create materialized view mv_table_name
      2    refresh on commit
      3  as
      4  select product_group_id, sum(percentage) total_percentage
      5    from table_name
      6*  group by product_group_id
    SQL> /
    
    Materialized view created.
    
    SQL> alter table mv_table_name
      2    add( constraint sum_of_1 check( total_percentage = 1 ));
    
    Table altered.
    

    That will allow you to insert rows that sum to 1

    SQL> insert into table_name values( 1, 1, 0.5 );
    
    1 row created.
    
    SQL> insert into table_name values( 2, 1, 0.5 );
    
    1 row created.
    
    SQL> commit;
    
    Commit complete.
    

    and will throw an error when you try to commit changes that cause the sum to be something other than 1

    SQL> insert into table_name values( 3, 1, 0.1 );
    
    1 row created.
    
    SQL> commit;
    commit
    *
    ERROR at line 1:
    ORA-12008: error in materialized view refresh path
    ORA-02290: check constraint (SCOTT.SUM_OF_1) violated
    

    Note that this is checked at commit time which is what you really need since you’ll need to violate the constraint during a transaction when you want to insert multiple rows.

    And, as has been pointed out, if there is the possibility of rounding errors, you probably want the CHECK constraint to allow sums that are 1 +/- some small epsilon (i.e between 0.999999 and 1.000001)

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

Sidebar

Related Questions

I have a product table which simplifies to this: create table product(id int primary
I have an SQL table like this : sales(product,timestamp) I want to display a
I have two tables that I'm querying from. In one table, there are fields
I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
I have a table of product information with many bit columns. This table can
So I have a table like this Table 1 Quote Ref | Product A
I have this table, -------------------------------------------- | products_id | related_products_ids | | ----------------------------------------- | 1
I have this table Test as Id, SomeValue, SomeText contains about a mill records,
I have a stupid question, I have this table : id_product name value 1
I have a product table like this: Product name , Affiliate ID , ProductCode

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.