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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:06:15+00:00 2026-05-18T02:06:15+00:00

We want to add a constraint in a Oracle database to check if the

  • 0

We want to add a constraint in a Oracle database to check if the employee’s salary respects the hierarchy.
An employee has an id, a name, a rank and a salary.
We have 3 ranks (rank1, rank2, rank3), rank1 is superior to rank2 and rank2 is superior to rank3.
When adding an employee that has rank2, his salary shouldn’t be superior to a salary of an employee with rank1.

Can you tell us which solution is the best to implement this constraint ?

Thank you very much

  • 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-18T02:06:16+00:00Added an answer on May 18, 2026 at 2:06 am

    I think you’ll end up having to use a trigger, and since you’ll need to refer to data within the same table on which the trigger exists it’ll need to be a table trigger rather than a row trigger. Something like this:

    CREATE OR REPLACE TRIGGER EMPLOYEES_AIU
      AFTER INSERT OR UPDATE ON EMPLOYEES
      -- Note: There is no FOR EACH ROW here so 'OLD' and 'NEW' values are not available
    DECLARE
      strPrev_rank      EMPLOYEES.RANK%TYPE;
      nPrev_max_salary  EMPLOYEES.SALARY%TYPE;
    BEGIN
      FOR aRow IN (SELECT RANK, MAX(SALARY) AS MAX_SALARY
                     FROM EMPLOYEES
                     GROUP BY RANK
                     ORDER BY RANK DESC)
      LOOP
        IF nPrev_max_salary IS NOT NULL AND
             aRow.MAX_SALARY > nPrev_max_salary THEN
          RAISE_APPLICATION_ERROR(-20101, 'Max salary (' || aRow.MAX_SALARY||
                                          ') of rank ' || aRow.RANK ||
                                          ' exceeds max salary (' || nPrev_max_salary ||
                                          ') of rank ' || strPrev_rank);
        END IF;
    
        strPrev_rank := aRow.RANK;
        nPrev_max_salary := aRow.MAX_SALARY;
      END LOOP;
    END EMPLOYEES_AIU;
    

    I’ve used similar triggers to check data validity and have found it works well.

    Share and enjoy.

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

Sidebar

Related Questions

On a oracle database I have a foreign key, without knowing its name, just
I have a fairly simple check constraint, where I want to force two columns
Question: I want to add a unique constraint on a mapping table (n:n). I
I want add my custom sidebar next right column all page. Please check this
I have a website built with ASP.NET (3.5) and want add some level of
I have a list of string values that I want add to a hashtable
I have problem, when I want add Node to my GUI from other Thread.
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to alter a table to add a constraint during upgrade on a
I have a slightly unique requirement with the Java-JDBC API along with Oracle Database.

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.