← Back to MySQL Course | Chapter 14: Views & Indexes | Lesson 5 of 5

UNIQUE Index

एक UNIQUE index एक book index है जो यह भी insist करता है कि कोई दो entries same न हों, इसलिए कोई भी duplicate refuse कर दिया जाता है।
Syntax
sql
CREATE UNIQUE INDEX index_name
ON table_name (column_name);

एक Unique Index क्या है?

एक UNIQUE index enforce करता है कि indexed column में store की गई हर value बाकी हर row की value से अलग हो, किसी भी ऐसे insert या update को reject करते हुए जो एक duplicate बनाए।

उदाहरण: What is a Unique Index?

sql
CREATE TABLE users (id INT, email TEXT UNIQUE);
INSERT INTO users VALUES (1, '[email protected]');
-- Fails: duplicate email
INSERT INTO users VALUES (2, '[email protected]');

Table Creation के दौरान UNIQUE Index बनाना

Table-creation time पर एक unique index define करना पहली row से ही constraint bake कर देता है, जो तब सबसे clean approach है जब आपको पहले से पता हो कि एक column — जैसे एक email address — कभी repeat नहीं होना चाहिए।

उदाहरण: Creating UNIQUE Index during Table Creation

sql
CREATE TABLE users (id INT, email TEXT, UNIQUE (email));
INSERT INTO users VALUES (1, '[email protected]');

मौजूदा Tables में UNIQUE Index Add करना

पहले से exist करने वाली और पहले से data रखने वाली किसी table के लिए, ALTER TABLE आपको बाद में एक unique index add करने देता है, हालाँकि अगर मौजूदा data में पहले से duplicate values हों तो MySQL मना कर देगा।

उदाहरण: Adding UNIQUE Index to Existing Tables

sql
CREATE TABLE users (id INT, email TEXT);
INSERT INTO users VALUES (1, '[email protected]');
ALTER TABLE users ADD UNIQUE (email);

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

Composite Unique Index

एक composite unique index हर column अकेले के बजाय कई columns के combination पर uniqueness apply करता है — individual columns repeat हो सकते हैं, लेकिन कोई दो rows exact same combination share नहीं कर सकतीं।

उदाहरण: Composite Unique Index

sql
CREATE TABLE enrollments (student_id INT, course_id INT, UNIQUE (student_id, course_id));
INSERT INTO enrollments VALUES (1, 101);
-- Allowed: same student, different course
INSERT INTO enrollments VALUES (1, 102);

एक Unique Index हटाना

एक unique index drop करना uniqueness enforcement को performance benefit के साथ हटा देता है, यानी आगे उस column में duplicate values फिर से allowed हो जाती हैं।

उदाहरण: Dropping a Unique Index

sql
CREATE TABLE users (id INT, email TEXT, UNIQUE KEY uq_email (email));
ALTER TABLE users DROP INDEX uq_email;

⚠️ 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. पहले से duplicates वाले एक column पर एक unique index बनाना, जो fail हो जाता है।
  2. यह मान लेना कि एक unique index कई NULL values रोकता है, जबकि MySQL कई NULLs allow करता है।
  3. दो अलग unique indexes के एक pair को unique बनाने की उम्मीद करना, जबकि एक pair को एक composite UNIQUE (a, b) चाहिए।
चैप्टर सारांश
  • एक view एक saved query है जिसे आप create, update, और drop कर सकते हैं।
  • Indexes data lookups तेज़ करते हैं और create तथा drop किए जा सकते हैं।
  • एक UNIQUE index duplicate values भी रोकता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.