{"id":4892,"date":"2012-05-30T08:00:25","date_gmt":"2012-05-30T08:00:25","guid":{"rendered":"http:\/\/phpcodez.com\/?p=4892"},"modified":"2012-05-30T08:00:25","modified_gmt":"2012-05-30T08:00:25","slug":"sql-foreign-key","status":"publish","type":"post","link":"http:\/\/phpcodez.com\/sql-foreign-key\/","title":{"rendered":"SQL FOREIGN KEY"},"content":{"rendered":"<p>SQL FOREIGN KEYin one table points to the primary key of another table .<\/p>\n<p>Table &#8211; users<\/p>\n<p>+&#8212;-+&#8212;&#8212;+<br \/>\n| uid | name |<br \/>\n+&#8212;-+&#8212;&#8212;+<br \/>\n| \u00a01 | AAAA |<br \/>\n| \u00a02 | BBBB |<br \/>\n| \u00a03 | CCCC |<br \/>\n| \u00a04 | DDDD |<br \/>\n| \u00a05 | EEEE |<br \/>\n+&#8212;-+&#8212;&#8212;+<\/p>\n<p>Table \u00a0&#8211; orders<\/p>\n<p>+&#8212;&#8211;+&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| oid | uid | items \u00a0|<br \/>\n+&#8212;&#8211;+&#8212;&#8211;+&#8212;&#8212;&#8211;+<br \/>\n| \u00a0\u00a01 | \u00a0\u00a01 | Pen \u00a0\u00a0\u00a0|<br \/>\n| \u00a0\u00a02 | \u00a0\u00a02 | Watch \u00a0|<br \/>\n| \u00a0\u00a03 | \u00a0\u00a03 | shoe \u00a0\u00a0|<br \/>\n| \u00a0\u00a04 | \u00a0\u00a04 | mobile |<br \/>\n+&#8212;&#8211;+&#8212;&#8211;+&#8212;&#8212;&#8211;+<\/p>\n<p>Here the field uid in the table orders points to the column uid in users .<\/p>\n<p>That is \u00a0uid in the orders is the foreign key and that in users is primary key<\/p>\n<p>FOREIGN KEY constraint prevent any action that may destroy the relation between the tables.<\/p>\n<p>It also prevent invalid data and make sure that the data to be inserted should be one of the primary key value in another<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>CREATE TABLE IF NOT EXISTS `orders` (<br \/>\n`oid` int(11) NOT NULL AUTO_INCREMENT,<br \/>\n`uid` int(11) NOT NULL,<br \/>\n`items` varchar(255) NOT NULL,<br \/>\nPRIMARY KEY (`oid`),<br \/>\nFOREIGN KEY (uid) REFERENCES users(uid)<br \/>\n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL FOREIGN KEYin one table points to the primary key of another table . Table &#8211; users +&#8212;-+&#8212;&#8212;+ | uid | name | +&#8212;-+&#8212;&#8212;+ | \u00a01 | AAAA | | \u00a02 | BBBB | | \u00a03 | CCCC | | \u00a04 | DDDD | | \u00a05 | EEEE | +&#8212;-+&#8212;&#8212;+ Table \u00a0&#8211; orders +&#8212;&#8211;+&#8212;&#8211;+&#8212;&#8212;&#8211;+ | &hellip; <a href=\"http:\/\/phpcodez.com\/sql-foreign-key\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL FOREIGN KEY<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[332,370,392,457,461],"class_list":["post-4892","post","type-post","status-publish","format-standard","hentry","category-sql","tag-mysql","tag-php","tag-query","tag-sql","tag-statement"],"_links":{"self":[{"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/posts\/4892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/comments?post=4892"}],"version-history":[{"count":0,"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/posts\/4892\/revisions"}],"wp:attachment":[{"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/media?parent=4892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/categories?post=4892"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/phpcodez.com\/wp-json\/wp\/v2\/tags?post=4892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}