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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:52:50+00:00 2026-06-07T19:52:50+00:00

Suppose I have a table User(userid, usernama) and a table Owner(ownerid, ownername) and a

  • 0

Suppose I have a

table User(userid, usernama) and a
table Owner(ownerid, ownername) and a joined
table UserOwnerMapping(userid, ownerid, IS_MASTER)
which create a many-to-many between user and owner

The is_master default is ‘N’. A user can only has one master.

How can I used a constraint to force no more than one master
for a user?

I am using Oracle.

  • 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-07T19:52:51+00:00Added an answer on June 7, 2026 at 7:52 pm

    You can achieve the constraint using a unique function-based index:

    create unique index idx on userownermapping 
        (case when is_master='Y' then userid end);
    

    This will only index the userid when is_master = ‘Y’, so forces userid to be unique when is_master = ‘Y’ but not otherwise:

    SQL> create table UserOwnerMapping (userid integer, ownerid integer, IS_MASTER varchar2(1));
    
    Table created.
    
    SQL> 
    SQL> create unique index idx on userownermapping 
      2      (case when is_master='Y' then userid end);
    
    Index created.
    
    SQL>     
    SQL> insert into UserOwnerMapping (userid, ownerid, IS_MASTER) values (1, 2, 'Y');
    
    1 row created.
    
    SQL> 
    SQL> insert into UserOwnerMapping (userid, ownerid, IS_MASTER) values (1, 3, 'N');
    
    1 row created.
    
    SQL>     
    SQL> insert into UserOwnerMapping (userid, ownerid, IS_MASTER) values (1, 4, 'N');
    
    1 row created.
    
    SQL> 
    SQL> insert into UserOwnerMapping (userid, ownerid, IS_MASTER) values (1, 5, 'Y');
    insert into UserOwnerMapping (userid, ownerid, IS_MASTER) values (1, 5, 'Y')
    *
    ERROR at line 1:
    ORA-00001: unique constraint (MYSCHEMA.IDX) violated
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table as follows in MySQL: create table user(name varchar(20), join_time
Suppose you have a table like this: id user score It's a dump of
Suppose I have a table of users. Something like: ID integer, USER text, POSITION
Suppose I have table Person table Employee, which inherits Person. I want to get
Suppose I have a table events {id, userid, create_time, country, type, page, browser} I
Suppose I have a table like: create table { id numeric(5,3), code varchar(10) }
I have two tables, in which one user can have several contacts: User(UserId, UserName)
Suppose you have a table like: create table user_news ( user_id int unsigned not
I have three tables, 1-Users, 2-Softwares, 3-UserSoftwares. if suppose, Users table having 6 user
Suppose you have two models, User and City, joined by a third model CityPermission:

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.