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

The Archive Base Latest Questions

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

Table: Relatives emp_id dep_id (composite primary key) We have to restrict one employee to

  • 0

Table: Relatives

  • emp_id
  • dep_id(composite primary key)

We have to restrict one employee to three dependents.

  • 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-28T00:23:42+00:00Added an answer on May 28, 2026 at 12:23 am

    This cannot be done using a check constraint alone, but there is a way using a materialized view and a check constraint as I demonstrate here on my blog. For your example this would be:

    create materialized view emp_dep_mv
    build immediate
    refresh complete on commit as
    select emp_id, count(*) cnt
    from relatives
    group by emp_id;
    
    alter table emp_dep_mv
    add constraint emp_dep_mv_chk
    check (cnt <= 3)
    deferrable;
    

    However, this approach might not be performant in a large, busy production database, in which case you could go for an approach that uses triggers and a check constraint, plus an extra column on the employees table:

    alter table employees add num_relatives number(1,0) default 0 not null;
    
    -- Populate for existing data
    update employees
    set num_relatives = (select count(*) from relatives r
                         where r.emp_id = e.emp_id)
    where exists (select * from relatives r
                  where r.emp_id = e.emp_id);
    
    alter table employees add constraint emp_relatives_chk
    check (num_relatives <= 3);
    
    create trigger relatives_trg
    after insert or update or delete on relatives
    for each row
    begin
       if inserting or updating then
          update employees
          set    num_relatives = num_relatives + 1
          where  emp_id = :new.emp_id;
       end if;
       if deleting or updating then
          update employees
          set    num_relatives = num_relatives - 1
          where  emp_id = :old.emp_id;
       end if;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table relationship which links one person to many relatives. so the
I've got a table of 5,651,744 rows, with a primary key made of 6
I have a table where one column has duplicate records but other columns are
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I have the following html code: <table> <tr> <td> <div id=fixmywidth style=position:relative; height:30px;> <div
I have a relatively large table (5,208,387 rows, 400mb data/670mb index), all columns i
I have Movie table & TblMovieVideos table and I have relations with this tables,
I have a database table with a field containing RTF (Rich text Format). I
I have a table with observations (x, y) and need to estimate the mean
I have a table with a bunch of cells. (No way! Amazing! :P) Some

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.