How to create an unique constraint that won't allow any other character?










1















I am using Oracle 12c. I have a table named myTestTab and that table has a column named "active". The column is defined as



ACTIVE varchar2(1)


So that only one character can be entered in that column. Now, for this column,



  • There can be no more than one row that can have value 'Y' in it.

  • Additionally, have to add a check constraint so that nothing but a 'Y' can be entered into that column (to prevent the user from entering any other value).

So far, to match the requirement I have created an index on that table as:



create unique index only_one_yes on myTestTab (case when upper(ACTIVE)='Y' then 'Y' else '' end);


However, it's not setting lower-case 'y' as upper-case 'y' and it is accepting any other character as long as it is unique. May I get any idea on how to fix it?










share|improve this question




























    1















    I am using Oracle 12c. I have a table named myTestTab and that table has a column named "active". The column is defined as



    ACTIVE varchar2(1)


    So that only one character can be entered in that column. Now, for this column,



    • There can be no more than one row that can have value 'Y' in it.

    • Additionally, have to add a check constraint so that nothing but a 'Y' can be entered into that column (to prevent the user from entering any other value).

    So far, to match the requirement I have created an index on that table as:



    create unique index only_one_yes on myTestTab (case when upper(ACTIVE)='Y' then 'Y' else '' end);


    However, it's not setting lower-case 'y' as upper-case 'y' and it is accepting any other character as long as it is unique. May I get any idea on how to fix it?










    share|improve this question


























      1












      1








      1


      0






      I am using Oracle 12c. I have a table named myTestTab and that table has a column named "active". The column is defined as



      ACTIVE varchar2(1)


      So that only one character can be entered in that column. Now, for this column,



      • There can be no more than one row that can have value 'Y' in it.

      • Additionally, have to add a check constraint so that nothing but a 'Y' can be entered into that column (to prevent the user from entering any other value).

      So far, to match the requirement I have created an index on that table as:



      create unique index only_one_yes on myTestTab (case when upper(ACTIVE)='Y' then 'Y' else '' end);


      However, it's not setting lower-case 'y' as upper-case 'y' and it is accepting any other character as long as it is unique. May I get any idea on how to fix it?










      share|improve this question
















      I am using Oracle 12c. I have a table named myTestTab and that table has a column named "active". The column is defined as



      ACTIVE varchar2(1)


      So that only one character can be entered in that column. Now, for this column,



      • There can be no more than one row that can have value 'Y' in it.

      • Additionally, have to add a check constraint so that nothing but a 'Y' can be entered into that column (to prevent the user from entering any other value).

      So far, to match the requirement I have created an index on that table as:



      create unique index only_one_yes on myTestTab (case when upper(ACTIVE)='Y' then 'Y' else '' end);


      However, it's not setting lower-case 'y' as upper-case 'y' and it is accepting any other character as long as it is unique. May I get any idea on how to fix it?







      oracle oracle11g






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 22:22







      Patty

















      asked Nov 13 '18 at 22:45









      PattyPatty

      73831837




      73831837






















          1 Answer
          1






          active

          oldest

          votes


















          2















          it's not setting lower-case 'y' as upper-case 'y'...




          You haven't told it to. Your unique index permits at most one 'y' or 'Y' value, but indexes don't change the data going into a table. For that you will need to use a trigger, such as the following:



          CREATE OR REPLACE TRIGGER biu_myTestTab_active_uc
          BEFORE INSERT OR UPDATE ON myTestTab
          FOR EACH ROW
          BEGIN
          :new.active := UPPER(:new.active);
          END;
          /



          ... it is accepting any other character as long as it is unique




          Actually it's accepting any other character regardless of whether it is unique. You could insert 'X' five times if you wanted to and the index wouldn't stop you. Again, that's not something the index you have defined can do. As mentioned in your question, what you need is a check constraint:



          ALTER TABLE myTestTab ADD CONSTRAINT active_yes_or_null CHECK (active = 'Y' OR active IS NULL);





          share|improve this answer























          • Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

            – mathguy
            Nov 14 '18 at 1:10











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290653%2fhow-to-create-an-unique-constraint-that-wont-allow-any-other-character%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2















          it's not setting lower-case 'y' as upper-case 'y'...




          You haven't told it to. Your unique index permits at most one 'y' or 'Y' value, but indexes don't change the data going into a table. For that you will need to use a trigger, such as the following:



          CREATE OR REPLACE TRIGGER biu_myTestTab_active_uc
          BEFORE INSERT OR UPDATE ON myTestTab
          FOR EACH ROW
          BEGIN
          :new.active := UPPER(:new.active);
          END;
          /



          ... it is accepting any other character as long as it is unique




          Actually it's accepting any other character regardless of whether it is unique. You could insert 'X' five times if you wanted to and the index wouldn't stop you. Again, that's not something the index you have defined can do. As mentioned in your question, what you need is a check constraint:



          ALTER TABLE myTestTab ADD CONSTRAINT active_yes_or_null CHECK (active = 'Y' OR active IS NULL);





          share|improve this answer























          • Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

            – mathguy
            Nov 14 '18 at 1:10
















          2















          it's not setting lower-case 'y' as upper-case 'y'...




          You haven't told it to. Your unique index permits at most one 'y' or 'Y' value, but indexes don't change the data going into a table. For that you will need to use a trigger, such as the following:



          CREATE OR REPLACE TRIGGER biu_myTestTab_active_uc
          BEFORE INSERT OR UPDATE ON myTestTab
          FOR EACH ROW
          BEGIN
          :new.active := UPPER(:new.active);
          END;
          /



          ... it is accepting any other character as long as it is unique




          Actually it's accepting any other character regardless of whether it is unique. You could insert 'X' five times if you wanted to and the index wouldn't stop you. Again, that's not something the index you have defined can do. As mentioned in your question, what you need is a check constraint:



          ALTER TABLE myTestTab ADD CONSTRAINT active_yes_or_null CHECK (active = 'Y' OR active IS NULL);





          share|improve this answer























          • Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

            – mathguy
            Nov 14 '18 at 1:10














          2












          2








          2








          it's not setting lower-case 'y' as upper-case 'y'...




          You haven't told it to. Your unique index permits at most one 'y' or 'Y' value, but indexes don't change the data going into a table. For that you will need to use a trigger, such as the following:



          CREATE OR REPLACE TRIGGER biu_myTestTab_active_uc
          BEFORE INSERT OR UPDATE ON myTestTab
          FOR EACH ROW
          BEGIN
          :new.active := UPPER(:new.active);
          END;
          /



          ... it is accepting any other character as long as it is unique




          Actually it's accepting any other character regardless of whether it is unique. You could insert 'X' five times if you wanted to and the index wouldn't stop you. Again, that's not something the index you have defined can do. As mentioned in your question, what you need is a check constraint:



          ALTER TABLE myTestTab ADD CONSTRAINT active_yes_or_null CHECK (active = 'Y' OR active IS NULL);





          share|improve this answer














          it's not setting lower-case 'y' as upper-case 'y'...




          You haven't told it to. Your unique index permits at most one 'y' or 'Y' value, but indexes don't change the data going into a table. For that you will need to use a trigger, such as the following:



          CREATE OR REPLACE TRIGGER biu_myTestTab_active_uc
          BEFORE INSERT OR UPDATE ON myTestTab
          FOR EACH ROW
          BEGIN
          :new.active := UPPER(:new.active);
          END;
          /



          ... it is accepting any other character as long as it is unique




          Actually it's accepting any other character regardless of whether it is unique. You could insert 'X' five times if you wanted to and the index wouldn't stop you. Again, that's not something the index you have defined can do. As mentioned in your question, what you need is a check constraint:



          ALTER TABLE myTestTab ADD CONSTRAINT active_yes_or_null CHECK (active = 'Y' OR active IS NULL);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 23:16









          Luke WoodwardLuke Woodward

          44.8k126688




          44.8k126688












          • Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

            – mathguy
            Nov 14 '18 at 1:10


















          • Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

            – mathguy
            Nov 14 '18 at 1:10

















          Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

          – mathguy
          Nov 14 '18 at 1:10






          Actually - ODDLY - in a CONSTRAINT, a condition is "satisfied" if it is either TRUE or UNKNOWN, so you don't need to handle NULL separately. CHECK(active = 'Y') suffices. (Although... for clarity it is probably better to spell it out explicitly anyway.)

          – mathguy
          Nov 14 '18 at 1:10


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290653%2fhow-to-create-an-unique-constraint-that-wont-allow-any-other-character%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線