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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:25:05+00:00 2026-06-09T13:25:05+00:00

I came across this example on SO which gives a solution to create a

  • 0

I came across this example on SO which gives a solution to create a unique index by ignoring nulls. However, I want to extend it and I am not able reach to a solution.

I have a composite index for 3 columns of a table (there are other 10 columns in the table). These columns are not part of PK. Of these 3 columns, 2 will always hold some value and 3rd may be NULL. I have huge test data, and there are many inserts with 2 of the columns with same value and 3rd column NULL. These all inserts work well for PostgreSQL, however Oracle complains. For my test cases to work, I think the simplest solution that I think is to try a unique index for Oracle that would work as it works in PostgreSQL.

Precisely: I want a construct of following kind, not sure how to combine col1 + col2 + col3

create unique index tbl_idx on tbl (nvl2(col3, col1 + col2, col1 + col2 + col3))

I am using liquibase. Index is created in following way –

<changeSet dbms="postgresql,oracle" author="abc" id="222">
    <createIndex indexName="Index_7" schemaName="ss" tableName="Users" unique="true">
        <column name="idOrganization"/>
        <column name="strUsername"/>
        <column name="strNotDeleted"/>
    </createIndex>
</changeSet>

I am using liquibase to create my test data, here are two insert statements

<insert schemaName="ss" tableName="Users">
    <column name="strUsername" value="user1" />
    <column name="idUser" valueNumeric="20741" />
    <column name="idOrganization" valueNumeric="4" />
    <column name="strFirstName" value="user" />
    <column name="strLastName" value="one" />
    <column name="strEmail" value="email@foo.com" />
    <column name="strNotDeleted" />
</insert>
<insert schemaName="ss" tableName="Users">
    <column name="strUsername" value="user1" />
    <column name="idUser" valueNumeric="20771" />
    <column name="idOrganization" valueNumeric="4" />
    <column name="strFirstName" value="user" />
    <column name="strLastName" value="one" />
    <column name="strEmail" value="email@foo.com" />
    <column name="strNotDeleted" />
</insert>

These 2 inserts work fine for PostgreSQL, however fail for Oracle with error “Index_7 constraint violation”.

  • 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-09T13:25:06+00:00Added an answer on June 9, 2026 at 1:25 pm

    If the goal is only to prevent duplicates where strNotDeleted is set to a non-NULL value, then you want a function-based index like this

    SQL> create table users(
      2    idOrganization number,
      3    strUsername    varchar2(100),
      4    strNotDeleted  varchar2(3)
      5  );
    
    Table created.
    
    
    SQL> create unique index idx_users
      2      on users( (case when strNotDeleted is not null
      3                      then idOrganization
      4                      else null
      5                  end),
      6                (case when strNotDeleted is not null
      7                      then strUsername
      8                      else null
      9                 end) );
    
    Index created.
    

    This allows the two rows you mention in your question to be inserted

    SQL> insert into users values( 4, 'user', null );
    
    1 row created.
    
    SQL> insert into users values( 4, 'user', null );
    
    1 row created.
    

    You can insert one row where the strNotNull column is set to a non-NULL value

    SQL> insert into users values( 4, 'user', 'Yes' );
    
    1 row created.
    

    But you then can’t insert a second such row

    SQL> insert into users values( 4, 'user', 'Yes' );
    insert into users values( 4, 'user', 'Yes' )
    *
    ERROR at line 1:
    ORA-00001: unique constraint (SCOTT.IDX_USERS) violated
    

    Behind the scenes, an Oracle b*-tree index does not index completely NULL entries. The two CASE statements ensure that the index only has entries for idOrganization and strUsername if strNotDeleted is not NULL. If strNotDeleted is NULL, then both CASE statements evaluate to NULL and no entry is made in the index. Conceptually, it’s similar to a partial index in other databases which allows you to specify a WHERE clause on your index so that you only index “interesting” rows.

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

Sidebar

Related Questions

I came across this free example website design template which has some links across
After searching for a tableless model example I came across this code which seems
I came across this example class http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/sp_techniques.html#as_lock and I'm struggling with the syntax. class
While I was reading Oracle Swing documentation, I came across this example of a
The other day, I came across this construct: static_cast<size_type>(-1) in some example C++ code,
I came across this problem when I want to check what I input is
1) I came across THIS ARTICLE today, which states: The most popular method in
I was reading the Python docs about classes and came across this paragraph which
I came across a few articles like this one , which suggest that some
I came across this question about memory management of dictionaries, which mentions the intern

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.