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

PRIMARY KEY

एक primary key एक student ID number जैसी है: हर row को अपनी खुद की मिलती है, कोई इसे share नहीं करता और यह कभी खाली नहीं हो सकती।
Syntax
sql
column_name datatype PRIMARY KEY

PRIMARY KEY (column1, column2)

PRIMARY KEY को समझना

एक PRIMARY KEY किसी table की हर row को uniquely identify करती है और, एक plain UNIQUE column के उलट, कभी NULL नहीं रख सकती — यह वह anchor है जिस पर MySQL और आपका application दोनों किसी specific record को unambiguously reference करने के लिए depend करते हैं।

उदाहरण: Understanding PRIMARY KEY

sql
CREATE TABLE users (id INT PRIMARY KEY);

Composite Primary Keys

एक composite primary key दो या ज़्यादा columns को साथ span करती है, तब इस्तेमाल होती है जब कोई single column खुद unique न हो — एक classic example एक order_items table है जो (order_id, product_id) combined पर keyed है।

उदाहरण: Composite Primary Keys

sql
CREATE TABLE order_items (
  order_id INT,
  product_id INT,
  PRIMARY KEY (order_id, product_id)
);

मौजूदा Tables में PRIMARY KEY Add करना

ALTER TABLE के through एक मौजूदा table में एक primary key add करने के लिए target column में पहले से कोई NULLs और कोई duplicate values न होना ज़रूरी है, नहीं तो operation पूरी तरह fail हो जाता है।

उदाहरण: Adding PRIMARY KEY to Existing Tables

sql
ALTER TABLE users ADD PRIMARY KEY (id);

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

एक PRIMARY KEY हटाना

क्योंकि किसी table में कभी सिर्फ एक primary key हो सकती है, इसे drop करने के लिए column का नाम बताने की ज़रूरत नहीं — MySQL को पहले से exactly पता है कि आप कौन सा constraint मतलब रखते हैं।

उदाहरण: Dropping a PRIMARY KEY

sql
ALTER TABLE users DROP PRIMARY KEY;

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

Primary Keys के लिए Best Practices

लगभग हर अच्छी तरह design की गई table में एक primary key होनी चाहिए: यह lookups और joins को dramatically तेज़ करती है और हर row को दूसरी tables से reference करने के लिए एक stable, unambiguous identity देती है।

उदाहरण: Best Practices for Primary Keys

sql
CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  email VARCHAR(100)
);

⚠️ 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. किसी table में दूसरी primary key add करने की कोशिश करना, जबकि सिर्फ एक हो सकती है (इसके बजाय एक composite key इस्तेमाल करें)।
  2. एक बदलने वाली value, जैसे एक email, को primary key की तरह इस्तेमाल करना, जो फिर दूसरी tables से references तोड़ देती है।
  3. किसी primary key column में NULL या एक duplicate insert करना, जो 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.