Two depended ManyToMany Releations
I have a user who may have 0 or more foo's and bar's. Here is a diagram:

Each foo has a unique integer ord.
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar.
My thought how to avoid logical inconsistencies is to simple never insert any foo_user relation with ord=1. However when I want to query the foo of a user with smallest ord I need to do this a bit complicated like this:
- Query: Check if bar relation exists. If it does, get foo with
ord=1
- Query: Check if bar relation exists. If it does, get foo with
- Query: If no bar bar relation exists, get foo with smallest
ordcolumn.
- Query: If no bar bar relation exists, get foo with smallest
Is there maybe a more convenient database structure for this scenario?
mysql relational-theory
add a comment |
I have a user who may have 0 or more foo's and bar's. Here is a diagram:

Each foo has a unique integer ord.
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar.
My thought how to avoid logical inconsistencies is to simple never insert any foo_user relation with ord=1. However when I want to query the foo of a user with smallest ord I need to do this a bit complicated like this:
- Query: Check if bar relation exists. If it does, get foo with
ord=1
- Query: Check if bar relation exists. If it does, get foo with
- Query: If no bar bar relation exists, get foo with smallest
ordcolumn.
- Query: If no bar bar relation exists, get foo with smallest
Is there maybe a more convenient database structure for this scenario?
mysql relational-theory
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seemsbarmust be joined tofoowith constraint offoo.ord=1, not touser...
– Akina
18 mins ago
add a comment |
I have a user who may have 0 or more foo's and bar's. Here is a diagram:

Each foo has a unique integer ord.
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar.
My thought how to avoid logical inconsistencies is to simple never insert any foo_user relation with ord=1. However when I want to query the foo of a user with smallest ord I need to do this a bit complicated like this:
- Query: Check if bar relation exists. If it does, get foo with
ord=1
- Query: Check if bar relation exists. If it does, get foo with
- Query: If no bar bar relation exists, get foo with smallest
ordcolumn.
- Query: If no bar bar relation exists, get foo with smallest
Is there maybe a more convenient database structure for this scenario?
mysql relational-theory
I have a user who may have 0 or more foo's and bar's. Here is a diagram:

Each foo has a unique integer ord.
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar.
My thought how to avoid logical inconsistencies is to simple never insert any foo_user relation with ord=1. However when I want to query the foo of a user with smallest ord I need to do this a bit complicated like this:
- Query: Check if bar relation exists. If it does, get foo with
ord=1
- Query: Check if bar relation exists. If it does, get foo with
- Query: If no bar bar relation exists, get foo with smallest
ordcolumn.
- Query: If no bar bar relation exists, get foo with smallest
Is there maybe a more convenient database structure for this scenario?
mysql relational-theory
mysql relational-theory
edited 1 min ago
Adam
asked 1 hour ago
AdamAdam
1033
1033
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seemsbarmust be joined tofoowith constraint offoo.ord=1, not touser...
– Akina
18 mins ago
add a comment |
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seemsbarmust be joined tofoowith constraint offoo.ord=1, not touser...
– Akina
18 mins ago
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seems
bar must be joined to foo with constraint of foo.ord=1, not to user...– Akina
18 mins ago
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seems
bar must be joined to foo with constraint of foo.ord=1, not to user...– Akina
18 mins ago
add a comment |
1 Answer
1
active
oldest
votes
Schematically:
CREATE TABLE users (id,
name);
CREATE TABLE foo (id,
user_id,
name,
ord,
FK (user_id) REF users (id),
KEY (id, ord));
CREATE TABLE bar (id,
foo_id,
name,
ord AS (1) VIRTUAL,
FK (foo_id, ord) REF foo (id, ord));
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. Onefoomay also have many users so the pivot tables are necessary.
– Adam
1 min ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f227853%2ftwo-depended-manytomany-releations%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
Schematically:
CREATE TABLE users (id,
name);
CREATE TABLE foo (id,
user_id,
name,
ord,
FK (user_id) REF users (id),
KEY (id, ord));
CREATE TABLE bar (id,
foo_id,
name,
ord AS (1) VIRTUAL,
FK (foo_id, ord) REF foo (id, ord));
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. Onefoomay also have many users so the pivot tables are necessary.
– Adam
1 min ago
add a comment |
Schematically:
CREATE TABLE users (id,
name);
CREATE TABLE foo (id,
user_id,
name,
ord,
FK (user_id) REF users (id),
KEY (id, ord));
CREATE TABLE bar (id,
foo_id,
name,
ord AS (1) VIRTUAL,
FK (foo_id, ord) REF foo (id, ord));
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. Onefoomay also have many users so the pivot tables are necessary.
– Adam
1 min ago
add a comment |
Schematically:
CREATE TABLE users (id,
name);
CREATE TABLE foo (id,
user_id,
name,
ord,
FK (user_id) REF users (id),
KEY (id, ord));
CREATE TABLE bar (id,
foo_id,
name,
ord AS (1) VIRTUAL,
FK (foo_id, ord) REF foo (id, ord));
Schematically:
CREATE TABLE users (id,
name);
CREATE TABLE foo (id,
user_id,
name,
ord,
FK (user_id) REF users (id),
KEY (id, ord));
CREATE TABLE bar (id,
foo_id,
name,
ord AS (1) VIRTUAL,
FK (foo_id, ord) REF foo (id, ord));
answered 7 mins ago
AkinaAkina
3,6681311
3,6681311
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. Onefoomay also have many users so the pivot tables are necessary.
– Adam
1 min ago
add a comment |
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. Onefoomay also have many users so the pivot tables are necessary.
– Adam
1 min ago
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. One
foo may also have many users so the pivot tables are necessary.– Adam
1 min ago
Sorry I think I was unclear in the title, foo and bar are ManyToMany relations. One
foo may also have many users so the pivot tables are necessary.– Adam
1 min ago
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f227853%2ftwo-depended-manytomany-releations%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
If a user has a bar, then this implies that he has a foo with ord=1. A user may not have foo with ord=1 without a bar. it seems
barmust be joined tofoowith constraint offoo.ord=1, not touser...– Akina
18 mins ago