Options for structuring multiple associations to a record












0















This is 101 level stuff and I promise to read a book later, but in the meantime, given the following three tables:



Product
---------
ProductId (uniqueidentifier)


User
---------
UserId (uniqueidentifier)


Company
---------
CompanyId (uniqueidentifier)


If a Product can be assigned to different "owners" (i.e., a User, a Company, etc.), I can think of two table structures to accomplish this:



Solution 1 (1 table for all owners):



ProductAssignment
---------
ProductId,
OwnerId (constraint to ensure either UserId, CompanyId, etc.)


Solution 2 (1 table for each owner):



UserProduct
---------
UserId,
ProductId

CompanyProduct
---------
CompanyId,
ProductId


Solution 2 offers more of a "proper" model, but we have a lot of these scenarios and it's going to result in our database being littered with many dozens of these types of tables. It's also a pain to code for since I would need to create a new, redundant query each time a new owner table is added. I feel the maintenance and coding would be much easier for Solution 1, but maybe I'm wrong?



For what it's worth, most of the data access will be through LINQ to SQL (ASP.NET Web Forms project), and Entity Framework (ASP.NET MVC project). Very few sprocs and such.




  • What pros and cons am I missing for each solution?

  • Will bad things happen to me if I go with Solution 1? i.e., guys in black suits and sun glasses showing up at the office to make me "disappear?"

  • Is there a Solution 3?










share|improve this question
















bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

    – ypercubeᵀᴹ
    May 19 '15 at 21:06













  • Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

    – StronglyTyped
    May 20 '15 at 12:58
















0















This is 101 level stuff and I promise to read a book later, but in the meantime, given the following three tables:



Product
---------
ProductId (uniqueidentifier)


User
---------
UserId (uniqueidentifier)


Company
---------
CompanyId (uniqueidentifier)


If a Product can be assigned to different "owners" (i.e., a User, a Company, etc.), I can think of two table structures to accomplish this:



Solution 1 (1 table for all owners):



ProductAssignment
---------
ProductId,
OwnerId (constraint to ensure either UserId, CompanyId, etc.)


Solution 2 (1 table for each owner):



UserProduct
---------
UserId,
ProductId

CompanyProduct
---------
CompanyId,
ProductId


Solution 2 offers more of a "proper" model, but we have a lot of these scenarios and it's going to result in our database being littered with many dozens of these types of tables. It's also a pain to code for since I would need to create a new, redundant query each time a new owner table is added. I feel the maintenance and coding would be much easier for Solution 1, but maybe I'm wrong?



For what it's worth, most of the data access will be through LINQ to SQL (ASP.NET Web Forms project), and Entity Framework (ASP.NET MVC project). Very few sprocs and such.




  • What pros and cons am I missing for each solution?

  • Will bad things happen to me if I go with Solution 1? i.e., guys in black suits and sun glasses showing up at the office to make me "disappear?"

  • Is there a Solution 3?










share|improve this question
















bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

    – ypercubeᵀᴹ
    May 19 '15 at 21:06













  • Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

    – StronglyTyped
    May 20 '15 at 12:58














0












0








0








This is 101 level stuff and I promise to read a book later, but in the meantime, given the following three tables:



Product
---------
ProductId (uniqueidentifier)


User
---------
UserId (uniqueidentifier)


Company
---------
CompanyId (uniqueidentifier)


If a Product can be assigned to different "owners" (i.e., a User, a Company, etc.), I can think of two table structures to accomplish this:



Solution 1 (1 table for all owners):



ProductAssignment
---------
ProductId,
OwnerId (constraint to ensure either UserId, CompanyId, etc.)


Solution 2 (1 table for each owner):



UserProduct
---------
UserId,
ProductId

CompanyProduct
---------
CompanyId,
ProductId


Solution 2 offers more of a "proper" model, but we have a lot of these scenarios and it's going to result in our database being littered with many dozens of these types of tables. It's also a pain to code for since I would need to create a new, redundant query each time a new owner table is added. I feel the maintenance and coding would be much easier for Solution 1, but maybe I'm wrong?



For what it's worth, most of the data access will be through LINQ to SQL (ASP.NET Web Forms project), and Entity Framework (ASP.NET MVC project). Very few sprocs and such.




  • What pros and cons am I missing for each solution?

  • Will bad things happen to me if I go with Solution 1? i.e., guys in black suits and sun glasses showing up at the office to make me "disappear?"

  • Is there a Solution 3?










share|improve this question
















This is 101 level stuff and I promise to read a book later, but in the meantime, given the following three tables:



Product
---------
ProductId (uniqueidentifier)


User
---------
UserId (uniqueidentifier)


Company
---------
CompanyId (uniqueidentifier)


If a Product can be assigned to different "owners" (i.e., a User, a Company, etc.), I can think of two table structures to accomplish this:



Solution 1 (1 table for all owners):



ProductAssignment
---------
ProductId,
OwnerId (constraint to ensure either UserId, CompanyId, etc.)


Solution 2 (1 table for each owner):



UserProduct
---------
UserId,
ProductId

CompanyProduct
---------
CompanyId,
ProductId


Solution 2 offers more of a "proper" model, but we have a lot of these scenarios and it's going to result in our database being littered with many dozens of these types of tables. It's also a pain to code for since I would need to create a new, redundant query each time a new owner table is added. I feel the maintenance and coding would be much easier for Solution 1, but maybe I'm wrong?



For what it's worth, most of the data access will be through LINQ to SQL (ASP.NET Web Forms project), and Entity Framework (ASP.NET MVC project). Very few sprocs and such.




  • What pros and cons am I missing for each solution?

  • Will bad things happen to me if I go with Solution 1? i.e., guys in black suits and sun glasses showing up at the office to make me "disappear?"

  • Is there a Solution 3?







sql-server-2012 database-design architecture






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 20 '15 at 12:56







StronglyTyped

















asked May 19 '15 at 13:57









StronglyTypedStronglyTyped

14016




14016





bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 10 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

    – ypercubeᵀᴹ
    May 19 '15 at 21:06













  • Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

    – StronglyTyped
    May 20 '15 at 12:58



















  • Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

    – ypercubeᵀᴹ
    May 19 '15 at 21:06













  • Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

    – StronglyTyped
    May 20 '15 at 12:58

















Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

– ypercubeᵀᴹ
May 19 '15 at 21:06







Can a product have 2 owners (a user and a company)? The 2nd method does not forbid it.

– ypercubeᵀᴹ
May 19 '15 at 21:06















Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

– StronglyTyped
May 20 '15 at 12:58





Yes, a product can have 2 owners. In solution 1, there would be a ProductAssignmentId PK.

– StronglyTyped
May 20 '15 at 12:58










1 Answer
1






active

oldest

votes


















0














I'm not entirely sure if I'm right, but as far as I know solution 1 will not work since you can't add two foreign keys in the same column and even if it is possible the values will probably overlap, such as existing a UserId = 1 and a CompanyId = 1 causing you not to know which of them is the true one, furthermore thinking of a big solution you may even compromise the performance of a query that wishes only company owned or user owned. Solution 2 seems much more robust and a efficient design to me.






share|improve this answer





















  • 1





    "performatic" is not a word (as far as I know.) I think you mean "efficient".

    – ypercubeᵀᴹ
    May 19 '15 at 21:09











  • Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

    – StronglyTyped
    May 20 '15 at 12:54











  • sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

    – Enthusiast
    May 20 '15 at 16:12













  • I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

    – Queue Mann
    May 20 '15 at 16:21











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%2f101902%2foptions-for-structuring-multiple-associations-to-a-record%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














I'm not entirely sure if I'm right, but as far as I know solution 1 will not work since you can't add two foreign keys in the same column and even if it is possible the values will probably overlap, such as existing a UserId = 1 and a CompanyId = 1 causing you not to know which of them is the true one, furthermore thinking of a big solution you may even compromise the performance of a query that wishes only company owned or user owned. Solution 2 seems much more robust and a efficient design to me.






share|improve this answer





















  • 1





    "performatic" is not a word (as far as I know.) I think you mean "efficient".

    – ypercubeᵀᴹ
    May 19 '15 at 21:09











  • Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

    – StronglyTyped
    May 20 '15 at 12:54











  • sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

    – Enthusiast
    May 20 '15 at 16:12













  • I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

    – Queue Mann
    May 20 '15 at 16:21
















0














I'm not entirely sure if I'm right, but as far as I know solution 1 will not work since you can't add two foreign keys in the same column and even if it is possible the values will probably overlap, such as existing a UserId = 1 and a CompanyId = 1 causing you not to know which of them is the true one, furthermore thinking of a big solution you may even compromise the performance of a query that wishes only company owned or user owned. Solution 2 seems much more robust and a efficient design to me.






share|improve this answer





















  • 1





    "performatic" is not a word (as far as I know.) I think you mean "efficient".

    – ypercubeᵀᴹ
    May 19 '15 at 21:09











  • Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

    – StronglyTyped
    May 20 '15 at 12:54











  • sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

    – Enthusiast
    May 20 '15 at 16:12













  • I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

    – Queue Mann
    May 20 '15 at 16:21














0












0








0







I'm not entirely sure if I'm right, but as far as I know solution 1 will not work since you can't add two foreign keys in the same column and even if it is possible the values will probably overlap, such as existing a UserId = 1 and a CompanyId = 1 causing you not to know which of them is the true one, furthermore thinking of a big solution you may even compromise the performance of a query that wishes only company owned or user owned. Solution 2 seems much more robust and a efficient design to me.






share|improve this answer















I'm not entirely sure if I'm right, but as far as I know solution 1 will not work since you can't add two foreign keys in the same column and even if it is possible the values will probably overlap, such as existing a UserId = 1 and a CompanyId = 1 causing you not to know which of them is the true one, furthermore thinking of a big solution you may even compromise the performance of a query that wishes only company owned or user owned. Solution 2 seems much more robust and a efficient design to me.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 20 '15 at 16:17

























answered May 19 '15 at 20:58









EnthusiastEnthusiast

80111




80111








  • 1





    "performatic" is not a word (as far as I know.) I think you mean "efficient".

    – ypercubeᵀᴹ
    May 19 '15 at 21:09











  • Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

    – StronglyTyped
    May 20 '15 at 12:54











  • sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

    – Enthusiast
    May 20 '15 at 16:12













  • I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

    – Queue Mann
    May 20 '15 at 16:21














  • 1





    "performatic" is not a word (as far as I know.) I think you mean "efficient".

    – ypercubeᵀᴹ
    May 19 '15 at 21:09











  • Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

    – StronglyTyped
    May 20 '15 at 12:54











  • sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

    – Enthusiast
    May 20 '15 at 16:12













  • I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

    – Queue Mann
    May 20 '15 at 16:21








1




1





"performatic" is not a word (as far as I know.) I think you mean "efficient".

– ypercubeᵀᴹ
May 19 '15 at 21:09





"performatic" is not a word (as far as I know.) I think you mean "efficient".

– ypercubeᵀᴹ
May 19 '15 at 21:09













Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

– StronglyTyped
May 20 '15 at 12:54





Very sorry, I forgot to mention that the IDs will be uniqueidentifiers (guids). I'll update the OP to reflect this.

– StronglyTyped
May 20 '15 at 12:54













sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

– Enthusiast
May 20 '15 at 16:12







sorry, english is not my first language and in my language there is the equivalent word for performatic. i'll correct it

– Enthusiast
May 20 '15 at 16:12















I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

– Queue Mann
May 20 '15 at 16:21





I'd be very careful about having your IDs as GUIDs if you plan to have them as your clustering primary key. All your NC indexes will implicitly contain that 16 byte key and your index space will blow up.

– Queue Mann
May 20 '15 at 16:21


















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%2f101902%2foptions-for-structuring-multiple-associations-to-a-record%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

SQL Server 17 - Attemping to backup to remote NAS but Access is denied

Always On Availability groups resolving state after failover - Remote harden of transaction...

Restoring from pg_dump with foreign key constraints