How to avoid adding duplicate foreign key constraintsHow can I prevent SQL injection in PHP?How can foreign key constraints be temporarily disabled using T-SQL?Foreign key constraint may cause cycles or multiple cascade paths?Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsHow do I UPDATE from a SELECT in SQL Server?Can table columns with a Foreign Key be NULL?MySQL foreign key constraints, cascade deleteWhat are the options for storing hierarchical data in a relational database?Foreign key constraints: When to use ON UPDATE and ON DELETEAdd Foreign Key to existing table
Arthur Somervell: 1000 Exercises - Meaning of this notation
tikz: show 0 at the axis origin
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
Why do falling prices hurt debtors?
Theorems that impeded progress
The use of multiple foreign keys on same column in SQL Server
Is this a crack on the carbon frame?
Why, historically, did Gödel think CH was false?
What's the point of deactivating Num Lock on login screens?
How does one intimidate enemies without having the capacity for violence?
Has the BBC provided arguments for saying Brexit being cancelled is unlikely?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Do VLANs within a subnet need to have their own subnet for router on a stick?
How is it possible to have an ability score that is less than 3?
Fully-Firstable Anagram Sets
Can I make popcorn with any corn?
LaTeX closing $ signs makes cursor jump
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?
Can divisibility rules for digits be generalized to sum of digits
strToHex ( string to its hex representation as string)
Is it possible to do 50 km distance without any previous training?
Mathematical cryptic clues
Adding span tags within wp_list_pages list items
How to find program name(s) of an installed package?
How to avoid adding duplicate foreign key constraints
How can I prevent SQL injection in PHP?How can foreign key constraints be temporarily disabled using T-SQL?Foreign key constraint may cause cycles or multiple cascade paths?Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsHow do I UPDATE from a SELECT in SQL Server?Can table columns with a Foreign Key be NULL?MySQL foreign key constraints, cascade deleteWhat are the options for storing hierarchical data in a relational database?Foreign key constraints: When to use ON UPDATE and ON DELETEAdd Foreign Key to existing table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I would to know if it is possible to avoid adding several times the same foreign key constraint?
Example: if I execute 3 times the query below, the constraint will exist with 3 times in phpmyadmin... It would be great that it would be rejected the second and third time I apply that query.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY (`items_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
mysql sql foreign-keys
add a comment |
I would to know if it is possible to avoid adding several times the same foreign key constraint?
Example: if I execute 3 times the query below, the constraint will exist with 3 times in phpmyadmin... It would be great that it would be rejected the second and third time I apply that query.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY (`items_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
mysql sql foreign-keys
add a comment |
I would to know if it is possible to avoid adding several times the same foreign key constraint?
Example: if I execute 3 times the query below, the constraint will exist with 3 times in phpmyadmin... It would be great that it would be rejected the second and third time I apply that query.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY (`items_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
mysql sql foreign-keys
I would to know if it is possible to avoid adding several times the same foreign key constraint?
Example: if I execute 3 times the query below, the constraint will exist with 3 times in phpmyadmin... It would be great that it would be rejected the second and third time I apply that query.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY (`items_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
mysql sql foreign-keys
mysql sql foreign-keys
edited Mar 8 at 4:23
GMB
21.5k51028
21.5k51028
asked Mar 8 at 4:00
PierrePierre
438520
438520
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
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%2f55056524%2fhow-to-avoid-adding-duplicate-foreign-key-constraints%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
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
add a comment |
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
add a comment |
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
answered Mar 8 at 4:22
GMBGMB
21.5k51028
21.5k51028
add a comment |
add a comment |
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%2f55056524%2fhow-to-avoid-adding-duplicate-foreign-key-constraints%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