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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:53:45+00:00 2026-06-09T00:53:45+00:00

I have a problem in a hierarchical query. I have a self-referencing table like

  • 0

I have a problem in a hierarchical query. I have a self-referencing table like this:

    id    parent_id    name    size
-----------------------------------------
    1       null       Ship1    50
    2        1         cabin1   10
    3        1         cabin2   30
    4        3         shelf1    5
    5        3         shelf2   20
    6       null       Ship2    50
    7        6         cabin1   10
    8        6         cabin2   30
    9        7         shelf1   15

I need to add a trigger in the database which prevents the user from adding a cabin which exceeds the remaining size of that ship.

For example, in the table, for ship 1, we have a total size of 50. There are 2 cabins present which consume the size of 40. So now we are left with 10 as the available space. We should not be able to add another cabin(s) with size>10. Anything <= 10 is fine.
Similar is the case for cabins and shelves. The total size of all the shelves in a cabin should not exceed the total size allotted for that cabin.

There can be any number of ship-entries in the table (which is the root).

I know about hierarchical queries and i can traverse a tree and all but I’m finding it difficult to gather my thoughts on the problem. Could anyone please point me in the right direction so that I can add that trigger successfully?

  • 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-09T00:53:47+00:00Added an answer on June 9, 2026 at 12:53 am

    I agree with @N_west that you should probably have separate tables for Ships, Cabins and Shelves, for your minimalist purpose and for the ease of maintenance (archiving/purging etc.).
    If you want to have a Trigger to handle this then you will have to capture the data from SHIPS to a log table using a trigger on SHIPS then use the data on log table to verify against insert on SHIPS. Its not best of solution but can achieve what you want. You can have a user defined exception to handle ALERTS in your application based on the error code (20101).

    SQL> CREATE TABLE LOG_SHIPS AS SELECT * FROM SHIPS;
    
    SQL> CREATE or REPLACE TRIGGER TRG_SHIP
    BEFORE INSERT ON SHIPS
    FOR EACH ROW
    
    L_count   NUMBER(10);
    L_total   NUMBER(10);
    e_exp     EXCEPTION;
    
    BEGIN
         SELECT sum(size) INTO L_count
           FROM LOG_SHIPS
          WHERE parent_id = :new.parent_id;
    
         SELECT size INTO L_total
           FROM LOG_SHIPS
          WHERE id = :new.parent_id;
    
         if L_count+:new.size > L_total then
            RAISE e_exp;
         else
            INSERT INTO LOG_SHIPS VALUES (:new.id,:new.parent_id,:new.name,:new.size);
         end if;
    
    EXCEPTION
       WHEN e_exp THEN
         RAISE_APPLICATION_ERROR (-20101,'Size entered exceeds limit.');
       WHEN others THEN
          null; -- do some meaningful exception handling here
    END;
    /
    

    Another approach would be to use COMPOUND TRIGGERS only if you are using Oracle 11g.

    • 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 hierarchical structure. like this: (source: aspalliance.com ) and table
I have problem with my query on C, I’m using the oci8 driver. This
I got a problem with Entity Framework 4.0 I have a hierarchical table Category:
I have a self reference table to store hierarchical values in order to show
I have problem about group in cluster analysis(hierarchical cluster). As example, this is the
I have a database table, with people identified by a name, a job and
In flex 3 I have a hierarchical data structure. I would like to display
What's the best way to approach this problem: I have a list of navigation
I have a similar problem with this ( https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination ) and can't really figure
i have the following table data in ms sql server table name category and

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.