Is a nullable foreign key workable?How can foreign key constraints be temporarily disabled using T-SQL?Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?How can I list all foreign keys referencing a given table in SQL Server?Does a foreign key automatically create an index?Can I change foreign key IDs when deleting the FK row?SQL - Null foreign key or junction table?Auto increment primary key in SQL Server Management Studio 2012SQL Aggregate function issues using DISTINCT functionSQL query to return data from multiple tables?With SqlServer, where * not exists
What exactly is a "murder hobo"?
The Apéry's constant and the Airy function
Gory anime with pink haired girl escaping an asylum
What does "spinning upon the shoals" mean?
Very Simple Spell (Number to Words) 001 to 999 in VBA
Pages: Fit two A5 page on on a single A4 page in landscape mode
Deck of Cards with Shuffle and Sort functionality
Can one block with a protection from color creature?
Why is a semiprime used as the modulus in RSA?
How to evaluate the performance of open source solver?
Why am I getting unevenly-spread results when using $RANDOM?
Writing an ace/aro character?
What are the effects of abstaining from eating a certain flavor?
Other Space Shuttle O-ring failures
SQL Server Sch-S locks on unrelated tables
My professor has told me he will be the corresponding author. Will it hurt my future career?
Tikz, arrow formatting
Matrices with shadows
Is this really the Saturn V computer only, or are there other systems here as well?
Why did Old English lose both thorn and eth?
Wires do not connect in Circuitikz
Is it ok for parents to kiss and romance with each other while their 2- to 8-year-old child watches?
Why do people prefer metropolitan areas, considering monsters and villains?
Was it ever illegal to name a pig "Napoleon" in France?
Is a nullable foreign key workable?
How can foreign key constraints be temporarily disabled using T-SQL?Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?How can I list all foreign keys referencing a given table in SQL Server?Does a foreign key automatically create an index?Can I change foreign key IDs when deleting the FK row?SQL - Null foreign key or junction table?Auto increment primary key in SQL Server Management Studio 2012SQL Aggregate function issues using DISTINCT functionSQL query to return data from multiple tables?With SqlServer, where * not exists
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Consider the following tables for creating a simple order:
Customer(CustomerId, Name)
Order(OrderId, CustomerId)
OrderLine(OrderLineId, OrderId, CatalogItemId)
The above tables are typical. However, the following is not typical because it includes a CustomerId in it:
CatalogItem(CatalogItemId, Description, CustomerId)
In other words, each catalog item is for one client only. However, what is actually needed is that a catalog item is either assigned to a specific customer, or ANY customer.
What is the best way to model that? (It may be as simple as allowing CustomerId to be null, but I am uncertain and that is not the way it is being done currently.)
This would then be used when showing the list of CatalogItems available for a customer:
select * from CatalogItems ci where (ci.customerId is null or ci.Customerid = @customerid);
The current system uses 0 for the CustomerId to flag that it is available to all customers, but that seems non-standard to me.
add a comment |
Consider the following tables for creating a simple order:
Customer(CustomerId, Name)
Order(OrderId, CustomerId)
OrderLine(OrderLineId, OrderId, CatalogItemId)
The above tables are typical. However, the following is not typical because it includes a CustomerId in it:
CatalogItem(CatalogItemId, Description, CustomerId)
In other words, each catalog item is for one client only. However, what is actually needed is that a catalog item is either assigned to a specific customer, or ANY customer.
What is the best way to model that? (It may be as simple as allowing CustomerId to be null, but I am uncertain and that is not the way it is being done currently.)
This would then be used when showing the list of CatalogItems available for a customer:
select * from CatalogItems ci where (ci.customerId is null or ci.Customerid = @customerid);
The current system uses 0 for the CustomerId to flag that it is available to all customers, but that seems non-standard to me.
1
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22
add a comment |
Consider the following tables for creating a simple order:
Customer(CustomerId, Name)
Order(OrderId, CustomerId)
OrderLine(OrderLineId, OrderId, CatalogItemId)
The above tables are typical. However, the following is not typical because it includes a CustomerId in it:
CatalogItem(CatalogItemId, Description, CustomerId)
In other words, each catalog item is for one client only. However, what is actually needed is that a catalog item is either assigned to a specific customer, or ANY customer.
What is the best way to model that? (It may be as simple as allowing CustomerId to be null, but I am uncertain and that is not the way it is being done currently.)
This would then be used when showing the list of CatalogItems available for a customer:
select * from CatalogItems ci where (ci.customerId is null or ci.Customerid = @customerid);
The current system uses 0 for the CustomerId to flag that it is available to all customers, but that seems non-standard to me.
Consider the following tables for creating a simple order:
Customer(CustomerId, Name)
Order(OrderId, CustomerId)
OrderLine(OrderLineId, OrderId, CatalogItemId)
The above tables are typical. However, the following is not typical because it includes a CustomerId in it:
CatalogItem(CatalogItemId, Description, CustomerId)
In other words, each catalog item is for one client only. However, what is actually needed is that a catalog item is either assigned to a specific customer, or ANY customer.
What is the best way to model that? (It may be as simple as allowing CustomerId to be null, but I am uncertain and that is not the way it is being done currently.)
This would then be used when showing the list of CatalogItems available for a customer:
select * from CatalogItems ci where (ci.customerId is null or ci.Customerid = @customerid);
The current system uses 0 for the CustomerId to flag that it is available to all customers, but that seems non-standard to me.
asked Mar 25 at 22:16
Greg GumGreg Gum
12.6k19 gold badges92 silver badges144 bronze badges
12.6k19 gold badges92 silver badges144 bronze badges
1
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22
add a comment |
1
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22
1
1
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22
add a comment |
1 Answer
1
active
oldest
votes
I believe these designs are fine. The important thing is that once you choose a design to stick with it. In the database I work out of daily it employs many different designs. Sometimes there are zeros, nulls, or 1s or -1s to represent that an object is for ALL entities or for no entities. Perhaps there is another meaning that is inferred that simply depends on how the database table was originally implemented and whether or not the data being populated into that table adheres to the original design.
So I say that 'Yes' - it's certainly ok.
The design you mentioned in your question is how my application determines a clients defaults. This is used in many different sections of my application.
For example - To show the system's default 'Catalog Items' available to all clients:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null;
To locate custom 'Catalog Items' for a specific client:
SELECT * FROM CatalogItems ci WHERE ci.customerId = @customerid;
And as you pointed out if you want to find ALL available 'Catalog Items' for a client:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null or ci.Customerid = @customerid;
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%2f55347204%2fis-a-nullable-foreign-key-workable%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
I believe these designs are fine. The important thing is that once you choose a design to stick with it. In the database I work out of daily it employs many different designs. Sometimes there are zeros, nulls, or 1s or -1s to represent that an object is for ALL entities or for no entities. Perhaps there is another meaning that is inferred that simply depends on how the database table was originally implemented and whether or not the data being populated into that table adheres to the original design.
So I say that 'Yes' - it's certainly ok.
The design you mentioned in your question is how my application determines a clients defaults. This is used in many different sections of my application.
For example - To show the system's default 'Catalog Items' available to all clients:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null;
To locate custom 'Catalog Items' for a specific client:
SELECT * FROM CatalogItems ci WHERE ci.customerId = @customerid;
And as you pointed out if you want to find ALL available 'Catalog Items' for a client:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null or ci.Customerid = @customerid;
add a comment |
I believe these designs are fine. The important thing is that once you choose a design to stick with it. In the database I work out of daily it employs many different designs. Sometimes there are zeros, nulls, or 1s or -1s to represent that an object is for ALL entities or for no entities. Perhaps there is another meaning that is inferred that simply depends on how the database table was originally implemented and whether or not the data being populated into that table adheres to the original design.
So I say that 'Yes' - it's certainly ok.
The design you mentioned in your question is how my application determines a clients defaults. This is used in many different sections of my application.
For example - To show the system's default 'Catalog Items' available to all clients:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null;
To locate custom 'Catalog Items' for a specific client:
SELECT * FROM CatalogItems ci WHERE ci.customerId = @customerid;
And as you pointed out if you want to find ALL available 'Catalog Items' for a client:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null or ci.Customerid = @customerid;
add a comment |
I believe these designs are fine. The important thing is that once you choose a design to stick with it. In the database I work out of daily it employs many different designs. Sometimes there are zeros, nulls, or 1s or -1s to represent that an object is for ALL entities or for no entities. Perhaps there is another meaning that is inferred that simply depends on how the database table was originally implemented and whether or not the data being populated into that table adheres to the original design.
So I say that 'Yes' - it's certainly ok.
The design you mentioned in your question is how my application determines a clients defaults. This is used in many different sections of my application.
For example - To show the system's default 'Catalog Items' available to all clients:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null;
To locate custom 'Catalog Items' for a specific client:
SELECT * FROM CatalogItems ci WHERE ci.customerId = @customerid;
And as you pointed out if you want to find ALL available 'Catalog Items' for a client:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null or ci.Customerid = @customerid;
I believe these designs are fine. The important thing is that once you choose a design to stick with it. In the database I work out of daily it employs many different designs. Sometimes there are zeros, nulls, or 1s or -1s to represent that an object is for ALL entities or for no entities. Perhaps there is another meaning that is inferred that simply depends on how the database table was originally implemented and whether or not the data being populated into that table adheres to the original design.
So I say that 'Yes' - it's certainly ok.
The design you mentioned in your question is how my application determines a clients defaults. This is used in many different sections of my application.
For example - To show the system's default 'Catalog Items' available to all clients:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null;
To locate custom 'Catalog Items' for a specific client:
SELECT * FROM CatalogItems ci WHERE ci.customerId = @customerid;
And as you pointed out if you want to find ALL available 'Catalog Items' for a client:
SELECT * FROM CatalogItems ci WHERE ci.customerId is null or ci.Customerid = @customerid;
edited Mar 25 at 22:33
answered Mar 25 at 22:24
Code NoviceCode Novice
7174 silver badges20 bronze badges
7174 silver badges20 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55347204%2fis-a-nullable-foreign-key-workable%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
1
Its down to personal preference, I would use null to be super clear as opposed to 0. But whatever works.
– Dale Burrell
Mar 25 at 22:22