How to create an unique constraint that won't allow any other character?
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
add a comment |
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
add a comment |
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
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
oracle oracle11g
edited Nov 14 '18 at 22:22
Patty
asked Nov 13 '18 at 22:45
PattyPatty
73831837
73831837
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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);
Actually - ODDLY - in aCONSTRAINT
, a condition is "satisfied" if it is eitherTRUE
orUNKNOWN
, so you don't need to handleNULL
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Actually - ODDLY - in aCONSTRAINT
, a condition is "satisfied" if it is eitherTRUE
orUNKNOWN
, so you don't need to handleNULL
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
add a comment |
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);
Actually - ODDLY - in aCONSTRAINT
, a condition is "satisfied" if it is eitherTRUE
orUNKNOWN
, so you don't need to handleNULL
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
add a comment |
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);
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);
answered Nov 13 '18 at 23:16
Luke WoodwardLuke Woodward
44.8k126688
44.8k126688
Actually - ODDLY - in aCONSTRAINT
, a condition is "satisfied" if it is eitherTRUE
orUNKNOWN
, so you don't need to handleNULL
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
add a comment |
Actually - ODDLY - in aCONSTRAINT
, a condition is "satisfied" if it is eitherTRUE
orUNKNOWN
, so you don't need to handleNULL
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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