← Back to MySQL Course | Chapter 3: Data Types | Lesson 4 of 8

ENUM Data Type

ENUM एक multiple-choice question जैसा है: column list के answers में से सिर्फ एक हो सकता है, जैसे small, medium या large।
Syntax
sql
column_name ENUM('value1', 'value2', 'value3')

ENUM क्या है?

ENUM किसी column को table-creation time पर define की गई एक fixed list में से चुनी गई एक value तक restrict करता है, जैसे small, medium, large — यह options के एक छोटे, stable set के लिए एक पूरी lookup table का एक lightweight alternative है।

उदाहरण: What is ENUM?

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');

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

ENUM Fields Query करना

भले ही आप ENUM values को ordinary strings की तरह insert और query करते हैं, MySQL actually उन्हें internally compact integer indexes की तरह store करता है, जो column को छोटा और compare करने में तेज़ रखता है।

उदाहरण: Querying ENUM Fields

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');
SELECT * FROM shirts WHERE size = 'medium';

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

ENUM Numeric Indexing

हर defined ENUM value को 1 से शुरू होने वाला एक implicit index मिलता है (0 invalid/empty entries के लिए reserved है), और आप technically string के बजाय उस numeric index से insert या filter कर सकते हैं।

उदाहरण: ENUM Numeric Indexing

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');
SELECT * FROM shirts WHERE size + 0 = 2;

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

Invalid ENUM Values Handle करना

strict SQL mode में, ENUM की defined list में मौजूद न होने वाली एक value insert करना एक error raise करता है और row reject कर देता है; strict mode के बाहर, MySQL इसके बजाय चुपचाप एक empty string store कर सकता है।

उदाहरण: Handling Invalid ENUM Values

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('extra-large'); -- rejected in strict SQL mode

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

ENUM Options Modify करना

किसी ENUM में एक नई allowed value add करने के लिए एक ALTER TABLE चाहिए जो पूरे column को redefine करे, जो बहुत बड़ी tables पर एक धीमा, table-rewriting operation हो सकता है — ENUM की compactness के against एक real tradeoff।

उदाहरण: Modifying ENUM Options

sql
ALTER TABLE shirts MODIFY size ENUM('small', 'medium', 'large', 'x-large');

⚠️ 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. list में न होने वाली एक value insert करना, जैसे xl, जो strict mode में store होने के बजाय error देता है।
  2. एक ENUM column sort करना और alphabetical order की उम्मीद करना, जबकि यह list में position से sort होता है।
  3. मौजूदा values की पूरी list दोहराए बिना बाद में ALTER TABLE से एक नया option add करना।
🔒

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.