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

CREATE INDEX

एक index किसी book के पीछे के index जैसा है जो आपको सब कुछ पलटने के बजाय सीधे एक page पर jump करने देता है।
Syntax
sql
CREATE [UNIQUE] INDEX index_name
ON table_name (column1[, column2]);

एक Index क्या है?

एक index एक अलग data structure है जिसे MySQL specific columns पर lookups को कहीं ज़्यादा तेज़ बनाने के लिए table के साथ maintain करता है, conceptually किसी textbook के पीछे के index जैसा काम करते हुए जो आपको पूरी book scan करने पर मजबूर करने के बजाय सीधे एक page पर point करता है।

उदाहरण: What is an Index?

sql
CREATE TABLE users (id INT, email TEXT);
CREATE INDEX idx_email ON users (email);

एक UNIQUE Index बनाना

एक UNIQUE index double duty करता है: यह indexed column पर lookups को उतना ही तेज़ बनाता है जितना एक normal index करता, साथ ही यह enforce भी करता है कि कोई दो rows उस column में same value store न कर पाएँ।

उदाहरण: Creating a UNIQUE Index

sql
CREATE TABLE users (id INT, email TEXT);
CREATE UNIQUE INDEX idx_unique_email ON users (email);

एक Composite Index बनाना

एक composite index एक साथ कई columns span करता है और सबसे effective तब होता है जब queries उन same columns को साथ filter करती हैं, index definition में जिस order में वे दिखते हैं उसी order में।

उदाहरण: Creating a Composite Index

sql
CREATE TABLE orders (id INT, customer_id INT, order_date DATE);
CREATE INDEX idx_customer_date ON orders (customer_id, order_date);

ALTER TABLE इस्तेमाल करके Indexes Add करना

आप सिर्फ किसी table के पहली बार बनते समय indexes define करने तक limited नहीं हैं — ALTER TABLE आपको एक ऐसी table में एक index add करने देता है जो पहले से exist करती है और पहले से data रखती है।

उदाहरण: Adding Indexes Using ALTER TABLE

sql
CREATE TABLE products (id INT, sku TEXT);
INSERT INTO products VALUES (1, 'SKU123');
ALTER TABLE products ADD INDEX idx_sku (sku);

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

Prefix Text Parts Index करना

बहुत लंबे text columns के लिए, सिर्फ पहले कई characters index करना (एक prefix index) index को छोटा और तेज़ रखता है साथ ही अभी भी ज़्यादातर searches को तेज़ करता है, थोड़ी precision को एक बड़ी storage savings के लिए trade करते हुए।

उदाहरण: Indexing Prefix Text Parts

sql
CREATE TABLE articles (id INT, title VARCHAR(255));
CREATE INDEX idx_title_prefix ON articles (title(20));
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. बिना prefix length के एक TEXT column index करना, जैसे (email(100)), जो BLOB/TEXT column used in key specification without a key length के साथ fail होता है।
  2. हर column में एक index add करना, जो INSERT, UPDATE, और DELETE धीमा करता है और storage बर्बाद करता है।
  3. एक composite index को गलत column order में बनाना, ताकि सिर्फ दूसरे column पर filter करने वाली queries इसे इस्तेमाल न कर सकें।
🔒

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.