{"id":3987,"date":"2025-06-07T09:02:37","date_gmt":"2025-06-07T09:02:37","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3987"},"modified":"2025-06-07T09:02:39","modified_gmt":"2025-06-07T09:02:39","slug":"sql-foreign-key","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-foreign-key\/","title":{"rendered":"SQL FOREIGN KEY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>FOREIGN KEY<\/strong> creates a link between two tables by enforcing a relationship. It ensures the value in one table matches a value in another, maintaining referential integrity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Basic Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Define foreign key when creating a table\nCREATE TABLE child_table (\n  column_name data_type,\n  FOREIGN KEY (column_name) REFERENCES parent_table(parent_column)\n);\n\n-- Add foreign key to existing table\nALTER TABLE child_table\nADD CONSTRAINT fk_name FOREIGN KEY (column_name) REFERENCES parent_table(parent_column);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>CREATE TABLE departments (\n  department_id INT PRIMARY KEY,\n  department_name VARCHAR(100)\n);\n\nCREATE TABLE employees (\n  employee_id INT PRIMARY KEY,\n  employee_name VARCHAR(100),\n  department_id INT,\n  FOREIGN KEY (department_id) REFERENCES departments(department_id)\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Important Notes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>FOREIGN KEY values must match a value in the referenced (parent) table or be NULL.<\/li>\n\n\n\n<li>Helps prevent orphan records and maintains consistent data.<\/li>\n\n\n\n<li>You can specify actions like <code>ON DELETE CASCADE<\/code> to automatically delete related rows.<\/li>\n\n\n\n<li>Multiple foreign keys can exist in a table.<\/li>\n\n\n\n<li>Parent table must have a PRIMARY KEY or UNIQUE constraint on the referenced column.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>FOREIGN KEY<\/td><td>Links columns between tables, enforcing relationship<\/td><\/tr><tr><td>Referential Integrity<\/td><td>Ensures child value exists in parent table<\/td><\/tr><tr><td>Actions on Delete\/Update<\/td><td>Can cascade or restrict changes<\/td><\/tr><tr><td>Multiple FKs allowed<\/td><td>A table can have multiple foreign keys<\/td><\/tr><tr><td>Requires parent key<\/td><td>Parent column must be PRIMARY KEY or UNIQUE<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use FOREIGN KEYS to enforce data consistency across related tables and maintain relational integrity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A FOREIGN KEY creates a link between two tables by enforcing a relationship. It ensures the value in one table matches a value in another, maintaining referential integrity. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example \ud83d\udd39 Important Notes \ud83e\udde0 Quick Recap Key Point Explanation FOREIGN KEY Links columns between tables, enforcing relationship Referential Integrity Ensures child value [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3987","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}