Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
MySQL + SQL · Lesson 135

Primary Key vs Foreign Key

दो keys जो साथ काम करती हैं

Primary key और foreign key relational database की दो सबसे महत्वपूर्ण keys हैं। Primary key table में हर row को पहचानती है; foreign key एक table को दूसरी से जोड़ती है। साथ में ये आपको data साफ़ tables में बाँटकर भी सब सही जोड़े रखने देती हैं।

💡 Primary key = table के अंदर unique id। Foreign key = दूसरी table की primary key से link।

Primary key

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);

Primary key (id) हर customer को विशिष्ट पहचानती है। यह null नहीं हो सकती, दोहरा नहीं सकती, और table में एक ही होती है। हर table में primary key होनी चाहिए।

Foreign key

CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    amount INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

यहाँ customer_id एक foreign key है जो customers.id की ओर इशारा करती है। यह हर order को एक customer से जोड़ती है, तो आप हर order में customer का नाम और details नहीं दोहराते।

Foreign key सिर्फ़ संबंधित row की id store करती है। पूरी details देखने को, आप tables join करते हैं:

SELECT orders.id, customers.name, orders.amount
FROM orders
JOIN customers ON orders.customer_id = customers.id;

यह हर order को key के ज़रिए उसके customer से जोड़ता है — कोई दोहराया data नहीं, हमेशा संगत।

Referential integrity

Foreign key referential integrity लागू करती है: आप ऐसी customer_id वाला order insert नहीं कर सकते जो customers में मौजूद न हो। MySQL इसे ठुकराता है। यह "orphan" rows रोकता है और data भरोसेमंद रखता है। Foreign key null हो सकती है (वैकल्पिक link), पर primary key कभी नहीं।

आमने-सामने

बातPrimary keyForeign key
मक़सदहर row पहचाननादूसरी table से जोड़ना
Null allowedनहींहाँ (वैकल्पिक link)
प्रति table कितनीएककई
DuplicatesAllowed नहींAllowed
किसकी ओर इशाराकुछ नहीं (यह id है)दूसरी table की key

आम गलतियाँ

  • कोई primary key नहीं। हर table को rows पहचानने को एक चाहिए।
  • बेमेल types। Foreign key column referenced key के type से मेल खाना चाहिए।
  • Non-unique column दर्शाना। Foreign key को primary या unique key दर्शानी चाहिए।
  • Referenced rows लापरवाही से delete करना। orders वाले customer को हटाना integrity तोड़ सकता है जब तक संभाला न जाए।

सारांश

  • Primary key हर row को विशिष्ट पहचानती है; कभी null नहीं, table में एक।
  • Foreign key एक table को दूसरी की key से जोड़ती है।
  • Foreign keys null हो सकती हैं और table में कई हो सकती हैं।
  • ये referential integrity लागू करती हैं, orphan rows रोककर।
  • साथ में ये data साफ़ बाँटकर वापस join करने देती हैं।

आगे, सारे key प्रकार देखिए keys in DBMS explained में, या नियम लागू कीजिए referential integrity से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Primary key और foreign key में क्या अंतर है?
Primary key अपनी table में हर row को विशिष्ट पहचानती है और null नहीं हो सकती। Foreign key एक table का column है जो दूसरी table की primary key को दर्शाता है, उनके बीच link बनाकर। एक पहचानती है; दूसरी जोड़ती है।
क्या एक table में एक से ज़्यादा foreign key हो सकती हैं?
हाँ। एक table में कई foreign keys हो सकती हैं, हर एक अलग table की ओर इशारा करती। जैसे, orders table में customer_id (customers को) और product_id (products को) हो सकती हैं। पर table में सिर्फ़ एक primary key होती है।
क्या foreign key null हो सकती है?
हाँ, foreign key null हो सकती है अगर संबंध वैकल्पिक हो। जैसे, employee row में null manager_id हो सकती है अगर उनका कोई manager न हो। पर primary key कभी null नहीं हो सकती।
Referential integrity क्या है?
Referential integrity मतलब foreign key को ऐसी value की ओर इशारा करना चाहिए जो referenced table में असल में मौजूद हो (या null हो)। यह न-मौजूद customer के लिए order जोड़ने से रोकता है, data संगत रखते हुए।
क्या foreign key को primary key दर्शानी ज़रूरी है?
इसे दूसरी table में unique column दर्शाना चाहिए — आमतौर पर primary key, पर unique key भी चलती है। यह पक्का करता है हर foreign key value referenced table में ठीक एक row से मेल खाए।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicER Diagram Symbols और Examples आपकी प्रगति: 135 / 151 (89%) · Lesson 136 / 151 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें