create unique index multiple columns with one is columns value is zero
I have a table users
with following columns and attributes
users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0
)
foreigner values is 0 and 1 default 0
how I can create unique index on column ssn, when checked foreigner value is 0.
id ssn foreigner
1 55 0 true
2 60 1 true
3 60 1 true
----------------------------------
4 55 0 false
----------------------------------
5 77 0 true
mysql sql database
add a comment |
I have a table users
with following columns and attributes
users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0
)
foreigner values is 0 and 1 default 0
how I can create unique index on column ssn, when checked foreigner value is 0.
id ssn foreigner
1 55 0 true
2 60 1 true
3 60 1 true
----------------------------------
4 55 0 false
----------------------------------
5 77 0 true
mysql sql database
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44
add a comment |
I have a table users
with following columns and attributes
users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0
)
foreigner values is 0 and 1 default 0
how I can create unique index on column ssn, when checked foreigner value is 0.
id ssn foreigner
1 55 0 true
2 60 1 true
3 60 1 true
----------------------------------
4 55 0 false
----------------------------------
5 77 0 true
mysql sql database
I have a table users
with following columns and attributes
users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0
)
foreigner values is 0 and 1 default 0
how I can create unique index on column ssn, when checked foreigner value is 0.
id ssn foreigner
1 55 0 true
2 60 1 true
3 60 1 true
----------------------------------
4 55 0 false
----------------------------------
5 77 0 true
mysql sql database
mysql sql database
edited Nov 15 '18 at 12:23
Raza Rafaideen
5381518
5381518
asked Nov 15 '18 at 11:43
Meqenaneri VacharqMeqenaneri Vacharq
217
217
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44
add a comment |
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44
add a comment |
1 Answer
1
active
oldest
votes
You can only do this without a trigger in MySQL using the most recent versions of MySQL.
create table users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0,
foreigner_ssn tinyint generated always as (case when foreigner = 0 then ssn end) store unique
);
This creates a new generated column. MySQL unique indexes to repeat NULL
values, so the condition is really only on foreigner = 0
.
You could possibly simplify this, just by having ssn
be NULL
when foreigner = 0
and declaring that to be unique
.
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (thestore
keyword). I wasn't sure if that would be necessary.
– Gordon Linoff
Nov 15 '18 at 13:24
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%2f53318746%2fcreate-unique-index-multiple-columns-with-one-is-columns-value-is-zero%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
You can only do this without a trigger in MySQL using the most recent versions of MySQL.
create table users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0,
foreigner_ssn tinyint generated always as (case when foreigner = 0 then ssn end) store unique
);
This creates a new generated column. MySQL unique indexes to repeat NULL
values, so the condition is really only on foreigner = 0
.
You could possibly simplify this, just by having ssn
be NULL
when foreigner = 0
and declaring that to be unique
.
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (thestore
keyword). I wasn't sure if that would be necessary.
– Gordon Linoff
Nov 15 '18 at 13:24
add a comment |
You can only do this without a trigger in MySQL using the most recent versions of MySQL.
create table users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0,
foreigner_ssn tinyint generated always as (case when foreigner = 0 then ssn end) store unique
);
This creates a new generated column. MySQL unique indexes to repeat NULL
values, so the condition is really only on foreigner = 0
.
You could possibly simplify this, just by having ssn
be NULL
when foreigner = 0
and declaring that to be unique
.
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (thestore
keyword). I wasn't sure if that would be necessary.
– Gordon Linoff
Nov 15 '18 at 13:24
add a comment |
You can only do this without a trigger in MySQL using the most recent versions of MySQL.
create table users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0,
foreigner_ssn tinyint generated always as (case when foreigner = 0 then ssn end) store unique
);
This creates a new generated column. MySQL unique indexes to repeat NULL
values, so the condition is really only on foreigner = 0
.
You could possibly simplify this, just by having ssn
be NULL
when foreigner = 0
and declaring that to be unique
.
You can only do this without a trigger in MySQL using the most recent versions of MySQL.
create table users (
id int(11),
ssn varchar(100),
foreigner tinyint(1) default 0,
foreigner_ssn tinyint generated always as (case when foreigner = 0 then ssn end) store unique
);
This creates a new generated column. MySQL unique indexes to repeat NULL
values, so the condition is really only on foreigner = 0
.
You could possibly simplify this, just by having ssn
be NULL
when foreigner = 0
and declaring that to be unique
.
edited Nov 15 '18 at 13:24
answered Nov 15 '18 at 11:50
Gordon LinoffGordon Linoff
766k35300402
766k35300402
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (thestore
keyword). I wasn't sure if that would be necessary.
– Gordon Linoff
Nov 15 '18 at 13:24
add a comment |
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (thestore
keyword). I wasn't sure if that would be necessary.
– Gordon Linoff
Nov 15 '18 at 13:24
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
topicstarter and or future readers but keep in mind this query only works on a InnoDB table engine which supports indexing on virtaul columns like this.. MyISAM only supports it when you use stored see demos InnoDB db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/1 and MyISAM db-fiddle.com/f/5EFMpZn1czkM5rM8rJPb3V/0 .. But who is using MyISAM nowadays all modern MySQL versions run InnoDB table engine as default now..
– Raymond Nijland
Nov 15 '18 at 12:20
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
thank you for your response. I am using 5.5.56-MariaDB
– Meqenaneri Vacharq
Nov 15 '18 at 12:29
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
[Err] 1904 - Key/Index cannot be defined on a non-stored computed column
– Meqenaneri Vacharq
Nov 15 '18 at 13:05
@MeqenaneriVacharq . . . So store the value (the
store
keyword). I wasn't sure if that would be necessary.– Gordon Linoff
Nov 15 '18 at 13:24
@MeqenaneriVacharq . . . So store the value (the
store
keyword). I wasn't sure if that would be necessary.– Gordon Linoff
Nov 15 '18 at 13:24
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%2f53318746%2fcreate-unique-index-multiple-columns-with-one-is-columns-value-is-zero%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
Can there be multiple rows possible with (55,1) ?
– Madhur Bhaiya
Nov 15 '18 at 11:44