The use of multiple foreign keys on same column in SQL Serverforeign key constraints on same tableChanging field length when foreign keys reference primary key field in tableNeed for indexes on foreign keysComposite primary key from multiple tables / multiple foreign keysForeign Keys to tables where primary key is not clustered indexWhat would I use a MATCH SIMPLE foreign key for?Multiple foreign keys on single columnSQL Server database design with foreign keysComposite Primary Key on partitioned tables, and Foreign KeysIs it best practice to use surrogate keys when creating foreign key constraints in SQL Server?
Smoothness of finite-dimensional functional calculus
can i play a electric guitar through a bass amp?
Is it important to consider tone, melody, and musical form while writing a song?
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
Do I have a twin with permutated remainders?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
LaTeX closing $ signs makes cursor jump
Theorems that impeded progress
Did Shadowfax go to Valinor?
How does one intimidate enemies without having the capacity for violence?
"to be prejudice towards/against someone" vs "to be prejudiced against/towards someone"
Test if tikzmark exists on same page
Email Account under attack (really) - anything I can do?
Arthur Somervell: 1000 Exercises - Meaning of this notation
Minkowski space
How to format long polynomial?
If I cast Expeditious Retreat, can I Dash as a bonus action on the same turn?
Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.
Fencing style for blades that can attack from a distance
Writing rule stating superpower from different root cause is bad writing
Test whether all array elements are factors of a number
How to say job offer in Mandarin/Cantonese?
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
The use of multiple foreign keys on same column in SQL Server
foreign key constraints on same tableChanging field length when foreign keys reference primary key field in tableNeed for indexes on foreign keysComposite primary key from multiple tables / multiple foreign keysForeign Keys to tables where primary key is not clustered indexWhat would I use a MATCH SIMPLE foreign key for?Multiple foreign keys on single columnSQL Server database design with foreign keysComposite Primary Key on partitioned tables, and Foreign KeysIs it best practice to use surrogate keys when creating foreign key constraints in SQL Server?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
SQL Server allows me to create multiple foreign keys on a column, and each time using just different name I can create another key referencing to the same object. Basically all the keys are defining the same relationship. I want to know what's the use of having multiple foreign keys which are defined on the same column and reference to the same column in another table. What's the benefit of it that SQL Server allows us to do a thing like that?
sql-server foreign-key
add a comment |
SQL Server allows me to create multiple foreign keys on a column, and each time using just different name I can create another key referencing to the same object. Basically all the keys are defining the same relationship. I want to know what's the use of having multiple foreign keys which are defined on the same column and reference to the same column in another table. What's the benefit of it that SQL Server allows us to do a thing like that?
sql-server foreign-key
add a comment |
SQL Server allows me to create multiple foreign keys on a column, and each time using just different name I can create another key referencing to the same object. Basically all the keys are defining the same relationship. I want to know what's the use of having multiple foreign keys which are defined on the same column and reference to the same column in another table. What's the benefit of it that SQL Server allows us to do a thing like that?
sql-server foreign-key
SQL Server allows me to create multiple foreign keys on a column, and each time using just different name I can create another key referencing to the same object. Basically all the keys are defining the same relationship. I want to know what's the use of having multiple foreign keys which are defined on the same column and reference to the same column in another table. What's the benefit of it that SQL Server allows us to do a thing like that?
sql-server foreign-key
sql-server foreign-key
asked 14 hours ago
ElGrigElGrig
71916
71916
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
There is no use for having identical foreign key constraints., that is on same columns and referencing same table and columns.
It's like having the same check 2 or more times.
add a comment |
There is no benefit to having redundant constraints that differ only by name. Similarly, there is no benefit to having redundant indexes that differ only by name. Both add overhead without value.
The SQL Server database engine does not stop you from doing so.
add a comment |
SQL Server allows you to do a lot of silly things.
You can even create a foreign key on a column referencing itself - despite the fact that this can never be violated as every row will meet the constraint on itself.
One edge case where the ability to create two foreign keys on the same relationship would be potentially useful is because the index used for validating foreign keys is determined at creation time. If a better (i.e. narrower) index comes along later then this would allow a new foreign key constraint to be created bound on the better index and then the original constraint dropped without having any gap with no active constraint.
(As in example below)
CREATE TABLE T1(
T1_Id INT PRIMARY KEY CLUSTERED NOT NULL,
Filler CHAR(4000) NULL,
)
INSERT INTO T1 VALUES (1, '');
CREATE TABLE T2(
T2_Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
T1_Id INT NOT NULL CONSTRAINT FK REFERENCES T1 (T1_Id),
Filler CHAR(4000) NULL,
)
ALTER TABLE T1 ADD CONSTRAINT
UQ_T1 UNIQUE NONCLUSTERED(T1_Id)
/*Execution Plan uses clustered index*/
INSERT INTO T2 VALUES (1,1)
ALTER TABLE T2 WITH CHECK ADD CONSTRAINT FK2 FOREIGN KEY(T1_Id)
REFERENCES T1 (T1_Id)
ALTER TABLE T2 DROP CONSTRAINT FK
/*Now Execution Plan now uses non clustered index*/
INSERT INTO T2 VALUES (1,1)
DROP TABLE T2, T1;
As an aside for the interim period whilst both constraints exist any inserts end up being validated against both indexes.
add a comment |
Same reason you can create 50 indexes on the same column, add a second log file, set max server memory to 20MB... most people won't do these things, but there can be legitimate reasons to do them occasionally, so there's no benefit in creating overhead in the engine to add checks against things that are merely ill-advised.
add a comment |
Sounds like a blue-green thing.
When you begin to cutover from blue to green, you need to temporarily create extra copies of things.
What we want to do is temporarily create an extra foreign key CHECK WITH NOCHECK
and ON UPDATE CASCADE ON DELETE SET NULL
; what this does is it is a working foreign key but the existing rows aren't checked when the key is created.
Later after cleaning up all the rows that should match we would create the new foreign key without any command options (default is CHECK WITH CHECK
which is what you typically want), and drop the temporary foreign key.
Notice that if you just dropped and recreated the foreign key, some garbage rows could slip by you.
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%2f234086%2fthe-use-of-multiple-foreign-keys-on-same-column-in-sql-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no use for having identical foreign key constraints., that is on same columns and referencing same table and columns.
It's like having the same check 2 or more times.
add a comment |
There is no use for having identical foreign key constraints., that is on same columns and referencing same table and columns.
It's like having the same check 2 or more times.
add a comment |
There is no use for having identical foreign key constraints., that is on same columns and referencing same table and columns.
It's like having the same check 2 or more times.
There is no use for having identical foreign key constraints., that is on same columns and referencing same table and columns.
It's like having the same check 2 or more times.
edited 14 hours ago
answered 14 hours ago
ypercubeᵀᴹypercubeᵀᴹ
78.1k11136219
78.1k11136219
add a comment |
add a comment |
There is no benefit to having redundant constraints that differ only by name. Similarly, there is no benefit to having redundant indexes that differ only by name. Both add overhead without value.
The SQL Server database engine does not stop you from doing so.
add a comment |
There is no benefit to having redundant constraints that differ only by name. Similarly, there is no benefit to having redundant indexes that differ only by name. Both add overhead without value.
The SQL Server database engine does not stop you from doing so.
add a comment |
There is no benefit to having redundant constraints that differ only by name. Similarly, there is no benefit to having redundant indexes that differ only by name. Both add overhead without value.
The SQL Server database engine does not stop you from doing so.
There is no benefit to having redundant constraints that differ only by name. Similarly, there is no benefit to having redundant indexes that differ only by name. Both add overhead without value.
The SQL Server database engine does not stop you from doing so.
answered 14 hours ago
Dan GuzmanDan Guzman
14.1k21736
14.1k21736
add a comment |
add a comment |
SQL Server allows you to do a lot of silly things.
You can even create a foreign key on a column referencing itself - despite the fact that this can never be violated as every row will meet the constraint on itself.
One edge case where the ability to create two foreign keys on the same relationship would be potentially useful is because the index used for validating foreign keys is determined at creation time. If a better (i.e. narrower) index comes along later then this would allow a new foreign key constraint to be created bound on the better index and then the original constraint dropped without having any gap with no active constraint.
(As in example below)
CREATE TABLE T1(
T1_Id INT PRIMARY KEY CLUSTERED NOT NULL,
Filler CHAR(4000) NULL,
)
INSERT INTO T1 VALUES (1, '');
CREATE TABLE T2(
T2_Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
T1_Id INT NOT NULL CONSTRAINT FK REFERENCES T1 (T1_Id),
Filler CHAR(4000) NULL,
)
ALTER TABLE T1 ADD CONSTRAINT
UQ_T1 UNIQUE NONCLUSTERED(T1_Id)
/*Execution Plan uses clustered index*/
INSERT INTO T2 VALUES (1,1)
ALTER TABLE T2 WITH CHECK ADD CONSTRAINT FK2 FOREIGN KEY(T1_Id)
REFERENCES T1 (T1_Id)
ALTER TABLE T2 DROP CONSTRAINT FK
/*Now Execution Plan now uses non clustered index*/
INSERT INTO T2 VALUES (1,1)
DROP TABLE T2, T1;
As an aside for the interim period whilst both constraints exist any inserts end up being validated against both indexes.
add a comment |
SQL Server allows you to do a lot of silly things.
You can even create a foreign key on a column referencing itself - despite the fact that this can never be violated as every row will meet the constraint on itself.
One edge case where the ability to create two foreign keys on the same relationship would be potentially useful is because the index used for validating foreign keys is determined at creation time. If a better (i.e. narrower) index comes along later then this would allow a new foreign key constraint to be created bound on the better index and then the original constraint dropped without having any gap with no active constraint.
(As in example below)
CREATE TABLE T1(
T1_Id INT PRIMARY KEY CLUSTERED NOT NULL,
Filler CHAR(4000) NULL,
)
INSERT INTO T1 VALUES (1, '');
CREATE TABLE T2(
T2_Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
T1_Id INT NOT NULL CONSTRAINT FK REFERENCES T1 (T1_Id),
Filler CHAR(4000) NULL,
)
ALTER TABLE T1 ADD CONSTRAINT
UQ_T1 UNIQUE NONCLUSTERED(T1_Id)
/*Execution Plan uses clustered index*/
INSERT INTO T2 VALUES (1,1)
ALTER TABLE T2 WITH CHECK ADD CONSTRAINT FK2 FOREIGN KEY(T1_Id)
REFERENCES T1 (T1_Id)
ALTER TABLE T2 DROP CONSTRAINT FK
/*Now Execution Plan now uses non clustered index*/
INSERT INTO T2 VALUES (1,1)
DROP TABLE T2, T1;
As an aside for the interim period whilst both constraints exist any inserts end up being validated against both indexes.
add a comment |
SQL Server allows you to do a lot of silly things.
You can even create a foreign key on a column referencing itself - despite the fact that this can never be violated as every row will meet the constraint on itself.
One edge case where the ability to create two foreign keys on the same relationship would be potentially useful is because the index used for validating foreign keys is determined at creation time. If a better (i.e. narrower) index comes along later then this would allow a new foreign key constraint to be created bound on the better index and then the original constraint dropped without having any gap with no active constraint.
(As in example below)
CREATE TABLE T1(
T1_Id INT PRIMARY KEY CLUSTERED NOT NULL,
Filler CHAR(4000) NULL,
)
INSERT INTO T1 VALUES (1, '');
CREATE TABLE T2(
T2_Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
T1_Id INT NOT NULL CONSTRAINT FK REFERENCES T1 (T1_Id),
Filler CHAR(4000) NULL,
)
ALTER TABLE T1 ADD CONSTRAINT
UQ_T1 UNIQUE NONCLUSTERED(T1_Id)
/*Execution Plan uses clustered index*/
INSERT INTO T2 VALUES (1,1)
ALTER TABLE T2 WITH CHECK ADD CONSTRAINT FK2 FOREIGN KEY(T1_Id)
REFERENCES T1 (T1_Id)
ALTER TABLE T2 DROP CONSTRAINT FK
/*Now Execution Plan now uses non clustered index*/
INSERT INTO T2 VALUES (1,1)
DROP TABLE T2, T1;
As an aside for the interim period whilst both constraints exist any inserts end up being validated against both indexes.
SQL Server allows you to do a lot of silly things.
You can even create a foreign key on a column referencing itself - despite the fact that this can never be violated as every row will meet the constraint on itself.
One edge case where the ability to create two foreign keys on the same relationship would be potentially useful is because the index used for validating foreign keys is determined at creation time. If a better (i.e. narrower) index comes along later then this would allow a new foreign key constraint to be created bound on the better index and then the original constraint dropped without having any gap with no active constraint.
(As in example below)
CREATE TABLE T1(
T1_Id INT PRIMARY KEY CLUSTERED NOT NULL,
Filler CHAR(4000) NULL,
)
INSERT INTO T1 VALUES (1, '');
CREATE TABLE T2(
T2_Id INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
T1_Id INT NOT NULL CONSTRAINT FK REFERENCES T1 (T1_Id),
Filler CHAR(4000) NULL,
)
ALTER TABLE T1 ADD CONSTRAINT
UQ_T1 UNIQUE NONCLUSTERED(T1_Id)
/*Execution Plan uses clustered index*/
INSERT INTO T2 VALUES (1,1)
ALTER TABLE T2 WITH CHECK ADD CONSTRAINT FK2 FOREIGN KEY(T1_Id)
REFERENCES T1 (T1_Id)
ALTER TABLE T2 DROP CONSTRAINT FK
/*Now Execution Plan now uses non clustered index*/
INSERT INTO T2 VALUES (1,1)
DROP TABLE T2, T1;
As an aside for the interim period whilst both constraints exist any inserts end up being validated against both indexes.
edited 9 hours ago
answered 9 hours ago
Martin SmithMartin Smith
64.1k10173258
64.1k10173258
add a comment |
add a comment |
Same reason you can create 50 indexes on the same column, add a second log file, set max server memory to 20MB... most people won't do these things, but there can be legitimate reasons to do them occasionally, so there's no benefit in creating overhead in the engine to add checks against things that are merely ill-advised.
add a comment |
Same reason you can create 50 indexes on the same column, add a second log file, set max server memory to 20MB... most people won't do these things, but there can be legitimate reasons to do them occasionally, so there's no benefit in creating overhead in the engine to add checks against things that are merely ill-advised.
add a comment |
Same reason you can create 50 indexes on the same column, add a second log file, set max server memory to 20MB... most people won't do these things, but there can be legitimate reasons to do them occasionally, so there's no benefit in creating overhead in the engine to add checks against things that are merely ill-advised.
Same reason you can create 50 indexes on the same column, add a second log file, set max server memory to 20MB... most people won't do these things, but there can be legitimate reasons to do them occasionally, so there's no benefit in creating overhead in the engine to add checks against things that are merely ill-advised.
answered 12 hours ago
Aaron Bertrand♦Aaron Bertrand
154k18298493
154k18298493
add a comment |
add a comment |
Sounds like a blue-green thing.
When you begin to cutover from blue to green, you need to temporarily create extra copies of things.
What we want to do is temporarily create an extra foreign key CHECK WITH NOCHECK
and ON UPDATE CASCADE ON DELETE SET NULL
; what this does is it is a working foreign key but the existing rows aren't checked when the key is created.
Later after cleaning up all the rows that should match we would create the new foreign key without any command options (default is CHECK WITH CHECK
which is what you typically want), and drop the temporary foreign key.
Notice that if you just dropped and recreated the foreign key, some garbage rows could slip by you.
add a comment |
Sounds like a blue-green thing.
When you begin to cutover from blue to green, you need to temporarily create extra copies of things.
What we want to do is temporarily create an extra foreign key CHECK WITH NOCHECK
and ON UPDATE CASCADE ON DELETE SET NULL
; what this does is it is a working foreign key but the existing rows aren't checked when the key is created.
Later after cleaning up all the rows that should match we would create the new foreign key without any command options (default is CHECK WITH CHECK
which is what you typically want), and drop the temporary foreign key.
Notice that if you just dropped and recreated the foreign key, some garbage rows could slip by you.
add a comment |
Sounds like a blue-green thing.
When you begin to cutover from blue to green, you need to temporarily create extra copies of things.
What we want to do is temporarily create an extra foreign key CHECK WITH NOCHECK
and ON UPDATE CASCADE ON DELETE SET NULL
; what this does is it is a working foreign key but the existing rows aren't checked when the key is created.
Later after cleaning up all the rows that should match we would create the new foreign key without any command options (default is CHECK WITH CHECK
which is what you typically want), and drop the temporary foreign key.
Notice that if you just dropped and recreated the foreign key, some garbage rows could slip by you.
Sounds like a blue-green thing.
When you begin to cutover from blue to green, you need to temporarily create extra copies of things.
What we want to do is temporarily create an extra foreign key CHECK WITH NOCHECK
and ON UPDATE CASCADE ON DELETE SET NULL
; what this does is it is a working foreign key but the existing rows aren't checked when the key is created.
Later after cleaning up all the rows that should match we would create the new foreign key without any command options (default is CHECK WITH CHECK
which is what you typically want), and drop the temporary foreign key.
Notice that if you just dropped and recreated the foreign key, some garbage rows could slip by you.
answered 34 mins ago
JoshuaJoshua
1336
1336
add a comment |
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%2f234086%2fthe-use-of-multiple-foreign-keys-on-same-column-in-sql-server%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