← Back to MySQL Course | Chapter 4: Constraints | Lesson 5 of 8

FOREIGN KEY

एक foreign key अपने card पर एक दोस्त का ID number लिखने जैसा है ताकि सबको पता चले कि आप किससे connected हैं। MySQL check करता है कि दोस्त actually exist करता है।
Syntax
sql
FOREIGN KEY (column_name)
  REFERENCES parent_table (parent_column)

FOREIGN KEY को समझना

एक table में एक FOREIGN KEY column दूसरे में एक PRIMARY KEY column को reference करता है, जो है कि MySQL कैसे enforce करता है कि related data actually tables में consistent रहे — आप एक ऐसे customer को reference नहीं कर सकते जो exist नहीं करता।

उदाहरण: Understanding FOREIGN KEY

sql
CREATE TABLE authors (id INT PRIMARY KEY);
CREATE TABLE books (
  id INT PRIMARY KEY,
  author_id INT,
  FOREIGN KEY (author_id) REFERENCES authors(id)
);

Foreign Key Constraints

एक foreign key value वाली row insert करते समय, MySQL पहले check करता है कि referenced row actually parent table में exist करती है, और अगर नहीं तो insert को पूरी तरह reject कर देता है — इसे referential integrity कहते हैं।

उदाहरण: Foreign Key Constraints

sql
CREATE TABLE authors (id INT PRIMARY KEY);
CREATE TABLE books (
  id INT PRIMARY KEY,
  author_id INT,
  FOREIGN KEY (author_id) REFERENCES authors(id)
);
INSERT INTO books VALUES (1, 999); -- rejected: no author with id 999

ON DELETE CASCADE

ON DELETE CASCADE जब भी उनका parent row delete हो तो automatically matching child rows delete कर देता है, जो cleanup के लिए convenient है लेकिन dangerous है अगर वहाँ apply किया जाए जहाँ data loss के लिए explicit confirmation चाहिए होनी चाहिए थी।

उदाहरण: ON DELETE CASCADE

sql
CREATE TABLE authors (id INT PRIMARY KEY);
CREATE TABLE books (
  id INT PRIMARY KEY,
  author_id INT,
  FOREIGN KEY (author_id) REFERENCES authors(id) ON DELETE CASCADE
);

मौजूदा Tables में Foreign Keys Add करना

independently बनाई गई tables में एक foreign key add करना आपको उनके बीच एक relationship को retroactively formalize करने देता है, हालाँकि मौजूदा data को पहले से constraint satisfy करना ज़रूरी है नहीं तो ALTER TABLE fail हो जाएगा।

उदाहरण: Adding Foreign Keys to Existing Tables

sql
ALTER TABLE books ADD CONSTRAINT fk_author FOREIGN KEY (author_id) REFERENCES authors(id);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Foreign Keys हटाना

एक foreign key drop करने के लिए इसका specific constraint name जानना ज़रूरी है (SHOW CREATE TABLE से visible), क्योंकि एक table में कई foreign keys हो सकती हैं और MySQL को exactly पता होना चाहिए कि कौन सी हटानी है।

उदाहरण: Dropping Foreign Keys

sql
SHOW CREATE TABLE books;
ALTER TABLE books DROP FOREIGN KEY fk_author;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ऐसा child row insert करना जिसकी foreign key value का कोई matching parent row न हो, जो fail होता है।
  2. parent table से पहले child table बनाना, ताकि referenced table अभी exist ही न करे।
  3. foreign key और referenced key के लिए अलग data types इस्तेमाल करना, जैसे INT और BIGINT, जो fail या reject होता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.