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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:03:35+00:00 2026-05-27T02:03:35+00:00

Let’s say I have a table parent with primary key id , and a

  • 0

Let’s say I have a table parent with primary key id, and a table child with foreign key parent_id and a “boolean” column (constrained to a 0 or 1), let’s call it is_initial.

What I want to do is put a constraint on child so that for a particular value of parent_id, there can be only one row with is_initial = 1. There can be any number of rows with is_initial = 0.

Can this be done with a constraint? I prefer not to add a trigger.

Thanks.

  • 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-27T02:03:36+00:00Added an answer on May 27, 2026 at 2:03 am

    Here you go, I believe I understand what you are looking for now

    Note the change in the unique index:

    create unique index childTable_initialIndex on childTable(
      case when is_initial = 1 then parent_id 
      else null
      end);
    

    Ammended Code

    create table childTable(parent_id number, child_id number primary key, is_initial number, somethingelse varchar2(50) );
    
    create unique index childTable_initialIndex on childTable(
      case when is_initial = 1 then parent_id 
      else null
      end);
    
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (1,1,0,'works');
    1 rows inserted.  
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (1,1,0,'will not work if childId is pk');
    SQL Error: ORA-00001: unique constraint (SYS_C0062138) violated
    00001. 00000 -  "unique constraint (%s.%s) violated"
    *Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
               For Trusted Oracle configured in DBMS MAC mode, you may see
               this message if a duplicate entry exists at a different level.
    *Action:   Either remove the unique restriction or do not insert the key  
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (1,2,1,'works3');
    1 rows inserted.  
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (1,3,1,'should not work');
    SQL Error: ORA-00001: unique constraint (CHILDTABLE_INITIALINDEX) violated
    00001. 00000 -  "unique constraint (%s.%s) violated"
    *Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
               For Trusted Oracle configured in DBMS MAC mode, you may see
               this message if a duplicate entry exists at a different level.
    *Action:   Either remove the unique restriction or do not insert the key.  
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (2,4,0,'works4');
    1 rows inserted.
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (2,5,0,'works5');
    1 rows inserted.
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (2,6,1,'works6');
    1 rows inserted.
    
      insert into childTable(parent_id, child_id, is_initial,somethingelse) values (2,7,1,'should not work');
    SQL Error: ORA-00001: unique constraint (CHILDTABLE_INITIALINDEX) violated
    00001. 00000 -  "unique constraint (%s.%s) violated"
    *Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
               For Trusted Oracle configured in DBMS MAC mode, you may see
               this message if a duplicate entry exists at a different level.
    *Action:   Either remove the unique restriction or do not insert the key.  
      --we should only see things that work
      select * from childTable
    / 
    
    --this should not work, since works already has the 1/1 is_initial 1
    update childTable 
       set somethingelse = 'Should not work!'
         , is_initial    = 1
     where somethingelse = 'works';
    
    SQL Error: ORA-00001: unique constraint (CHILDTABLE_INITIALINDEX) violated
    00001. 00000 -  "unique constraint (%s.%s) violated"
    *Cause:    An UPDATE or INSERT statement attempted to insert a duplicate key.
               For Trusted Oracle configured in DBMS MAC mode, you may see
               this message if a duplicate entry exists at a different level.
    *Action:   Either remove the unique restriction or do not insert the key.
    

    Here are the results:

    PARENT_ID CHILD_ID IS_INITIAL SOMETHINGELSE                                    
    --------- -------- ---------- --------------------------------------------------
            1        1          0 works                                              
            1        2          1 works3                                             
            2        4          0 works4                                             
            2        5          0 works5                                             
            2        6          1 works6  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table and I want to insert a row. The
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I have a property foo defined in my parent POM. Is it
Let's say I have the following type type Key = String type Score =
Let's say we have a table that has 2 million rows. It has two
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to

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.