Two depended ManyToMany Releations












0















I have a user who may have 0 or more foo's and bar's. Here is a diagram:
enter image description here



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:





    1. Query: Check if bar relation exists. If it does, get foo with ord=1



    1. Query: If no bar bar relation exists, get foo with smallest ord column.



Is there maybe a more convenient database structure for this scenario?










share|improve this question

























  • 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


















0















I have a user who may have 0 or more foo's and bar's. Here is a diagram:
enter image description here



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:





    1. Query: Check if bar relation exists. If it does, get foo with ord=1



    1. Query: If no bar bar relation exists, get foo with smallest ord column.



Is there maybe a more convenient database structure for this scenario?










share|improve this question

























  • 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
















0












0








0








I have a user who may have 0 or more foo's and bar's. Here is a diagram:
enter image description here



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:





    1. Query: Check if bar relation exists. If it does, get foo with ord=1



    1. Query: If no bar bar relation exists, get foo with smallest ord column.



Is there maybe a more convenient database structure for this scenario?










share|improve this question
















I have a user who may have 0 or more foo's and bar's. Here is a diagram:
enter image description here



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:





    1. Query: Check if bar relation exists. If it does, get foo with ord=1



    1. Query: If no bar bar relation exists, get foo with smallest ord column.



Is there maybe a more convenient database structure for this scenario?







mysql relational-theory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















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












1 Answer
1






active

oldest

votes


















0














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));




share
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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));




share
























  • 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
















0














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));




share
























  • 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














0












0








0







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));




share













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));





share











share


share










answered 7 mins ago









AkinaAkina

3,6681311




3,6681311













  • 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

















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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

ف. موراي أبراهام

صرب

كأس إنترتوتو