{"id":172,"date":"2013-07-17T10:19:47","date_gmt":"2013-07-17T10:19:47","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=172"},"modified":"2013-07-17T10:22:12","modified_gmt":"2013-07-17T10:22:12","slug":"how-to-add-foreign-key-to-mysql-table","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-add-foreign-key-to-mysql-table\/","title":{"rendered":"How to add Foreign key to MySQL Table"},"content":{"rendered":"<p>If you already created a Table but you havent added foreign key constraint then you can do that by<br \/>\nrunning the following statement in MySQL <\/p>\n<p><strong>Run this before you run the FK Query Constraint  <\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSET FOREIGN_KEY_CHECKS = 0; \r\n<\/pre>\n<p><strong>Run the FK Query <\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nALTER TABLE tbl2Name \r\n  ADD CONSTRAINT fkConstraint FOREIGN KEY(fkcolumnName) \r\n      REFERENCES tbl1Name(pkcolumnName);\r\n<\/pre>\n<p><strong>Run the below code again<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SET FOREIGN_KEY_CHECKS = 1; \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you already created a Table but you havent added foreign key constraint then you can do that by running the following statement in MySQL Run this before you run the FK Query Constraint SET FOREIGN_KEY_CHECKS = 0; Run the FK Query ALTER TABLE tbl2Name ADD CONSTRAINT fkConstraint FOREIGN KEY(fkcolumnName) REFERENCES tbl1Name(pkcolumnName); Run the below&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-add-foreign-key-to-mysql-table\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":3,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/172\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}