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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:26:41+00:00 2026-05-14T14:26:41+00:00

What is a good way to prevent a table with 2 columns, a (unique)

  • 0

What is a good way to prevent a table with 2 columns, a (unique) and b, from having any record where column b is equal to any value in column a? This would be used for a table of corrections like this,

MR   -> Mr
Prf. -> Prof.
MRs  -> Mrs

I can see how it could be done with a trigger and a subquery assuming no concurrent activity but a more declarative approach would be preferable.

This is an example of what should be prevented,

Wing Commdr. -> Wing Cdr.
Wing Cdr.    -> Wing Commander

Ideally the solution would work with concurrent inserts and updates.

  • 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-14T14:26:42+00:00Added an answer on May 14, 2026 at 2:26 pm

    You could use a materialized view to enforce your requirements (tested with 10.2.0.1).

    SQL> CREATE TABLE t (a VARCHAR2(20) NOT NULL PRIMARY KEY,
      2                  b VARCHAR2(20) NOT NULL);
    Table created
    
    SQL> CREATE MATERIALIZED VIEW LOG ON t WITH (b), ROWID INCLUDING NEW VALUES;     
    Materialized view log created
    
    SQL> CREATE MATERIALIZED VIEW mv
      2     REFRESH FAST ON COMMIT
      3  AS
      4  SELECT 1 umarker, COUNT(*) c, count(a) cc, a val_col
      5    FROM t
      6   GROUP BY a
      7  UNION ALL
      8  SELECT 2 umarker, COUNT(*), COUNT(b), b
      9    FROM t
     10    GROUP BY b;     
    Materialized view created
    
    SQL> CREATE UNIQUE INDEX idx ON mv (val_col);     
    Index created 
    

    The unique index will ensure that you can not have the same value in both columns (on two rows).

    SQL> INSERT INTO t VALUES ('Wing Commdr.', 'Wing Cdr.');     
    1 row inserted
    
    SQL> COMMIT;     
    Commit complete
    
    SQL> INSERT INTO t VALUES ('Wing Cdr.', 'Wing Commander');     
    1 row inserted
    
    SQL> COMMIT;     
    
    ORA-12008: erreur dans le chemin de régénération de la vue matérialisée
    ORA-00001: violation de contrainte unique (VNZ.IDX)
    
    SQL> INSERT INTO t VALUES ('X', 'Wing Commdr.');     
    1 row inserted
    
    SQL> COMMIT;
    
    ORA-12008: erreur dans le chemin de régénération de la vue matérialisée
    ORA-00001: violation de contrainte unique (VNZ.IDX)
    

    It will serialize during commit but only on the values of columns A and B (ie: in general it should not prevent concurrent disjoint activity).

    The unicity will only be checked at COMMIT time and some tools don’t expect commit to fail and may behave inappropriately. Also when the COMMIT fails, the entire transaction is rolled back and you lose any uncommited changes (you can not “retry”).

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

Sidebar

Related Questions

I'm trying to figure out a good way to prevent developers from force pushing
What would be a good way to show hidden content with javascript, without having
What would be a good way to determine if a string contains an IPv4
Is there any good way of truncating text with plain HTML and CSS, so
We've got a database that generally has this structure: Master Record Table id (pk)
What is a good way to persist querystring values in asp.net mvc? If I
XmlAttributeOverrides is a good way to override some attributes for serialization. MSDN says: You
What's a good way to check if a package is installed while within a
Is there a good way in asp.net MVC to trace ModelState errors? IsValid returns
Is there a good way to get the debug information that Django provides when

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.